LMS 2012
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pru.c
Go to the documentation of this file.
1 /*
2  * pru/hal/uart/src/pru.c
3  *
4  * Copyright (C) 2010 Texas Instruments Incorporated
5  * Author: Jitendra Kumar <jitendra@mistralsolutions.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation version 2.
10  *
11  * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
12  * whether express or implied; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  */
16 
17 /************************************************************
18 * Include Files *
19 ************************************************************/
20 #include "tistdtypes.h"
21 #include "csl/cslr.h"
22 #include "csl/soc_OMAPL138.h"
23 #include "csl/cslr_prucore.h"
24 #include "csl/cslr_psc_OMAPL138.h"
25 #include "pru.h"
26 
27 /************************************************************
28 * Local Function Declarations *
29 ************************************************************/
30 static void pru_psc_disable(arm_pru_iomap * pru_arm_iomap);
31 static void pru_psc_enable(arm_pru_iomap * pru_arm_iomap);
32 
33 // Load the specified PRU with code
34 Uint32 pru_load(Uint8 pruNum, Uint32 * pruCode, Uint32 codeSizeInWords,
35  arm_pru_iomap * pru_arm_iomap)
36 {
37  Uint32 *pruIram;
38  Uint32 i;
39 
40  if (pruNum == CSL_PRUCORE_0) {
41  pruIram = (Uint32 *) ((Uint32 )pru_arm_iomap->pru_io_addr + 0x8000);
42  } else if (pruNum == CSL_PRUCORE_1) {
43  pruIram = (Uint32 *) ((Uint32 )pru_arm_iomap->pru_io_addr + 0xc000);
44  } else {
45  return E_FAIL;
46  }
47 
48  pru_enable(pruNum, pru_arm_iomap);
49 
50  // Copy dMAX code to its instruction RAM
51  for (i = 0; i < codeSizeInWords; i++) {
52  pruIram[i] = pruCode[i];
53  }
54 
55  return E_PASS;
56 }
57 
58 Uint32 pru_run(Uint8 pruNum, arm_pru_iomap * pru_arm_iomap)
59 {
61 
62  if (pruNum == CSL_PRUCORE_0) {
63  hPru = (CSL_PrucoreRegsOvly) ((Uint32 )pru_arm_iomap->pru_io_addr + 0x7000); //CSL_PRUCORE_0_REGS;
64  } else if (pruNum == CSL_PRUCORE_1) {
65  hPru = (CSL_PrucoreRegsOvly) ((Uint32 )pru_arm_iomap->pru_io_addr + 0x7800); //CSL_PRUCORE_1_REGS;
66  } else {
67  return E_FAIL;
68  }
69 
70  // Enable dMAX, let it execute the code we just copied
71  CSL_FINST(hPru->CONTROL, PRUCORE_CONTROL_COUNTENABLE, ENABLE);
72  CSL_FINST(hPru->CONTROL, PRUCORE_CONTROL_ENABLE, ENABLE);
73  return E_PASS;
74 }
75 
76 Uint32 pru_waitForHalt(Uint8 pruNum, Int32 timeout,
77  arm_pru_iomap * pru_arm_iomap)
78 {
80 
81  Int32 cnt = timeout;
82 
83  if (pruNum == CSL_PRUCORE_0) {
84  hPru = (CSL_PrucoreRegsOvly) ((Uint32 )pru_arm_iomap->pru_io_addr + 0x7000); //CSL_PRUCORE_0_REGS;
85  } else if (pruNum == CSL_PRUCORE_1) {
86  hPru = (CSL_PrucoreRegsOvly) ((Uint32 )pru_arm_iomap->pru_io_addr + 0x7800); //CSL_PRUCORE_1_REGS;
87  } else {
88  return E_FAIL;
89  }
90 
91  while (CSL_FEXT(hPru->CONTROL, PRUCORE_CONTROL_RUNSTATE) ==
93  if (cnt > 0) {
94  cnt--;
95  }
96  if (cnt == 0) {
97  return E_TIMEOUT;
98  }
99  }
100 
101  return E_PASS;
102 }
103 
104 Uint32 pru_disable(arm_pru_iomap * pru_arm_iomap)
105 {
106  CSL_PrucoreRegsOvly hPru;
107  unsigned int delay_cnt;
108 
109  // Disable PRU0
110  hPru = (CSL_PrucoreRegsOvly) ((Uint32 )pru_arm_iomap->pru_io_addr + 0x7000); //CSL_PRUCORE_0_REGS;
111  CSL_FINST(hPru->CONTROL, PRUCORE_CONTROL_COUNTENABLE, DISABLE);
112  for (delay_cnt = 0x10000; delay_cnt > 0; delay_cnt--)
113  CSL_FINST(hPru->CONTROL, PRUCORE_CONTROL_ENABLE, DISABLE);
114 
115  for (delay_cnt = 0x10000; delay_cnt > 0; delay_cnt--)
116  // Reset PRU0
117  hPru->CONTROL = CSL_PRUCORE_CONTROL_RESETVAL;
118 
119  // Disable PRU1
120  hPru = (CSL_PrucoreRegsOvly) ((Uint32 )pru_arm_iomap->pru_io_addr + 0x7800); //CSL_PRUCORE_1_REGS;
121  CSL_FINST(hPru->CONTROL, PRUCORE_CONTROL_COUNTENABLE, DISABLE);
122 
123  for (delay_cnt = 0x10000; delay_cnt > 0; delay_cnt--)
124  CSL_FINST(hPru->CONTROL, PRUCORE_CONTROL_ENABLE, DISABLE);
125 
126  for (delay_cnt = 0x10000; delay_cnt > 0; delay_cnt--)
127  // Reset PRU1
128  hPru->CONTROL = CSL_PRUCORE_CONTROL_RESETVAL;
129 
130  return E_PASS;
131 }
132 
133 Uint32 pru_enable(Uint8 pruNum, arm_pru_iomap * pru_arm_iomap)
134 {
135  CSL_PrucoreRegsOvly hPru;
136 
137  // Enable PRU SS
138  pru_psc_enable(pru_arm_iomap);
139 
140  if (pruNum == CSL_PRUCORE_0) {
141  // Reset PRU0
142  hPru = (CSL_PrucoreRegsOvly) ((Uint32 )pru_arm_iomap->pru_io_addr + 0x7000); //CSL_PRUCORE_0_REGS;
143  hPru->CONTROL = CSL_PRUCORE_CONTROL_RESETVAL;
144  } else if (pruNum == CSL_PRUCORE_1) {
145  // Reset PRU1
146  hPru = (CSL_PrucoreRegsOvly) ((Uint32 )pru_arm_iomap->pru_io_addr + 0x7800); //CSL_PRUCORE_1_REGS;
147  hPru->CONTROL = CSL_PRUCORE_CONTROL_RESETVAL;
148  }
149  return E_PASS;
150 }
151 
152 /************************************************************
153 * Local Function Definitions *
154 ************************************************************/
155 
156 static void pru_psc_enable(arm_pru_iomap * pru_arm_iomap)
157 {
158  CSL_PscRegsOvly PSC = (CSL_PscRegsOvly) pru_arm_iomap->psc0_io_addr; //CSL_PSC_0_REGS;
159 
160  // Wait for any outstanding transition to complete
161  while (CSL_FEXT(PSC->PTSTAT, PSC_PTSTAT_GOSTAT0) ==
163 
164  // If we are already in that state, just return
165  if (CSL_FEXT(PSC->MDSTAT[CSL_PSC_PRU], PSC_MDSTAT_STATE) ==
167  return;
168 
169  // Perform transition
170  CSL_FINST(PSC->MDCTL[CSL_PSC_PRU], PSC_MDCTL_NEXT, ENABLE);
171  CSL_FINST(PSC->PTCMD, PSC_PTCMD_GO0, SET);
172 
173  // Wait for transition to complete
174  while (CSL_FEXT(PSC->PTSTAT, PSC_PTSTAT_GOSTAT0) ==
176 
177  // Wait and verify the state
178  while (CSL_FEXT(PSC->MDSTAT[CSL_PSC_PRU], PSC_MDSTAT_STATE) !=
180 }
181 
182 static void pru_psc_disable(arm_pru_iomap * pru_arm_iomap)
183 {
184  CSL_PscRegsOvly PSC = (CSL_PscRegsOvly) pru_arm_iomap->psc0_io_addr; //CSL_PSC_0_REGS;
185 
186  // Wait for any outstanding transition to complete
187  while (CSL_FEXT(PSC->PTSTAT, PSC_PTSTAT_GOSTAT0) ==
189 
190  // If we are already in that state, just return
191  if (CSL_FEXT(PSC->MDSTAT[CSL_PSC_PRU], PSC_MDSTAT_STATE) ==
193  return;
194 
195  // Perform transition
196  CSL_FINST(PSC->MDCTL[CSL_PSC_PRU], PSC_MDCTL_NEXT, SYNCRST);
197  CSL_FINST(PSC->PTCMD, PSC_PTCMD_GO0, SET);
198 
199  // Wait for transition to complete
200  while (CSL_FEXT(PSC->PTSTAT, PSC_PTSTAT_GOSTAT0) ==
202 
203  // Wait and verify the state
204  while (CSL_FEXT(PSC->MDSTAT[CSL_PSC_PRU], PSC_MDSTAT_STATE) !=
206 }
207 
208 /***********************************************************
209 * End file *
210 ***********************************************************/
211 
224 short pru_ram_write_data
225  (Uint32 u32offset,
226  Uint8 * pu8datatowrite,
227  Uint16 u16bytestowrite, arm_pru_iomap * pru_arm_iomap) {
228  Uint8 *pu8addresstowrite;
229  Uint16 u16loop;
230  u32offset = (unsigned int)pru_arm_iomap->pru_io_addr + u32offset;
231  pu8addresstowrite = (Uint8 *) (u32offset);
232 
233  for (u16loop = 0; u16loop < u16bytestowrite; u16loop++)
234  *pu8addresstowrite++ = *pu8datatowrite++;
235  return 0;
236 }
237 
250 short pru_ram_read_data
251  (Uint32 u32offset,
252  Uint8 * pu8datatoread,
253  Uint16 u16bytestoread, arm_pru_iomap * pru_arm_iomap) {
254 
255  Uint8 *pu8addresstoread;
256  Uint16 u16loop;
257  u32offset = (unsigned int)pru_arm_iomap->pru_io_addr + u32offset;
258  pu8addresstoread = (Uint8 *) (u32offset);
259 
260  for (u16loop = 0; u16loop < u16bytestoread; u16loop++)
261  *pu8datatoread++ = *pu8addresstoread++;
262 
263  return 0;
264 }
265 
278 short pru_ram_write_data_4byte(unsigned int u32offset,
279  unsigned int *pu32datatowrite,
280  short u16wordstowrite)
281 {
282 
283  unsigned int *pu32addresstowrite;
284  short u16loop;
285 
286  pu32addresstowrite = (unsigned int *)(u32offset);
287 
288  for (u16loop = 0; u16loop < u16wordstowrite; u16loop++)
289  *pu32addresstowrite++ = *pu32datatowrite++;
290 
291  return 0;
292 }
293 
306 short pru_ram_read_data_4byte(unsigned int u32offset,
307  unsigned int *pu32datatoread,
308  short u16wordstoread)
309 {
310  unsigned int *pu32addresstoread;
311  short u16loop;
312 
313  pu32addresstoread = (unsigned int *)(u32offset);
314 
315  for (u16loop = 0; u16loop < u16wordstoread; u16loop++)
316  *pu32datatoread++ = *pu32addresstoread++;
317 
318  return 0;
319 }
320 
#define CSL_FEXT(reg, PER_REG_FIELD)
Definition: c_i2c.c:95
#define CSL_PRUCORE_CONTROL_RESETVAL
#define E_PASS
Definition: tistdtypes.h:42
#define CSL_PSC_MDSTAT_STATE_SYNCRST
short pru_ram_read_data(Uint32 u32offset, Uint8 *pu8datatoread, Uint16 u16bytestoread, arm_pru_iomap *pru_arm_iomap)
pru_ram_read_data() Download the data into data RAM of PRU0 or PRU1 of OMAP L138. ...
Definition: pru.c:251
#define E_TIMEOUT
Definition: tistdtypes.h:48
#define CSL_PSC_PTSTAT_GOSTAT0_IN_TRANSITION
void * psc0_io_addr
Definition: pru.h:113
Uint32 pru_run(Uint8 pruNum, arm_pru_iomap *pru_arm_iomap)
Definition: pru.c:58
Uint32 pru_enable(Uint8 pruNum, arm_pru_iomap *pru_arm_iomap)
Definition: pru.c:133
#define CSL_PRUCORE_1
Definition: soc_OMAPL138.h:205
#define CSL_PRUCORE_0
Peripheral Instance of PRU CORE instances.
Definition: soc_OMAPL138.h:204
short pru_ram_read_data_4byte(unsigned int u32offset, unsigned int *pu32datatoread, short u16wordstoread)
pru_ram_read_data_4byte() Download the data into data RAM of PRU0 or PRU1 of OMAP L138...
Definition: pru.c:306
Uint32 pru_load(Uint8 pruNum, Uint32 *pruCode, Uint32 codeSizeInWords, arm_pru_iomap *pru_arm_iomap)
Definition: pru.c:34
volatile CSL_PscRegs * CSL_PscRegsOvly
Uint32 pru_disable(arm_pru_iomap *pru_arm_iomap)
Definition: pru.c:104
#define CSL_FINST(reg, PER_REG_FIELD, TOKEN)
Uint32 pru_waitForHalt(Uint8 pruNum, Int32 timeout, arm_pru_iomap *pru_arm_iomap)
Definition: pru.c:76
#define E_FAIL
Definition: tistdtypes.h:45
void * pru_io_addr
Definition: pru.h:111
#define CSL_PRUCORE_CONTROL_RUNSTATE_RUN
short pru_ram_write_data(Uint32 u32offset, Uint8 *pu8datatowrite, Uint16 u16bytestowrite, arm_pru_iomap *pru_arm_iomap)
pru_ram_write_data() Download the data into data RAM of PRU0 or PRU1 of OMAP L138.
Definition: pru.c:225
volatile CSL_PrucoreRegs * CSL_PrucoreRegsOvly
Definition: cslr_prucore.h:106
#define CSL_PSC_MDSTAT_STATE_ENABLE
short pru_ram_write_data_4byte(unsigned int u32offset, unsigned int *pu32datatowrite, short u16wordstowrite)
pru_ram_write_data_4byte() Download the data into data RAM of PRU0 or PRU1 of OMAP L138...
Definition: pru.c:278