Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:03

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSBSPsARMLPC176X_dma
0007  *
0008  * @brief Direct memory access (DMA) support.
0009  */
0010 
0011 /*
0012  * Copyright (C) 2008, 2011 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #include <rtems/endian.h>
0037 #include <bsp/dma.h>
0038 #include <bsp/io.h>
0039 
0040 /**
0041  * @brief Table that indicates if a channel is currently occupied.
0042  */
0043 static bool lpc176x_dma_channel_occupation[ GPDMA_CH_NUMBER ];
0044 
0045 void lpc176x_dma_initialize( void )
0046 {
0047   /* Enable module power */
0048   lpc176x_module_enable( LPC176X_MODULE_GPDMA, LPC176X_MODULE_PCLK_DEFAULT );
0049 
0050   /* Disable module */
0051   GPDMA_CONFIG = 0u;
0052 
0053   /* Reset registers */
0054   GPDMA_SOFT_SREQ = 0u;
0055   GPDMA_SOFT_BREQ = 0u;
0056   GPDMA_SOFT_LSREQ = 0u;
0057   GPDMA_SOFT_LBREQ = 0u;
0058   GPDMA_SYNC = 0u;
0059   GPDMA_CH0_CFG = 0u;
0060   GPDMA_CH1_CFG = 0u;
0061 
0062   /* Enable module */
0063 #if BYTE_ORDER == LITTLE_ENDIAN
0064   GPDMA_CONFIG = GPDMA_CONFIG_EN;
0065 #else
0066   GPDMA_CONFIG = GPDMA_CONFIG_EN | GPDMA_CONFIG_MODE;
0067 #endif
0068 }
0069 
0070 rtems_status_code lpc176x_dma_channel_obtain( const unsigned channel )
0071 {
0072   rtems_status_code status_code = RTEMS_INVALID_ID;
0073 
0074   if ( channel < GPDMA_CH_NUMBER ) {
0075     rtems_interrupt_level level = 0u;
0076     bool                  occupation = true;
0077 
0078     rtems_interrupt_disable( level );
0079     occupation = lpc176x_dma_channel_occupation[ channel ];
0080     lpc176x_dma_channel_occupation[ channel ] = true;
0081     rtems_interrupt_enable( level );
0082 
0083     status_code = occupation ? RTEMS_RESOURCE_IN_USE : RTEMS_SUCCESSFUL;
0084   }
0085 
0086   /* else implies that the channel is not valid. Also,
0087      there is nothing to do. */
0088 
0089   return status_code;
0090 }
0091 
0092 void lpc176x_dma_channel_release( const unsigned channel )
0093 {
0094   if ( channel < GPDMA_CH_NUMBER ) {
0095     lpc176x_dma_channel_occupation[ channel ] = false;
0096   }
0097 
0098   /* else implies that the channel is not valid. Also,
0099      there is nothing to do. */
0100 }
0101 
0102 void lpc176x_dma_channel_disable(
0103   const unsigned channel,
0104   const bool     force
0105 )
0106 {
0107   if ( channel < GPDMA_CH_NUMBER ) {
0108     volatile lpc176x_dma_channel *ch = GPDMA_CH_BASE_ADDR( channel );
0109     uint32_t                      cfg = ch->cfg;
0110 
0111     if ( !force ) {
0112       /* Halt */
0113       ch->cfg |= GPDMA_CH_CFG_HALT;
0114 
0115       /* Wait for inactive */
0116       do {
0117         cfg = ch->cfg;
0118       } while ( ( cfg & GPDMA_CH_CFG_ACTIVE ) != 0u );
0119     }
0120 
0121     /* else implies that the channel is not to be forced. Also,
0122        there is nothing to do. */
0123 
0124     /* Disable */
0125     ch->cfg &= ~GPDMA_CH_CFG_EN;
0126   }
0127 
0128   /* else implies that the channel is not valid. Also,
0129      there is nothing to do. */
0130 }