Use DMA for SCSI and SD card transfers for a massive performance boost.
[SCSI2SD-V6.git] / software / SCSI2SD / src / scsiPhy.c
index fc42b4ff21e6f94a2be9144ec970dc10f7a98717..45362a7cc35928c33356e8dba90eb68a5a05d6cc 100755 (executable)
 \r
 #define scsiTarget_AUX_CTL (* (reg8 *) scsiTarget_datapath__DP_AUX_CTL_REG)\r
 \r
+// DMA controller can't handle any more bytes.\r
+#define MAX_DMA_BYTES 4095\r
+\r
+// Private DMA variables.\r
+static int dmaInProgress = 0;\r
+// used when transferring > MAX_DMA_BYTES.\r
+static uint8_t* dmaBuffer = NULL;\r
+static uint32_t dmaSentCount = 0;\r
+static uint32_t dmaTotalCount = 0;\r
+\r
+static uint8 scsiDmaRxChan = CY_DMA_INVALID_CHANNEL;\r
+static uint8 scsiDmaTxChan = CY_DMA_INVALID_CHANNEL;\r
+\r
+// DMA descriptors\r
+static uint8 scsiDmaRxTd[1] = { CY_DMA_INVALID_TD };\r
+static uint8 scsiDmaTxTd[1] = { CY_DMA_INVALID_TD };\r
+\r
+// Source of dummy bytes for DMA reads\r
+static uint8 dummyBuffer = 0xFF;\r
+\r
+volatile static uint8 rxDMAComplete;\r
+volatile static uint8 txDMAComplete;\r
+\r
+CY_ISR_PROTO(scsiRxCompleteISR);\r
+CY_ISR(scsiRxCompleteISR)\r
+{\r
+       rxDMAComplete = 1;\r
+}\r
+\r
+CY_ISR_PROTO(scsiTxCompleteISR);\r
+CY_ISR(scsiTxCompleteISR)\r
+{\r
+       txDMAComplete = 1;\r
+}\r
+\r
 CY_ISR_PROTO(scsiResetISR);\r
 CY_ISR(scsiResetISR)\r
 {\r
@@ -29,7 +64,8 @@ CY_ISR(scsiResetISR)
        SCSI_RST_ClearInterrupt();\r
 }\r
 \r
-uint8 scsiReadDBxPins()\r
+uint8_t\r
+scsiReadDBxPins()\r
 {\r
        return\r
                (SCSI_ReadPin(SCSI_In_DBx_DB7) << 7) |\r
@@ -39,79 +75,259 @@ uint8 scsiReadDBxPins()
                (SCSI_ReadPin(SCSI_In_DBx_DB3) << 3) |\r
                (SCSI_ReadPin(SCSI_In_DBx_DB2) << 2) |\r
                (SCSI_ReadPin(SCSI_In_DBx_DB1) << 1) |\r
-               SCSI_ReadPin(SCSI_In_DBx_DB0);          \r
+               SCSI_ReadPin(SCSI_In_DBx_DB0);\r
 }\r
 \r
-uint8 scsiReadByte(void)\r
+uint8_t\r
+scsiReadByte(void)\r
 {\r
-       while (!(CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 1) &&\r
-               !scsiDev.resetFlag) {}\r
-       CY_SET_REG8(scsiTarget_datapath__F0_REG, 0);\r
-       while (!(CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 2) &&\r
-               !scsiDev.resetFlag) {}\r
-               \r
+       while (scsiPhyTxFifoFull() && !scsiDev.resetFlag) {}\r
+       scsiPhyTx(0);\r
+\r
+       while (scsiPhyRxFifoEmpty() && !scsiDev.resetFlag) {}\r
+       uint8_t val = scsiPhyRx();\r
+\r
        while (SCSI_ReadPin(SCSI_In_ACK) && !scsiDev.resetFlag) {}\r
-               \r
-       return CY_GET_REG8(scsiTarget_datapath__F1_REG);\r
+\r
+       return val;\r
 }\r
 \r
-void scsiRead(uint8* data, uint32 count)\r
+static void\r
+scsiReadPIO(uint8* data, uint32 count)\r
 {\r
        int prep = 0;\r
        int i = 0;\r
 \r
        while (i < count && !scsiDev.resetFlag)\r
        {\r
-               if (prep < count && (CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 1))\r
+               uint8_t status = scsiPhyStatus();\r
+\r
+               if (prep < count && (status & SCSI_PHY_TX_FIFO_NOT_FULL))\r
                {\r
-                       CY_SET_REG8(scsiTarget_datapath__F0_REG, 0);\r
+                       scsiPhyTx(0);\r
                        ++prep;\r
                }\r
-               if ((CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 2))\r
+               if (status & SCSI_PHY_RX_FIFO_NOT_EMPTY)\r
                {\r
-                       data[i] =  CY_GET_REG8(scsiTarget_datapath__F1_REG);\r
+                       data[i] = scsiPhyRx();\r
                        ++i;\r
                }\r
        }\r
        while (SCSI_ReadPin(SCSI_In_ACK) && !scsiDev.resetFlag) {}\r
-\r
 }\r
 \r
-void scsiWriteByte(uint8 value)\r
+static void\r
+doRxSingleDMA(uint8* data, uint32 count)\r
 {\r
-       while (!(CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 1) &&\r
-               !scsiDev.resetFlag) {}\r
-       CY_SET_REG8(scsiTarget_datapath__F0_REG, value);\r
+       // Prepare DMA transfer\r
+       dmaInProgress = 1;\r
 \r
-       // TODO maybe move this TX EMPTY check to scsiEnterPhase ?\r
-       //while (!(CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 4)) {}\r
-       while (!(CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 2) &&\r
-               !scsiDev.resetFlag) {}\r
-       value = CY_GET_REG8(scsiTarget_datapath__F1_REG);\r
+       CyDmaTdSetConfiguration(\r
+               scsiDmaTxTd[0],\r
+               count,\r
+               CY_DMA_DISABLE_TD, // Disable the DMA channel when TD completes count bytes\r
+               SCSI_TX_DMA__TD_TERMOUT_EN // Trigger interrupt when complete\r
+               );\r
+       CyDmaTdSetConfiguration(\r
+               scsiDmaRxTd[0],\r
+               count,\r
+               CY_DMA_DISABLE_TD, // Disable the DMA channel when TD completes count bytes\r
+               TD_INC_DST_ADR |\r
+                       SCSI_RX_DMA__TD_TERMOUT_EN // Trigger interrupt when complete\r
+               );\r
        \r
+       CyDmaTdSetAddress(\r
+               scsiDmaTxTd[0],\r
+               LO16((uint32)&dummyBuffer),\r
+               LO16((uint32)scsiTarget_datapath__F0_REG));\r
+       CyDmaTdSetAddress(\r
+               scsiDmaRxTd[0],\r
+               LO16((uint32)scsiTarget_datapath__F1_REG),\r
+               LO16((uint32)data)\r
+               );\r
+       \r
+       CyDmaChSetInitialTd(scsiDmaTxChan, scsiDmaTxTd[0]);\r
+       CyDmaChSetInitialTd(scsiDmaRxChan, scsiDmaRxTd[0]);\r
+       \r
+       // The DMA controller is a bit trigger-happy. It will retain\r
+       // a drq request that was triggered while the channel was\r
+       // disabled.\r
+       CyDmaClearPendingDrq(scsiDmaTxChan);\r
+       CyDmaClearPendingDrq(scsiDmaRxChan);\r
+\r
+       txDMAComplete = 0;\r
+       rxDMAComplete = 0;\r
+\r
+       CyDmaChEnable(scsiDmaRxChan, 1);\r
+       CyDmaChEnable(scsiDmaTxChan, 1);\r
+}\r
+\r
+void\r
+scsiReadDMA(uint8* data, uint32 count)\r
+{\r
+       dmaSentCount = 0;\r
+       dmaTotalCount = count;\r
+       dmaBuffer = data;\r
+\r
+       uint32_t singleCount = (count > MAX_DMA_BYTES) ? MAX_DMA_BYTES : count;\r
+       doRxSingleDMA(data, singleCount);\r
+       dmaSentCount += count;\r
+}\r
+\r
+int\r
+scsiReadDMAPoll()\r
+{\r
+       if (txDMAComplete && rxDMAComplete && (scsiPhyStatus() & SCSI_PHY_TX_COMPLETE))\r
+       {\r
+               if (dmaSentCount == dmaTotalCount)\r
+               {\r
+                       dmaInProgress = 0;\r
+                       while (SCSI_ReadPin(SCSI_In_ACK) && !scsiDev.resetFlag) {}\r
+                       return 1;\r
+               }\r
+               else\r
+               {\r
+                       // Transfer was too large for a single DMA transfer. Continue\r
+                       // to send remaining bytes.\r
+                       uint32_t count = dmaTotalCount - dmaSentCount;\r
+                       if (count > MAX_DMA_BYTES) count = MAX_DMA_BYTES;\r
+                       doRxSingleDMA(dmaBuffer + dmaSentCount, count);\r
+                       dmaSentCount += count;\r
+                       return 0;\r
+               }\r
+       }\r
+       else\r
+       {\r
+               return 0;\r
+       }\r
+}\r
+\r
+void\r
+scsiRead(uint8_t* data, uint32_t count)\r
+{\r
+       if (count < 8)\r
+       {\r
+               scsiReadPIO(data, count);\r
+       }\r
+       else\r
+       {\r
+               scsiReadDMA(data, count);\r
+               while (!scsiReadDMAPoll() && !scsiDev.resetFlag) {};\r
+       }\r
+}\r
+\r
+void\r
+scsiWriteByte(uint8 value)\r
+{\r
+       while (scsiPhyTxFifoFull() && !scsiDev.resetFlag) {}\r
+       scsiPhyTx(value);\r
+\r
+       while (!(scsiPhyStatus() & SCSI_PHY_TX_COMPLETE) && !scsiDev.resetFlag) {}\r
+       scsiPhyRxFifoClear();\r
+\r
        while (SCSI_ReadPin(SCSI_In_ACK) && !scsiDev.resetFlag) {}\r
 }\r
 \r
-void scsiWrite(uint8* data, uint32 count)\r
+static void\r
+scsiWritePIO(uint8_t* data, uint32_t count)\r
 {\r
-       int prep = 0;\r
        int i = 0;\r
 \r
        while (i < count && !scsiDev.resetFlag)\r
        {\r
-               if (prep < count && (CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 1))\r
+               if (!scsiPhyTxFifoFull())\r
                {\r
-                       CY_SET_REG8(scsiTarget_datapath__F0_REG, data[prep]);\r
-                       ++prep;\r
+                       scsiPhyTx(data[i]);\r
+                       ++i;\r
                }\r
-               if ((CY_GET_REG8(scsiTarget_StatusReg__STATUS_REG) & 2))\r
+       }\r
+\r
+       while (!(scsiPhyStatus() & SCSI_PHY_TX_COMPLETE) && !scsiDev.resetFlag) {}\r
+       scsiPhyRxFifoClear();\r
+}\r
+\r
+static void\r
+doTxSingleDMA(uint8* data, uint32 count)\r
+{\r
+       // Prepare DMA transfer\r
+       dmaInProgress = 1;\r
+\r
+       CyDmaTdSetConfiguration(\r
+               scsiDmaTxTd[0],\r
+               count,\r
+               CY_DMA_DISABLE_TD, // Disable the DMA channel when TD completes count bytes\r
+               TD_INC_SRC_ADR |\r
+                       SCSI_TX_DMA__TD_TERMOUT_EN // Trigger interrupt when complete\r
+               );\r
+       CyDmaTdSetAddress(\r
+               scsiDmaTxTd[0],\r
+               LO16((uint32)data),\r
+               LO16((uint32)scsiTarget_datapath__F0_REG));\r
+       CyDmaChSetInitialTd(scsiDmaTxChan, scsiDmaTxTd[0]);\r
+\r
+       // The DMA controller is a bit trigger-happy. It will retain\r
+       // a drq request that was triggered while the channel was\r
+       // disabled.\r
+       CyDmaClearPendingDrq(scsiDmaTxChan);\r
+\r
+       txDMAComplete = 0;\r
+\r
+       CyDmaChEnable(scsiDmaTxChan, 1);\r
+}\r
+\r
+void\r
+scsiWriteDMA(uint8* data, uint32 count)\r
+{\r
+       dmaSentCount = 0;\r
+       dmaTotalCount = count;\r
+       dmaBuffer = data;\r
+\r
+       uint32_t singleCount = (count > MAX_DMA_BYTES) ? MAX_DMA_BYTES : count;\r
+       doTxSingleDMA(data, singleCount);\r
+       dmaSentCount += count;\r
+}\r
+\r
+int\r
+scsiWriteDMAPoll()\r
+{\r
+       if (txDMAComplete && (scsiPhyStatus() & SCSI_PHY_TX_COMPLETE))\r
+       {\r
+               if (dmaSentCount == dmaTotalCount)\r
                {\r
-                       CY_GET_REG8(scsiTarget_datapath__F1_REG);\r
-                       ++i;\r
+                       scsiPhyRxFifoClear();\r
+                       dmaInProgress = 0;\r
+                       while (SCSI_ReadPin(SCSI_In_ACK) && !scsiDev.resetFlag) {}\r
+                       return 1;\r
+               }\r
+               else\r
+               {\r
+                       // Transfer was too large for a single DMA transfer. Continue\r
+                       // to send remaining bytes.\r
+                       uint32_t count = dmaTotalCount - dmaSentCount;\r
+                       if (count > MAX_DMA_BYTES) count = MAX_DMA_BYTES;\r
+                       doTxSingleDMA(dmaBuffer + dmaSentCount, count);\r
+                       dmaSentCount += count;\r
+                       return 0;\r
                }\r
        }\r
-       \r
-       while (SCSI_ReadPin(SCSI_In_ACK) && !scsiDev.resetFlag) {}\r
+       else\r
+       {\r
+               return 0;\r
+       }\r
+}\r
+\r
+void\r
+scsiWrite(uint8_t* data, uint32_t count)\r
+{\r
+       if (count < 8)\r
+       {\r
+               scsiWritePIO(data, count);\r
+       }\r
+       else\r
+       {\r
+               scsiWriteDMA(data, count);\r
+               while (!scsiWriteDMAPoll() && !scsiDev.resetFlag) {};\r
+       }\r
 }\r
 \r
 static void busSettleDelay(void)\r
@@ -133,6 +349,20 @@ void scsiEnterPhase(int phase)
 \r
 void scsiPhyReset()\r
 {\r
+       if (dmaInProgress)\r
+       {\r
+               dmaInProgress = 0;\r
+               dmaBuffer = NULL;\r
+               dmaSentCount = 0;\r
+               dmaTotalCount = 0;\r
+               CyDmaChSetRequest(scsiDmaTxChan, CY_DMA_CPU_TERM_CHAIN);\r
+               CyDmaChSetRequest(scsiDmaRxChan, CY_DMA_CPU_TERM_CHAIN);\r
+               while (!(txDMAComplete && rxDMAComplete)) {}\r
+\r
+               CyDmaChDisable(scsiDmaTxChan);\r
+               CyDmaChDisable(scsiDmaRxChan);\r
+       }\r
+\r
        // Set the Clear bits for both SCSI device FIFOs\r
        scsiTarget_AUX_CTL = scsiTarget_AUX_CTL | 0x03;\r
 \r
@@ -155,8 +385,43 @@ void scsiPhyReset()
        scsiTarget_AUX_CTL = scsiTarget_AUX_CTL & ~(0x03);\r
 }\r
 \r
+static void scsiPhyInitDMA()\r
+{\r
+       // One-time init only.\r
+       if (scsiDmaTxChan == CY_DMA_INVALID_CHANNEL)\r
+       {\r
+               scsiDmaRxChan =\r
+                       SCSI_RX_DMA_DmaInitialize(\r
+                               1, // Bytes per burst\r
+                               1, // request per burst\r
+                               HI16(CYDEV_PERIPH_BASE),\r
+                               HI16(CYDEV_SRAM_BASE)\r
+                               );\r
+                       \r
+               scsiDmaTxChan =\r
+                       SCSI_TX_DMA_DmaInitialize(\r
+                               1, // Bytes per burst\r
+                               1, // request per burst\r
+                               HI16(CYDEV_SRAM_BASE),\r
+                               HI16(CYDEV_PERIPH_BASE)\r
+                               );\r
+\r
+               CyDmaChDisable(scsiDmaRxChan);\r
+               CyDmaChDisable(scsiDmaTxChan);\r
+\r
+               scsiDmaRxTd[0] = CyDmaTdAllocate();\r
+               scsiDmaTxTd[0] = CyDmaTdAllocate();\r
+               \r
+               SCSI_RX_DMA_COMPLETE_StartEx(scsiRxCompleteISR);\r
+               SCSI_TX_DMA_COMPLETE_StartEx(scsiTxCompleteISR);\r
+       }\r
+}\r
+\r
+\r
 void scsiPhyInit()\r
 {\r
+       scsiPhyInitDMA();\r
+\r
        SCSI_RST_ISR_StartEx(scsiResetISR);\r
 \r
        // Interrupts may have already been directed to the (empty)\r