Vince Bridgers | 94fb0ef | 2014-03-17 17:52:35 -0500 | [diff] [blame] | 1 | /* Altera TSE SGDMA and MSGDMA Linux driver |
| 2 | * Copyright (C) 2014 Altera Corporation. All rights reserved |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/netdevice.h> |
| 18 | #include "altera_utils.h" |
| 19 | #include "altera_tse.h" |
| 20 | #include "altera_msgdmahw.h" |
| 21 | |
| 22 | /* No initialization work to do for MSGDMA */ |
| 23 | int msgdma_initialize(struct altera_tse_private *priv) |
| 24 | { |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | void msgdma_uninitialize(struct altera_tse_private *priv) |
| 29 | { |
| 30 | } |
| 31 | |
Vince Bridgers | 37c0ffa | 2014-04-24 16:58:08 -0500 | [diff] [blame^] | 32 | void msgdma_start_rxdma(struct altera_tse_private *priv) |
| 33 | { |
| 34 | } |
| 35 | |
Vince Bridgers | 94fb0ef | 2014-03-17 17:52:35 -0500 | [diff] [blame] | 36 | void msgdma_reset(struct altera_tse_private *priv) |
| 37 | { |
| 38 | int counter; |
| 39 | struct msgdma_csr *txcsr = |
| 40 | (struct msgdma_csr *)priv->tx_dma_csr; |
| 41 | struct msgdma_csr *rxcsr = |
| 42 | (struct msgdma_csr *)priv->rx_dma_csr; |
| 43 | |
| 44 | /* Reset Rx mSGDMA */ |
| 45 | iowrite32(MSGDMA_CSR_STAT_MASK, &rxcsr->status); |
| 46 | iowrite32(MSGDMA_CSR_CTL_RESET, &rxcsr->control); |
| 47 | |
| 48 | counter = 0; |
| 49 | while (counter++ < ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) { |
| 50 | if (tse_bit_is_clear(&rxcsr->status, |
| 51 | MSGDMA_CSR_STAT_RESETTING)) |
| 52 | break; |
| 53 | udelay(1); |
| 54 | } |
| 55 | |
| 56 | if (counter >= ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) |
| 57 | netif_warn(priv, drv, priv->dev, |
| 58 | "TSE Rx mSGDMA resetting bit never cleared!\n"); |
| 59 | |
| 60 | /* clear all status bits */ |
| 61 | iowrite32(MSGDMA_CSR_STAT_MASK, &rxcsr->status); |
| 62 | |
| 63 | /* Reset Tx mSGDMA */ |
| 64 | iowrite32(MSGDMA_CSR_STAT_MASK, &txcsr->status); |
| 65 | iowrite32(MSGDMA_CSR_CTL_RESET, &txcsr->control); |
| 66 | |
| 67 | counter = 0; |
| 68 | while (counter++ < ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) { |
| 69 | if (tse_bit_is_clear(&txcsr->status, |
| 70 | MSGDMA_CSR_STAT_RESETTING)) |
| 71 | break; |
| 72 | udelay(1); |
| 73 | } |
| 74 | |
| 75 | if (counter >= ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) |
| 76 | netif_warn(priv, drv, priv->dev, |
| 77 | "TSE Tx mSGDMA resetting bit never cleared!\n"); |
| 78 | |
| 79 | /* clear all status bits */ |
| 80 | iowrite32(MSGDMA_CSR_STAT_MASK, &txcsr->status); |
| 81 | } |
| 82 | |
| 83 | void msgdma_disable_rxirq(struct altera_tse_private *priv) |
| 84 | { |
| 85 | struct msgdma_csr *csr = priv->rx_dma_csr; |
| 86 | tse_clear_bit(&csr->control, MSGDMA_CSR_CTL_GLOBAL_INTR); |
| 87 | } |
| 88 | |
| 89 | void msgdma_enable_rxirq(struct altera_tse_private *priv) |
| 90 | { |
| 91 | struct msgdma_csr *csr = priv->rx_dma_csr; |
| 92 | tse_set_bit(&csr->control, MSGDMA_CSR_CTL_GLOBAL_INTR); |
| 93 | } |
| 94 | |
| 95 | void msgdma_disable_txirq(struct altera_tse_private *priv) |
| 96 | { |
| 97 | struct msgdma_csr *csr = priv->tx_dma_csr; |
| 98 | tse_clear_bit(&csr->control, MSGDMA_CSR_CTL_GLOBAL_INTR); |
| 99 | } |
| 100 | |
| 101 | void msgdma_enable_txirq(struct altera_tse_private *priv) |
| 102 | { |
| 103 | struct msgdma_csr *csr = priv->tx_dma_csr; |
| 104 | tse_set_bit(&csr->control, MSGDMA_CSR_CTL_GLOBAL_INTR); |
| 105 | } |
| 106 | |
| 107 | void msgdma_clear_rxirq(struct altera_tse_private *priv) |
| 108 | { |
| 109 | struct msgdma_csr *csr = priv->rx_dma_csr; |
| 110 | iowrite32(MSGDMA_CSR_STAT_IRQ, &csr->status); |
| 111 | } |
| 112 | |
| 113 | void msgdma_clear_txirq(struct altera_tse_private *priv) |
| 114 | { |
| 115 | struct msgdma_csr *csr = priv->tx_dma_csr; |
| 116 | iowrite32(MSGDMA_CSR_STAT_IRQ, &csr->status); |
| 117 | } |
| 118 | |
| 119 | /* return 0 to indicate transmit is pending */ |
| 120 | int msgdma_tx_buffer(struct altera_tse_private *priv, struct tse_buffer *buffer) |
| 121 | { |
| 122 | struct msgdma_extended_desc *desc = priv->tx_dma_desc; |
| 123 | |
| 124 | iowrite32(lower_32_bits(buffer->dma_addr), &desc->read_addr_lo); |
| 125 | iowrite32(upper_32_bits(buffer->dma_addr), &desc->read_addr_hi); |
| 126 | iowrite32(0, &desc->write_addr_lo); |
| 127 | iowrite32(0, &desc->write_addr_hi); |
| 128 | iowrite32(buffer->len, &desc->len); |
| 129 | iowrite32(0, &desc->burst_seq_num); |
| 130 | iowrite32(MSGDMA_DESC_TX_STRIDE, &desc->stride); |
| 131 | iowrite32(MSGDMA_DESC_CTL_TX_SINGLE, &desc->control); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | u32 msgdma_tx_completions(struct altera_tse_private *priv) |
| 136 | { |
| 137 | u32 ready = 0; |
| 138 | u32 inuse; |
| 139 | u32 status; |
| 140 | struct msgdma_csr *txcsr = |
| 141 | (struct msgdma_csr *)priv->tx_dma_csr; |
| 142 | |
| 143 | /* Get number of sent descriptors */ |
| 144 | inuse = ioread32(&txcsr->rw_fill_level) & 0xffff; |
| 145 | |
| 146 | if (inuse) { /* Tx FIFO is not empty */ |
| 147 | ready = priv->tx_prod - priv->tx_cons - inuse - 1; |
| 148 | } else { |
| 149 | /* Check for buffered last packet */ |
| 150 | status = ioread32(&txcsr->status); |
| 151 | if (status & MSGDMA_CSR_STAT_BUSY) |
| 152 | ready = priv->tx_prod - priv->tx_cons - 1; |
| 153 | else |
| 154 | ready = priv->tx_prod - priv->tx_cons; |
| 155 | } |
| 156 | return ready; |
| 157 | } |
| 158 | |
| 159 | /* Put buffer to the mSGDMA RX FIFO |
| 160 | */ |
Vince Bridgers | 37c0ffa | 2014-04-24 16:58:08 -0500 | [diff] [blame^] | 161 | void msgdma_add_rx_desc(struct altera_tse_private *priv, |
Vince Bridgers | 94fb0ef | 2014-03-17 17:52:35 -0500 | [diff] [blame] | 162 | struct tse_buffer *rxbuffer) |
| 163 | { |
| 164 | struct msgdma_extended_desc *desc = priv->rx_dma_desc; |
| 165 | u32 len = priv->rx_dma_buf_sz; |
| 166 | dma_addr_t dma_addr = rxbuffer->dma_addr; |
| 167 | u32 control = (MSGDMA_DESC_CTL_END_ON_EOP |
| 168 | | MSGDMA_DESC_CTL_END_ON_LEN |
| 169 | | MSGDMA_DESC_CTL_TR_COMP_IRQ |
| 170 | | MSGDMA_DESC_CTL_EARLY_IRQ |
| 171 | | MSGDMA_DESC_CTL_TR_ERR_IRQ |
| 172 | | MSGDMA_DESC_CTL_GO); |
| 173 | |
| 174 | iowrite32(0, &desc->read_addr_lo); |
| 175 | iowrite32(0, &desc->read_addr_hi); |
| 176 | iowrite32(lower_32_bits(dma_addr), &desc->write_addr_lo); |
| 177 | iowrite32(upper_32_bits(dma_addr), &desc->write_addr_hi); |
| 178 | iowrite32(len, &desc->len); |
| 179 | iowrite32(0, &desc->burst_seq_num); |
| 180 | iowrite32(0x00010001, &desc->stride); |
| 181 | iowrite32(control, &desc->control); |
Vince Bridgers | 94fb0ef | 2014-03-17 17:52:35 -0500 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* status is returned on upper 16 bits, |
| 185 | * length is returned in lower 16 bits |
| 186 | */ |
| 187 | u32 msgdma_rx_status(struct altera_tse_private *priv) |
| 188 | { |
| 189 | u32 rxstatus = 0; |
| 190 | u32 pktlength; |
| 191 | u32 pktstatus; |
| 192 | struct msgdma_csr *rxcsr = |
| 193 | (struct msgdma_csr *)priv->rx_dma_csr; |
| 194 | struct msgdma_response *rxresp = |
| 195 | (struct msgdma_response *)priv->rx_dma_resp; |
| 196 | |
| 197 | if (ioread32(&rxcsr->resp_fill_level) & 0xffff) { |
| 198 | pktlength = ioread32(&rxresp->bytes_transferred); |
| 199 | pktstatus = ioread32(&rxresp->status); |
| 200 | rxstatus = pktstatus; |
| 201 | rxstatus = rxstatus << 16; |
| 202 | rxstatus |= (pktlength & 0xffff); |
| 203 | } |
| 204 | return rxstatus; |
| 205 | } |