| 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 | 
 | 22 | determine if ITCM (bit 0) and/or DTCM (bit 16) is present in the | 
 | 23 | machine. | 
 | 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 | 
 | 38 | memory in the location specified by the machine. | 
| Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 39 |  | 
 | 40 | TCM is used for a few things: | 
 | 41 |  | 
 | 42 | - FIQ and other interrupt handlers that need deterministic | 
 | 43 |   timing and cannot wait for cache misses. | 
 | 44 |  | 
 | 45 | - Idle loops where all external RAM is set to self-refresh | 
 | 46 |   retention mode, so only on-chip RAM is accessible by | 
 | 47 |   the CPU and then we hang inside ITCM waiting for an | 
 | 48 |   interrupt. | 
 | 49 |  | 
 | 50 | - Other operations which implies shutting off or reconfiguring | 
 | 51 |   the external RAM controller. | 
 | 52 |  | 
 | 53 | There is an interface for using TCM on the ARM architecture | 
 | 54 | in <asm/tcm.h>. Using this interface it is possible to: | 
 | 55 |  | 
 | 56 | - Define the physical address and size of ITCM and DTCM. | 
 | 57 |  | 
 | 58 | - Tag functions to be compiled into ITCM. | 
 | 59 |  | 
 | 60 | - Tag data and constants to be allocated to DTCM and ITCM. | 
 | 61 |  | 
 | 62 | - Have the remaining TCM RAM added to a special | 
 | 63 |   allocation pool with gen_pool_create() and gen_pool_add() | 
 | 64 |   and provice tcm_alloc() and tcm_free() for this | 
 | 65 |   memory. Such a heap is great for things like saving | 
 | 66 |   device state when shutting off device power domains. | 
 | 67 |  | 
 | 68 | A machine that has TCM memory shall select HAVE_TCM in | 
 | 69 | arch/arm/Kconfig for itself, and then the | 
 | 70 | rest of the functionality will depend on the physical | 
 | 71 | location and size of ITCM and DTCM to be defined in | 
 | 72 | mach/memory.h for the machine. Code that needs to use | 
 | 73 | TCM shall #include <asm/tcm.h> If the TCM is not located | 
 | 74 | at the place given in memory.h it will be moved using | 
 | 75 | the TCM Region registers. | 
 | 76 |  | 
 | 77 | Functions to go into itcm can be tagged like this: | 
 | 78 | int __tcmfunc foo(int bar); | 
 | 79 |  | 
 | 80 | Variables to go into dtcm can be tagged like this: | 
 | 81 | int __tcmdata foo; | 
 | 82 |  | 
 | 83 | Constants can be tagged like this: | 
 | 84 | int __tcmconst foo; | 
 | 85 |  | 
 | 86 | To put assembler into TCM just use | 
 | 87 | .section ".tcm.text" or .section ".tcm.data" | 
 | 88 | respectively. | 
 | 89 |  | 
 | 90 | Example code: | 
 | 91 |  | 
 | 92 | #include <asm/tcm.h> | 
 | 93 |  | 
 | 94 | /* Uninitialized data */ | 
 | 95 | static u32 __tcmdata tcmvar; | 
 | 96 | /* Initialized data */ | 
 | 97 | static u32 __tcmdata tcmassigned = 0x2BADBABEU; | 
 | 98 | /* Constant */ | 
 | 99 | static const u32 __tcmconst tcmconst = 0xCAFEBABEU; | 
 | 100 |  | 
 | 101 | static void __tcmlocalfunc tcm_to_tcm(void) | 
 | 102 | { | 
 | 103 | 	int i; | 
 | 104 | 	for (i = 0; i < 100; i++) | 
 | 105 | 		tcmvar ++; | 
 | 106 | } | 
 | 107 |  | 
 | 108 | static void __tcmfunc hello_tcm(void) | 
 | 109 | { | 
 | 110 | 	/* Some abstract code that runs in ITCM */ | 
 | 111 | 	int i; | 
 | 112 | 	for (i = 0; i < 100; i++) { | 
 | 113 | 		tcmvar ++; | 
 | 114 | 	} | 
 | 115 | 	tcm_to_tcm(); | 
 | 116 | } | 
 | 117 |  | 
 | 118 | static void __init test_tcm(void) | 
 | 119 | { | 
 | 120 | 	u32 *tcmem; | 
 | 121 | 	int i; | 
 | 122 |  | 
 | 123 | 	hello_tcm(); | 
 | 124 | 	printk("Hello TCM executed from ITCM RAM\n"); | 
 | 125 |  | 
 | 126 | 	printk("TCM variable from testrun: %u @ %p\n", tcmvar, &tcmvar); | 
 | 127 | 	tcmvar = 0xDEADBEEFU; | 
 | 128 | 	printk("TCM variable: 0x%x @ %p\n", tcmvar, &tcmvar); | 
 | 129 |  | 
 | 130 | 	printk("TCM assigned variable: 0x%x @ %p\n", tcmassigned, &tcmassigned); | 
 | 131 |  | 
 | 132 | 	printk("TCM constant: 0x%x @ %p\n", tcmconst, &tcmconst); | 
 | 133 |  | 
 | 134 | 	/* Allocate some TCM memory from the pool */ | 
 | 135 | 	tcmem = tcm_alloc(20); | 
 | 136 | 	if (tcmem) { | 
 | 137 | 		printk("TCM Allocated 20 bytes of TCM @ %p\n", tcmem); | 
 | 138 | 		tcmem[0] = 0xDEADBEEFU; | 
 | 139 | 		tcmem[1] = 0x2BADBABEU; | 
 | 140 | 		tcmem[2] = 0xCAFEBABEU; | 
 | 141 | 		tcmem[3] = 0xDEADBEEFU; | 
 | 142 | 		tcmem[4] = 0x2BADBABEU; | 
 | 143 | 		for (i = 0; i < 5; i++) | 
 | 144 | 			printk("TCM tcmem[%d] = %08x\n", i, tcmem[i]); | 
 | 145 | 		tcm_free(tcmem, 20); | 
 | 146 | 	} | 
 | 147 | } |