123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- /*
- * Copyright (c) 2012, Freescale Semiconductor, Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- *
- * o Redistributions of source code must retain the above copyright notice, this list
- * of conditions and the following disclaimer.
- *
- * o Redistributions in binary form must reproduce the above copyright notice, this
- * list of conditions and the following disclaimer in the documentation and/or
- * other materials provided with the distribution.
- *
- * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "cortex_a.h"
- #include "arm_cp_registers.h"
- ////////////////////////////////////////////////////////////////////////////////
- // Code
- ////////////////////////////////////////////////////////////////////////////////
- //! @brief Check if dcache is enabled or disabled
- int arm_dcache_state_query()
- {
- uint32_t sctlr; // System Control Register
-
- // read sctlr
- _ARM_MRC(15, 0, sctlr, 1, 0, 0);
-
- if (sctlr & BM_SCTLR_C)
- {
- return 1;
- }
- return 0;
- }
- void arm_dcache_enable()
- {
- uint32_t sctlr; // System Control Register
-
- // read sctlr
- _ARM_MRC(15, 0, sctlr, 1, 0, 0);
-
- if (!(sctlr & BM_SCTLR_C))
- {
- // set C bit (data caching enable)
- sctlr |= BM_SCTLR_C;
-
- // write modified sctlr
- _ARM_MCR(15, 0, sctlr, 1, 0, 0);
-
- // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
- _ARM_DSB();
- }
- }
- void arm_dcache_disable()
- {
- uint32_t sctlr; // System Control Register
-
- // read sctlr
- _ARM_MRC(15, 0, sctlr, 1, 0, 0);
-
- // set C bit (data caching enable)
- sctlr &= ~BM_SCTLR_C;
- // write modified sctlr
- _ARM_MCR(15, 0, sctlr, 1, 0, 0);
-
- // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
- _ARM_DSB();
- }
- void arm_dcache_invalidate()
- {
- uint32_t csid; // Cache Size ID
- uint32_t wayset; // wayset parameter
- int num_sets; // number of sets
- int num_ways; // number of ways
- _ARM_MRC(15, 1, csid, 0, 0, 0); // Read Cache Size ID
-
- // Fill number of sets and number of ways from csid register This walues are decremented by 1
- num_ways = (csid >> 0x03) & 0x3FFu; //((csid& csid_ASSOCIATIVITY_MASK) >> csid_ASSOCIATIVITY_SHIFT)
-
- // Invalidation all lines (all Sets in all ways)
- while (num_ways >= 0)
- {
- num_sets = (csid >> 0x0D) & 0x7FFFu; //((csid & csid_NUMSETS_MASK) >> csid_NUMSETS_SHIFT)
- while (num_sets >= 0 )
- {
- wayset = (num_sets << 5u) | (num_ways << 30u); //(num_sets << SETWAY_SET_SHIFT) | (num_sets << 3SETWAY_WAY_SHIFT)
- // invalidate line if we know set and way
- _ARM_MCR(15, 0, wayset, 7, 6, 2);
- num_sets--;
- }
- num_ways--;
- }
-
- // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
- _ARM_DSB();
- }
- void arm_dcache_invalidate_line(const void * addr)
- {
- uint32_t csidr = 0, line_size = 0;
- uint32_t va;
-
- // get the cache line size
- _ARM_MRC(15, 1, csidr, 0, 0, 0);
- line_size = 1 << ((csidr & 0x7) + 4);
- va = (uint32_t) addr & (~(line_size - 1)); //addr & va_VIRTUAL_ADDRESS_MASK
- // Invalidate data cache line by va to PoC (Point of Coherency).
- _ARM_MCR(15, 0, va, 7, 6, 1);
-
- // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
- _ARM_DSB();
- }
- void arm_dcache_invalidate_mlines(const void * addr, size_t length)
- {
- uint32_t va;
- uint32_t csidr = 0, line_size = 0;
-
- // get the cache line size
- _ARM_MRC(15, 1, csidr, 0, 0, 0);
- line_size = 1 << ((csidr & 0x7) + 4);
- // align the address with line
- const void * end_addr = (const void *)((uint32_t)addr + length);
-
- do
- {
- // Clean data cache line to PoC (Point of Coherence) by va.
- va = (uint32_t) ((uint32_t)addr & (~(line_size - 1))); //addr & va_VIRTUAL_ADDRESS_MASK
- _ARM_MCR(15, 0, va, 7, 6, 1);
- // increment addres to next line and decrement lenght
- addr = (const void *) ((uint32_t)addr + line_size);
- } while (addr < end_addr);
-
- // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
- _ARM_DSB();
- }
- void arm_dcache_flush()
- {
- uint32_t csid; // Cache Size ID
- uint32_t wayset; // wayset parameter
- int num_sets; // number of sets
- int num_ways; // number of ways
- _ARM_MRC(15, 1, csid, 0, 0, 0); // Read Cache Size ID
-
- // Fill number of sets and number of ways from csid register This walues are decremented by 1
- num_ways = (csid >> 0x03) & 0x3FFu; //((csid& csid_ASSOCIATIVITY_MASK) >> csid_ASSOCIATIVITY_SHIFT`)
- while (num_ways >= 0)
- {
- num_sets = (csid >> 0x0D) & 0x7FFFu; //((csid & csid_NUMSETS_MASK) >> csid_NUMSETS_SHIFT )
- while (num_sets >= 0 )
- {
- wayset = (num_sets << 5u) | (num_ways << 30u); //(num_sets << SETWAY_SET_SHIFT) | (num_ways << 3SETWAY_WAY_SHIFT)
- // FLUSH (clean) line if we know set and way
- _ARM_MCR(15, 0, wayset, 7, 10, 2);
- num_sets--;
- }
- num_ways--;
- }
-
- // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
- _ARM_DSB();
- }
- void arm_dcache_flush_line(const void * addr)
- {
- uint32_t csidr = 0, line_size = 0;
- uint32_t va;
-
- // get the cache line size
- _ARM_MRC(15, 1, csidr, 0, 0, 0);
- line_size = 1 << ((csidr & 0x7) + 4);
- va = (uint32_t) addr & (~(line_size - 1)); //addr & va_VIRTUAL_ADDRESS_MASK
-
- // Clean data cache line to PoC (Point of Coherence) by va.
- _ARM_MCR(15, 0, va, 7, 10, 1);
-
- // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
- _ARM_DSB();
- }
- void arm_dcache_flush_mlines(const void * addr, size_t length)
- {
- uint32_t va;
- uint32_t csidr = 0, line_size = 0;
- const void * end_addr = (const void *)((uint32_t)addr + length);
- // get the cache line size
- _ARM_MRC(15, 1, csidr, 0, 0, 0);
- line_size = 1 << ((csidr & 0x7) + 4);
-
- do
- {
- // Clean data cache line to PoC (Point of Coherence) by va.
- va = (uint32_t) ((uint32_t)addr & (~(line_size - 1))); //addr & va_VIRTUAL_ADDRESS_MASK
- _ARM_MCR(15, 0, va, 7, 10, 1);
-
- // increment addres to next line and decrement lenght
- addr = (const void *) ((uint32_t)addr + line_size);
- } while (addr < end_addr);
-
- // All Cache, Branch predictor and TLB maintenance operations before followed instruction complete
- _ARM_DSB();
- }
- int arm_icache_state_query()
- {
- uint32_t sctlr; // System Control Register
-
- // read sctlr
- _ARM_MRC(15, 0, sctlr, 1, 0, 0);
-
- if (sctlr & BM_SCTLR_I)
- {
- return 1;
- }
- return 0;
- }
- void arm_icache_enable()
- {
- uint32_t sctlr ;// System Control Register
-
- // read sctlr
- _ARM_MRC(15, 0, sctlr, 1, 0, 0);
-
- // ignore the operation if I is enabled already
- if(!(sctlr & BM_SCTLR_I))
- {
- // set I bit (instruction caching enable)
- sctlr |= BM_SCTLR_I;
-
- // write modified sctlr
- _ARM_MCR(15, 0, sctlr, 1, 0, 0);
-
- // synchronize context on this processor
- _ARM_ISB();
- }
- }
- void arm_icache_disable()
- {
- uint32_t sctlr ;// System Control Register
-
- // read sctlr
- _ARM_MRC(15, 0, sctlr, 1, 0, 0);
-
- // Clear I bit (instruction caching enable)
- sctlr &= ~BM_SCTLR_I;
- // write modified sctlr
- _ARM_MCR(15, 0, sctlr, 1, 0, 0);
-
- // synchronize context on this processor
- _ARM_ISB();
- }
- void arm_icache_invalidate()
- {
- uint32_t SBZ = 0x0u;
-
- _ARM_MCR(15, 0, SBZ, 7, 5, 0);
-
- // synchronize context on this processor
- _ARM_ISB();
- }
- void arm_icache_invalidate_is()
- {
- uint32_t SBZ = 0x0u;
-
- _ARM_MCR(15, 0, SBZ, 7, 1, 0);
-
- // synchronize context on this processor
- _ARM_ISB();
- }
- void arm_icache_invalidate_line(const void * addr)
- {
- uint32_t csidr = 0, line_size = 0;
- uint32_t va;
-
- // get the cache line size
- _ARM_MRC(15, 1, csidr, 0, 0, 0);
- line_size = 1 << ((csidr & 0x7) + 4);
- va = (uint32_t) addr & (~(line_size - 1)); //addr & va_VIRTUAL_ADDRESS_MASK
-
- // Invalidate instruction cache by va to PoU (Point of unification).
- _ARM_MCR(15, 0, va, 7, 5, 1);
-
- // synchronize context on this processor
- _ARM_ISB();
- }
- void arm_icache_invalidate_mlines(const void * addr, size_t length)
- {
- uint32_t va;
- uint32_t csidr = 0, line_size = 0;
- const void * end_addr = (const void *)((uint32_t)addr + length);
- // get the cache line size
- _ARM_MRC(15, 1, csidr, 0, 0, 0);
- line_size = 1 << ((csidr & 0x7) + 4);
-
- do
- {
- // Clean data cache line to PoC (Point of Coherence) by va.
- va = (uint32_t) ((uint32_t)addr & (~(line_size - 1))); //addr & va_VIRTUAL_ADDRESS_MASK
- _ARM_MCR(15, 0, va, 7, 5, 1);
- // increment addres to next line and decrement lenght
- addr = (const void *) ((uint32_t)addr + line_size);
- } while (addr < end_addr);
-
- // synchronize context on this processor
- _ARM_ISB();
- }
- ////////////////////////////////////////////////////////////////////////////////
- // EOF
- ////////////////////////////////////////////////////////////////////////////////
|