| Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 1 | ARM TCM (Tightly-Coupled Memory) handling in Linux | 
 | 2 | ---- | 
 | 3 | Written by Linus Walleij <linus.walleij@stericsson.com> | 
 | 4 |  | 
 | 5 | Some ARM SoC:s have a so-called TCM (Tightly-Coupled Memory). | 
 | 6 | This is usually just a few (4-64) KiB of RAM inside the ARM | 
 | 7 | processor. | 
 | 8 |  | 
 | 9 | Due to being embedded inside the CPU The TCM has a | 
 | 10 | Harvard-architecture, so there is an ITCM (instruction TCM) | 
 | 11 | and a DTCM (data TCM). The DTCM can not contain any | 
 | 12 | instructions, but the ITCM can actually contain data. | 
 | 13 | The size of DTCM or ITCM is minimum 4KiB so the typical | 
 | 14 | minimum configuration is 4KiB ITCM and 4KiB DTCM. | 
 | 15 |  | 
 | 16 | ARM CPU:s have special registers to read out status, physical | 
 | 17 | location and size of TCM memories. arch/arm/include/asm/cputype.h | 
 | 18 | defines a CPUID_TCM register that you can read out from the | 
 | 19 | system control coprocessor. Documentation from ARM can be found | 
 | 20 | at http://infocenter.arm.com, search for "TCM Status Register" | 
 | 21 | to see documents for all CPUs. Reading this register you can | 
| Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 22 | determine if ITCM (bits 1-0) and/or DTCM (bit 17-16) is present | 
 | 23 | in the machine. | 
| Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 24 |  | 
 | 25 | There is further a TCM region register (search for "TCM Region | 
 | 26 | Registers" at the ARM site) that can report and modify the location | 
 | 27 | size of TCM memories at runtime. This is used to read out and modify | 
 | 28 | TCM location and size. Notice that this is not a MMU table: you | 
 | 29 | actually move the physical location of the TCM around. At the | 
 | 30 | place you put it, it will mask any underlying RAM from the | 
 | 31 | CPU so it is usually wise not to overlap any physical RAM with | 
| Linus Walleij | 610ea6c | 2009-10-01 14:31:22 +0100 | [diff] [blame] | 32 | the TCM. | 
| Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 33 |  | 
| Linus Walleij | 610ea6c | 2009-10-01 14:31:22 +0100 | [diff] [blame] | 34 | The TCM memory can then be remapped to another address again using | 
 | 35 | the MMU, but notice that the TCM if often used in situations where | 
 | 36 | the MMU is turned off. To avoid confusion the current Linux | 
 | 37 | implementation will map the TCM 1 to 1 from physical to virtual | 
| Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 38 | memory in the location specified by the kernel. Currently Linux | 
 | 39 | will map ITCM to 0xfffe0000 and on, and DTCM to 0xfffe8000 and | 
 | 40 | on, supporting a maximum of 32KiB of ITCM and 32KiB of DTCM. | 
 | 41 |  | 
 | 42 | Newer versions of the region registers also support dividing these | 
 | 43 | TCMs in two separate banks, so for example an 8KiB ITCM is divided | 
 | 44 | into two 4KiB banks with its own control registers. The idea is to | 
 | 45 | be able to lock and hide one of the banks for use by the secure | 
 | 46 | world (TrustZone). | 
| Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 47 |  | 
 | 48 | TCM is used for a few things: | 
 | 49 |  | 
 | 50 | - FIQ and other interrupt handlers that need deterministic | 
 | 51 |   timing and cannot wait for cache misses. | 
 | 52 |  | 
 | 53 | - Idle loops where all external RAM is set to self-refresh | 
 | 54 |   retention mode, so only on-chip RAM is accessible by | 
 | 55 |   the CPU and then we hang inside ITCM waiting for an | 
 | 56 |   interrupt. | 
 | 57 |  | 
 | 58 | - Other operations which implies shutting off or reconfiguring | 
 | 59 |   the external RAM controller. | 
 | 60 |  | 
 | 61 | There is an interface for using TCM on the ARM architecture | 
 | 62 | in <asm/tcm.h>. Using this interface it is possible to: | 
 | 63 |  | 
 | 64 | - Define the physical address and size of ITCM and DTCM. | 
 | 65 |  | 
 | 66 | - Tag functions to be compiled into ITCM. | 
 | 67 |  | 
 | 68 | - Tag data and constants to be allocated to DTCM and ITCM. | 
 | 69 |  | 
 | 70 | - Have the remaining TCM RAM added to a special | 
 | 71 |   allocation pool with gen_pool_create() and gen_pool_add() | 
 | 72 |   and provice tcm_alloc() and tcm_free() for this | 
 | 73 |   memory. Such a heap is great for things like saving | 
 | 74 |   device state when shutting off device power domains. | 
 | 75 |  | 
| Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 76 | A machine that has TCM memory shall select HAVE_TCM from | 
 | 77 | arch/arm/Kconfig for itself. Code that needs to use TCM shall | 
 | 78 | #include <asm/tcm.h> | 
| Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 79 |  | 
 | 80 | Functions to go into itcm can be tagged like this: | 
 | 81 | int __tcmfunc foo(int bar); | 
 | 82 |  | 
| Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 83 | Since these are marked to become long_calls and you may want | 
 | 84 | to have functions called locally inside the TCM without | 
 | 85 | wasting space, there is also the __tcmlocalfunc prefix that | 
 | 86 | will make the call relative. | 
 | 87 |  | 
| Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 88 | Variables to go into dtcm can be tagged like this: | 
 | 89 | int __tcmdata foo; | 
 | 90 |  | 
 | 91 | Constants can be tagged like this: | 
 | 92 | int __tcmconst foo; | 
 | 93 |  | 
 | 94 | To put assembler into TCM just use | 
 | 95 | .section ".tcm.text" or .section ".tcm.data" | 
 | 96 | respectively. | 
 | 97 |  | 
 | 98 | Example code: | 
 | 99 |  | 
 | 100 | #include <asm/tcm.h> | 
 | 101 |  | 
 | 102 | /* Uninitialized data */ | 
 | 103 | static u32 __tcmdata tcmvar; | 
 | 104 | /* Initialized data */ | 
 | 105 | static u32 __tcmdata tcmassigned = 0x2BADBABEU; | 
 | 106 | /* Constant */ | 
 | 107 | static const u32 __tcmconst tcmconst = 0xCAFEBABEU; | 
 | 108 |  | 
 | 109 | static void __tcmlocalfunc tcm_to_tcm(void) | 
 | 110 | { | 
 | 111 | 	int i; | 
 | 112 | 	for (i = 0; i < 100; i++) | 
 | 113 | 		tcmvar ++; | 
 | 114 | } | 
 | 115 |  | 
 | 116 | static void __tcmfunc hello_tcm(void) | 
 | 117 | { | 
 | 118 | 	/* Some abstract code that runs in ITCM */ | 
 | 119 | 	int i; | 
 | 120 | 	for (i = 0; i < 100; i++) { | 
 | 121 | 		tcmvar ++; | 
 | 122 | 	} | 
 | 123 | 	tcm_to_tcm(); | 
 | 124 | } | 
 | 125 |  | 
 | 126 | static void __init test_tcm(void) | 
 | 127 | { | 
 | 128 | 	u32 *tcmem; | 
 | 129 | 	int i; | 
 | 130 |  | 
 | 131 | 	hello_tcm(); | 
 | 132 | 	printk("Hello TCM executed from ITCM RAM\n"); | 
 | 133 |  | 
 | 134 | 	printk("TCM variable from testrun: %u @ %p\n", tcmvar, &tcmvar); | 
 | 135 | 	tcmvar = 0xDEADBEEFU; | 
 | 136 | 	printk("TCM variable: 0x%x @ %p\n", tcmvar, &tcmvar); | 
 | 137 |  | 
 | 138 | 	printk("TCM assigned variable: 0x%x @ %p\n", tcmassigned, &tcmassigned); | 
 | 139 |  | 
 | 140 | 	printk("TCM constant: 0x%x @ %p\n", tcmconst, &tcmconst); | 
 | 141 |  | 
 | 142 | 	/* Allocate some TCM memory from the pool */ | 
 | 143 | 	tcmem = tcm_alloc(20); | 
 | 144 | 	if (tcmem) { | 
 | 145 | 		printk("TCM Allocated 20 bytes of TCM @ %p\n", tcmem); | 
 | 146 | 		tcmem[0] = 0xDEADBEEFU; | 
 | 147 | 		tcmem[1] = 0x2BADBABEU; | 
 | 148 | 		tcmem[2] = 0xCAFEBABEU; | 
 | 149 | 		tcmem[3] = 0xDEADBEEFU; | 
 | 150 | 		tcmem[4] = 0x2BADBABEU; | 
 | 151 | 		for (i = 0; i < 5; i++) | 
 | 152 | 			printk("TCM tcmem[%d] = %08x\n", i, tcmem[i]); | 
 | 153 | 		tcm_free(tcmem, 20); | 
 | 154 | 	} | 
 | 155 | } |