| 1 | /* | 
|---|
| 2 |  * soclib_dma.c - soclib Multi Channels DMA driver implementation | 
|---|
| 3 |  * | 
|---|
| 4 |  * Author     Alain Greiner (2017) | 
|---|
| 5 |  | 
|---|
| 6 |  * Copyright (c) UPMC Sorbonne Universites | 
|---|
| 7 |  * | 
|---|
| 8 |  * This file is part of ALMOS-MKH. | 
|---|
| 9 |  * | 
|---|
| 10 |  * ALMOS-MKH is free software; you can redistribute it and/or modify it | 
|---|
| 11 |  * under the terms of the GNU General Public License as published by | 
|---|
| 12 |  * the Free Software Foundation; version 2.0 of the License. | 
|---|
| 13 |  * | 
|---|
| 14 |  * ALMOS-MKH is distributed in the hope that it will be useful, but | 
|---|
| 15 |  * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 17 |  * General Public License for more details. | 
|---|
| 18 |  * | 
|---|
| 19 |  * You should have received a copy of the GNU General Public License | 
|---|
| 20 |  * along with ALMOS-MKH; if not, write to the Free Software Foundation, | 
|---|
| 21 |  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 22 |  */ | 
|---|
| 23 |  | 
|---|
| 24 | #include <hal_kernel_types.h> | 
|---|
| 25 | #include <chdev.h> | 
|---|
| 26 | #include <dev_dma.h> | 
|---|
| 27 | #include <thread.h> | 
|---|
| 28 | #include <soclib_dma.h> | 
|---|
| 29 |  | 
|---|
| 30 | /////////////////////////////////////// | 
|---|
| 31 | void soclib_dma_init( chdev_t * chdev ) | 
|---|
| 32 | { | 
|---|
| 33 |     // get hardware device cluster and local pointer | 
|---|
| 34 |     cxy_t      dma_cxy  = GET_CXY( chdev->base ); | 
|---|
| 35 |     uint32_t * dma_ptr  = (uint32_t *)GET_PTR( chdev->base ); | 
|---|
| 36 |  | 
|---|
| 37 |     // set driver specific fields in chdev descriptor | 
|---|
| 38 |     chdev->cmd = &soclib_dma_cmd; | 
|---|
| 39 |     chdev->isr = &soclib_dma_isr; | 
|---|
| 40 |  | 
|---|
| 41 |     // enable interrupts | 
|---|
| 42 |         hal_remote_sw( XPTR( dma_cxy , dma_ptr + DMA_IRQ_DISABLED ) , 0 ); | 
|---|
| 43 |  | 
|---|
| 44 | } // soclib_dma_init() | 
|---|
| 45 |  | 
|---|
| 46 | ////////////////////////////////////////////////////////////////// | 
|---|
| 47 | void __attribute__ ((noinline)) soclib_dma_cmd( xptr_t thread_xp ) | 
|---|
| 48 | { | 
|---|
| 49 |     xptr_t     dev_xp;       // extended pointer on DMA devive | 
|---|
| 50 |     xptr_t     dst_xp;       // extended pointer on destination buffer | 
|---|
| 51 |     xptr_t     src_xp;       // extended pointer on source buffer | 
|---|
| 52 |     uint32_t   size;         // buffer size | 
|---|
| 53 |  | 
|---|
| 54 |     // get client thread cluster and local pointer | 
|---|
| 55 |     cxy_t      thread_cxy = GET_CXY( thread_xp ); | 
|---|
| 56 |     thread_t * thread_ptr = (thread_t *)GET_PTR( thread_xp ); | 
|---|
| 57 |  | 
|---|
| 58 |     // get command arguments and extended pointer on DMA device | 
|---|
| 59 |     dev_xp = (xptr_t)hal_remote_lwd( XPTR( thread_cxy , &thread_ptr->dma_cmd.dev_xp ) ); | 
|---|
| 60 |     dst_xp = (xptr_t)hal_remote_lwd( XPTR( thread_cxy , &thread_ptr->dma_cmd.dst_xp ) ); | 
|---|
| 61 |     src_xp = (xptr_t)hal_remote_lwd( XPTR( thread_cxy , &thread_ptr->dma_cmd.src_xp ) ); | 
|---|
| 62 |     size   =         hal_remote_lw ( XPTR( thread_cxy , &thread_ptr->dma_cmd.size   ) ); | 
|---|
| 63 |  | 
|---|
| 64 |     // get DMA device cluster and local pointer | 
|---|
| 65 |     cxy_t     dev_cxy = GET_CXY( dev_xp ); | 
|---|
| 66 |     chdev_t * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); | 
|---|
| 67 |  | 
|---|
| 68 |     // get extended pointer on SOCLIB-DMA peripheral | 
|---|
| 69 |     xptr_t     dma_xp = hal_remote_lw( XPTR( dev_cxy , &dev_ptr->base ) ); | 
|---|
| 70 |  | 
|---|
| 71 |     // get SOCLIB_DMA device cluster and local pointer | 
|---|
| 72 |     cxy_t      dma_cxy = GET_CXY( dma_xp ); | 
|---|
| 73 |     uint32_t * dma_ptr = (uint32_t *)GET_PTR( dma_xp ); | 
|---|
| 74 |  | 
|---|
| 75 |     // get DMA channel index and channel base address | 
|---|
| 76 |     uint32_t * base = dma_ptr + DMA_SPAN * dev_ptr->channel; | 
|---|
| 77 |  | 
|---|
| 78 |     // split dst and src buffers addresses in two 32 bits words | 
|---|
| 79 |     uint32_t   dst_lsb = (uint32_t)(dst_xp); | 
|---|
| 80 |     uint32_t   dst_msb = (uint32_t)(dst_xp>>32); | 
|---|
| 81 |     uint32_t   src_lsb = (uint32_t)(src_xp); | 
|---|
| 82 |     uint32_t   src_msb = (uint32_t)(src_xp>>32); | 
|---|
| 83 |  | 
|---|
| 84 |     // set SOCLIB_DMA registers to start tranfer operation | 
|---|
| 85 |     hal_remote_sw( XPTR( dma_cxy , base + DMA_SRC     ) , src_lsb );  | 
|---|
| 86 |     hal_remote_sw( XPTR( dma_cxy , base + DMA_SRC_EXT ) , src_msb );  | 
|---|
| 87 |     hal_remote_sw( XPTR( dma_cxy , base + DMA_DST     ) , dst_lsb ); | 
|---|
| 88 |     hal_remote_sw( XPTR( dma_cxy , base + DMA_DST_EXT ) , dst_msb ); | 
|---|
| 89 |     hal_remote_sw( XPTR( dma_cxy , base + DMA_LEN     ) , size    ); | 
|---|
| 90 |  | 
|---|
| 91 |     // Block and deschedule server thread | 
|---|
| 92 |     thread_block( XPTR( local_cxy , CURRENT_THREAD ) , THREAD_BLOCKED_ISR ); | 
|---|
| 93 |     sched_yield("blocked on ISR"); | 
|---|
| 94 |      | 
|---|
| 95 | } // soclib_dma_cmd() | 
|---|
| 96 |  | 
|---|
| 97 | ///////////////////////////////////////////////////////////////// | 
|---|
| 98 | void __attribute__ ((noinline)) soclib_dma_isr( chdev_t * chdev ) | 
|---|
| 99 | { | 
|---|
| 100 |     // get extended pointer on client thread | 
|---|
| 101 |     xptr_t root      = XPTR( local_cxy , &chdev->wait_root ); | 
|---|
| 102 |     xptr_t client_xp = XLIST_FIRST_ELEMENT( root , thread_t , wait_list ); | 
|---|
| 103 |  | 
|---|
| 104 |     // get extended pointer on server thread | 
|---|
| 105 |     xptr_t server_xp = XPTR( local_cxy , &chdev->server ); | 
|---|
| 106 |  | 
|---|
| 107 |     // get client thread cluster and local pointer | 
|---|
| 108 |     cxy_t      client_cxy = GET_CXY( client_xp ); | 
|---|
| 109 |     thread_t * client_ptr = (thread_t *)GET_PTR( client_xp ); | 
|---|
| 110 |  | 
|---|
| 111 |     // get SOCLIB_DMA peripheral cluster and local pointer | 
|---|
| 112 |     cxy_t      dma_cxy  = GET_CXY( chdev->base ); | 
|---|
| 113 |     uint32_t * dma_ptr  = (uint32_t *)GET_PTR( chdev->base ); | 
|---|
| 114 |  | 
|---|
| 115 |     // get DMA channel base address | 
|---|
| 116 |     uint32_t * base = dma_ptr + (DMA_SPAN * chdev->channel); | 
|---|
| 117 |  | 
|---|
| 118 |     // get DMA status register  | 
|---|
| 119 |         uint32_t status = hal_remote_lw( XPTR( dma_cxy , base + DMA_LEN ) );    | 
|---|
| 120 |  | 
|---|
| 121 |     // acknowledge IRQ | 
|---|
| 122 |     hal_remote_sw( XPTR( dma_cxy , base + DMA_RESET ) , 0 ); | 
|---|
| 123 |  | 
|---|
| 124 |     // set operation status in command | 
|---|
| 125 |         error_t  error = ( status != DMA_SUCCESS );  | 
|---|
| 126 |     hal_remote_sw( XPTR( client_cxy , &client_ptr->dma_cmd.error ) , error ); | 
|---|
| 127 |  | 
|---|
| 128 |     // unblock server thread  | 
|---|
| 129 |     thread_unblock( server_xp , THREAD_BLOCKED_ISR ); | 
|---|
| 130 |  | 
|---|
| 131 |     // unblock client thread | 
|---|
| 132 |     thread_unblock( client_xp , THREAD_BLOCKED_IO ); | 
|---|
| 133 |  | 
|---|
| 134 | } // soclib_dma_isr() | 
|---|
| 135 |  | 
|---|
| 136 |  | 
|---|
| 137 |  | 
|---|