firewire: cdev: add PHY packet reception
[pandora-kernel.git] / drivers / firewire / ohci.c
index 9c588fd..08afccc 100644 (file)
@@ -18,6 +18,7 @@
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
+#include <linux/bug.h>
 #include <linux/compiler.h>
 #include <linux/delay.h>
 #include <linux/device.h>
@@ -33,6 +34,7 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
+#include <linux/mutex.h>
 #include <linux/pci.h>
 #include <linux/pci_ids.h>
 #include <linux/spinlock.h>
@@ -170,7 +172,10 @@ struct fw_ohci {
        int generation;
        int request_generation; /* for timestamping incoming requests */
        unsigned quirks;
+       unsigned int pri_req_max;
        u32 bus_time;
+       bool is_root;
+       bool csr_state_setclear_abdicate;
 
        /*
         * Spinlock for accessing fw_ohci data.  Never call out of
@@ -178,6 +183,8 @@ struct fw_ohci {
         */
        spinlock_t lock;
 
+       struct mutex phy_reg_mutex;
+
        struct ar_context ar_request_ctx;
        struct ar_context ar_response_ctx;
        struct context at_request_ctx;
@@ -513,13 +520,10 @@ static int write_phy_reg(const struct fw_ohci *ohci, int addr, u32 val)
        return -EBUSY;
 }
 
-static int ohci_update_phy_reg(struct fw_card *card, int addr,
-                              int clear_bits, int set_bits)
+static int update_phy_reg(struct fw_ohci *ohci, int addr,
+                         int clear_bits, int set_bits)
 {
-       struct fw_ohci *ohci = fw_ohci(card);
-       int ret;
-
-       ret = read_phy_reg(ohci, addr);
+       int ret = read_phy_reg(ohci, addr);
        if (ret < 0)
                return ret;
 
@@ -537,13 +541,38 @@ static int read_paged_phy_reg(struct fw_ohci *ohci, int page, int addr)
 {
        int ret;
 
-       ret = ohci_update_phy_reg(&ohci->card, 7, PHY_PAGE_SELECT, page << 5);
+       ret = update_phy_reg(ohci, 7, PHY_PAGE_SELECT, page << 5);
        if (ret < 0)
                return ret;
 
        return read_phy_reg(ohci, addr);
 }
 
+static int ohci_read_phy_reg(struct fw_card *card, int addr)
+{
+       struct fw_ohci *ohci = fw_ohci(card);
+       int ret;
+
+       mutex_lock(&ohci->phy_reg_mutex);
+       ret = read_phy_reg(ohci, addr);
+       mutex_unlock(&ohci->phy_reg_mutex);
+
+       return ret;
+}
+
+static int ohci_update_phy_reg(struct fw_card *card, int addr,
+                              int clear_bits, int set_bits)
+{
+       struct fw_ohci *ohci = fw_ohci(card);
+       int ret;
+
+       mutex_lock(&ohci->phy_reg_mutex);
+       ret = update_phy_reg(ohci, addr, clear_bits, set_bits);
+       mutex_unlock(&ohci->phy_reg_mutex);
+
+       return ret;
+}
+
 static int ar_context_add_page(struct ar_context *ctx)
 {
        struct device *dev = ctx->ohci->card.device;
@@ -1399,6 +1428,7 @@ static void bus_reset_tasklet(unsigned long data)
        unsigned long flags;
        void *free_rom = NULL;
        dma_addr_t free_rom_bus = 0;
+       bool is_new_root;
 
        reg = reg_read(ohci, OHCI1394_NodeID);
        if (!(reg & OHCI1394_NodeID_idValid)) {
@@ -1412,6 +1442,12 @@ static void bus_reset_tasklet(unsigned long data)
        ohci->node_id = reg & (OHCI1394_NodeID_busNumber |
                               OHCI1394_NodeID_nodeNumber);
 
+       is_new_root = (reg & OHCI1394_NodeID_root) != 0;
+       if (!(ohci->is_root && is_new_root))
+               reg_write(ohci, OHCI1394_LinkControlSet,
+                         OHCI1394_LinkControl_cycleMaster);
+       ohci->is_root = is_new_root;
+
        reg = reg_read(ohci, OHCI1394_SelfIDCount);
        if (reg & OHCI1394_SelfIDCount_selfIDError) {
                fw_notify("inconsistent self IDs\n");
@@ -1519,7 +1555,9 @@ static void bus_reset_tasklet(unsigned long data)
                    self_id_count, ohci->self_id_buffer);
 
        fw_core_handle_bus_reset(&ohci->card, ohci->node_id, generation,
-                                self_id_count, ohci->self_id_buffer);
+                                self_id_count, ohci->self_id_buffer,
+                                ohci->csr_state_setclear_abdicate);
+       ohci->csr_state_setclear_abdicate = false;
 }
 
 static irqreturn_t irq_handler(int irq, void *data)
@@ -1663,7 +1701,7 @@ static int configure_1394a_enhancements(struct fw_ohci *ohci)
                clear = PHY_ENABLE_ACCEL | PHY_ENABLE_MULTI;
                set = 0;
        }
-       ret = ohci_update_phy_reg(&ohci->card, 5, clear, set);
+       ret = update_phy_reg(ohci, 5, clear, set);
        if (ret < 0)
                return ret;
 
@@ -1685,7 +1723,7 @@ static int ohci_enable(struct fw_card *card,
 {
        struct fw_ohci *ohci = fw_ohci(card);
        struct pci_dev *dev = to_pci_dev(card->device);
-       u32 lps, seconds, irqs;
+       u32 lps, seconds, version, irqs;
        int i, ret;
 
        if (software_reset(ohci)) {
@@ -1721,10 +1759,9 @@ static int ohci_enable(struct fw_card *card,
                  OHCI1394_HCControl_noByteSwapData);
 
        reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->self_id_bus);
-       reg_write(ohci, OHCI1394_LinkControlClear,
-                 OHCI1394_LinkControl_rcvPhyPkt);
        reg_write(ohci, OHCI1394_LinkControlSet,
                  OHCI1394_LinkControl_rcvSelfID |
+                 OHCI1394_LinkControl_rcvPhyPkt |
                  OHCI1394_LinkControl_cycleTimerEnable |
                  OHCI1394_LinkControl_cycleMaster);
 
@@ -1738,6 +1775,19 @@ static int ohci_enable(struct fw_card *card,
        reg_write(ohci, OHCI1394_IsochronousCycleTimer, seconds << 25);
        ohci->bus_time = seconds & ~0x3f;
 
+       version = reg_read(ohci, OHCI1394_Version) & 0x00ff00ff;
+       if (version >= OHCI_VERSION_1_1) {
+               reg_write(ohci, OHCI1394_InitialChannelsAvailableHi,
+                         0xfffffffe);
+               card->broadcast_channel_auto_allocated = true;
+       }
+
+       /* Get implemented bits of the priority arbitration request counter. */
+       reg_write(ohci, OHCI1394_FairnessControl, 0x3f);
+       ohci->pri_req_max = reg_read(ohci, OHCI1394_FairnessControl) & 0x3f;
+       reg_write(ohci, OHCI1394_FairnessControl, 0);
+       card->priority_budget_implemented = ohci->pri_req_max != 0;
+
        ar_context_run(&ohci->ar_request_ctx);
        ar_context_run(&ohci->ar_response_ctx);
 
@@ -1830,12 +1880,8 @@ static int ohci_enable(struct fw_card *card,
                  OHCI1394_HCControl_BIBimageValid);
        flush_writes(ohci);
 
-       /*
-        * We are ready to go, initiate bus reset to finish the
-        * initialization.
-        */
-
-       fw_core_initiate_bus_reset(&ohci->card, 1);
+       /* We are ready to go, reset bus to finish initialization. */
+       fw_schedule_bus_reset(&ohci->card, false, true);
 
        return 0;
 }
@@ -1910,7 +1956,7 @@ static int ohci_set_config_rom(struct fw_card *card,
         * takes effect.
         */
        if (ret == 0)
-               fw_core_initiate_bus_reset(&ohci->card, 1);
+               fw_schedule_bus_reset(&ohci->card, true, true);
        else
                dma_free_coherent(ohci->card.device, CONFIG_ROM_SIZE,
                                  next_config_rom, next_config_rom_bus);
@@ -2000,13 +2046,26 @@ static int ohci_enable_phys_dma(struct fw_card *card,
 #endif /* CONFIG_FIREWIRE_OHCI_REMOTE_DMA */
 }
 
-static u32 ohci_read_csr_reg(struct fw_card *card, int csr_offset)
+static u32 ohci_read_csr(struct fw_card *card, int csr_offset)
 {
        struct fw_ohci *ohci = fw_ohci(card);
        unsigned long flags;
        u32 value;
 
        switch (csr_offset) {
+       case CSR_STATE_CLEAR:
+       case CSR_STATE_SET:
+               if (ohci->is_root &&
+                   (reg_read(ohci, OHCI1394_LinkControlSet) &
+                    OHCI1394_LinkControl_cycleMaster))
+                       value = CSR_STATE_BIT_CMSTR;
+               else
+                       value = 0;
+               if (ohci->csr_state_setclear_abdicate)
+                       value |= CSR_STATE_BIT_ABDICATE;
+
+               return value;
+
        case CSR_NODE_IDS:
                return reg_read(ohci, OHCI1394_NodeID) << 16;
 
@@ -2028,18 +2087,42 @@ static u32 ohci_read_csr_reg(struct fw_card *card, int csr_offset)
                value = reg_read(ohci, OHCI1394_ATRetries);
                return (value >> 4) & 0x0ffff00f;
 
+       case CSR_PRIORITY_BUDGET:
+               return (reg_read(ohci, OHCI1394_FairnessControl) & 0x3f) |
+                       (ohci->pri_req_max << 8);
+
        default:
                WARN_ON(1);
                return 0;
        }
 }
 
-static void ohci_write_csr_reg(struct fw_card *card, int csr_offset, u32 value)
+static void ohci_write_csr(struct fw_card *card, int csr_offset, u32 value)
 {
        struct fw_ohci *ohci = fw_ohci(card);
        unsigned long flags;
 
        switch (csr_offset) {
+       case CSR_STATE_CLEAR:
+               if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) {
+                       reg_write(ohci, OHCI1394_LinkControlClear,
+                                 OHCI1394_LinkControl_cycleMaster);
+                       flush_writes(ohci);
+               }
+               if (value & CSR_STATE_BIT_ABDICATE)
+                       ohci->csr_state_setclear_abdicate = false;
+               break;
+
+       case CSR_STATE_SET:
+               if ((value & CSR_STATE_BIT_CMSTR) && ohci->is_root) {
+                       reg_write(ohci, OHCI1394_LinkControlSet,
+                                 OHCI1394_LinkControl_cycleMaster);
+                       flush_writes(ohci);
+               }
+               if (value & CSR_STATE_BIT_ABDICATE)
+                       ohci->csr_state_setclear_abdicate = true;
+               break;
+
        case CSR_NODE_IDS:
                reg_write(ohci, OHCI1394_NodeID, value >> 16);
                flush_writes(ohci);
@@ -2065,6 +2148,11 @@ static void ohci_write_csr_reg(struct fw_card *card, int csr_offset, u32 value)
                flush_writes(ohci);
                break;
 
+       case CSR_PRIORITY_BUDGET:
+               reg_write(ohci, OHCI1394_FairnessControl, value & 0x3f);
+               flush_writes(ohci);
+               break;
+
        default:
                WARN_ON(1);
                break;
@@ -2502,14 +2590,15 @@ static int ohci_queue_iso(struct fw_iso_context *base,
 
 static const struct fw_card_driver ohci_driver = {
        .enable                 = ohci_enable,
+       .read_phy_reg           = ohci_read_phy_reg,
        .update_phy_reg         = ohci_update_phy_reg,
        .set_config_rom         = ohci_set_config_rom,
        .send_request           = ohci_send_request,
        .send_response          = ohci_send_response,
        .cancel_packet          = ohci_cancel_packet,
        .enable_phys_dma        = ohci_enable_phys_dma,
-       .read_csr_reg           = ohci_read_csr_reg,
-       .write_csr_reg          = ohci_write_csr_reg,
+       .read_csr               = ohci_read_csr,
+       .write_csr              = ohci_write_csr,
 
        .allocate_iso_context   = ohci_allocate_iso_context,
        .free_iso_context       = ohci_free_iso_context,
@@ -2577,6 +2666,7 @@ static int __devinit pci_probe(struct pci_dev *dev,
        pci_set_drvdata(dev, ohci);
 
        spin_lock_init(&ohci->lock);
+       mutex_init(&ohci->phy_reg_mutex);
 
        tasklet_init(&ohci->bus_reset_tasklet,
                     bus_reset_tasklet, (unsigned long)ohci);