mem_to_mem_idma2intr.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* The dpalloc function used and implemented in this file was derieved
  2. * from PPCBoot/U-Boot file "arch/powerpc/cpu/mpc8260/commproc.c".
  3. */
  4. /* Author: Arun Dharankar <ADharankar@ATTBI.Com>
  5. * This example is meant to only demonstrate how the IDMA could be used.
  6. */
  7. /*
  8. * This file is based on "arch/powerpc/8260_io/commproc.c" - here is it's
  9. * copyright notice:
  10. *
  11. * General Purpose functions for the global management of the
  12. * 8260 Communication Processor Module.
  13. * Copyright (c) 1999 Dan Malek (dmalek@jlc.net)
  14. * Copyright (c) 2000 MontaVista Software, Inc (source@mvista.com)
  15. * 2.3.99 Updates
  16. *
  17. * In addition to the individual control of the communication
  18. * channels, there are a few functions that globally affect the
  19. * communication processor.
  20. *
  21. * Buffer descriptors must be allocated from the dual ported memory
  22. * space. The allocator for that is here. When the communication
  23. * process is reset, we reclaim the memory available. There is
  24. * currently no deallocator for this memory.
  25. */
  26. #include <common.h>
  27. #include <console.h>
  28. #include <exports.h>
  29. DECLARE_GLOBAL_DATA_PTR;
  30. #define STANDALONE
  31. #ifndef STANDALONE /* Linked into/Part of PPCBoot */
  32. #include <command.h>
  33. #include <watchdog.h>
  34. #else /* Standalone app of PPCBoot */
  35. #define WATCHDOG_RESET() { \
  36. *(ushort *)(CONFIG_SYS_IMMR + 0x1000E) = 0x556c; \
  37. *(ushort *)(CONFIG_SYS_IMMR + 0x1000E) = 0xaa39; \
  38. }
  39. #endif /* STANDALONE */
  40. static int debug = 1;
  41. #define DEBUG(fmt, args...) { \
  42. if(debug != 0) { \
  43. printf("[%s %d %s]: ",__FILE__,__LINE__,__FUNCTION__); \
  44. printf(fmt, ##args); \
  45. } \
  46. }
  47. #define CPM_CR_IDMA1_SBLOCK (0x14)
  48. #define CPM_CR_IDMA2_SBLOCK (0x15)
  49. #define CPM_CR_IDMA3_SBLOCK (0x16)
  50. #define CPM_CR_IDMA4_SBLOCK (0x17)
  51. #define CPM_CR_IDMA1_PAGE (0x07)
  52. #define CPM_CR_IDMA2_PAGE (0x08)
  53. #define CPM_CR_IDMA3_PAGE (0x09)
  54. #define CPM_CR_IDMA4_PAGE (0x0a)
  55. #define PROFF_IDMA1_BASE ((uint)0x87fe)
  56. #define PROFF_IDMA2_BASE ((uint)0x88fe)
  57. #define PROFF_IDMA3_BASE ((uint)0x89fe)
  58. #define PROFF_IDMA4_BASE ((uint)0x8afe)
  59. #define CPM_CR_INIT_TRX ((ushort)0x0000)
  60. #define CPM_CR_FLG ((ushort)0x0001)
  61. #define mk_cr_cmd(PG, SBC, MCN, OP) \
  62. ((PG << 26) | (SBC << 21) | (MCN << 6) | OP)
  63. #pragma pack(1)
  64. typedef struct ibdbits {
  65. unsigned b_valid:1;
  66. unsigned b_resv1:1;
  67. unsigned b_wrap:1;
  68. unsigned b_interrupt:1;
  69. unsigned b_last:1;
  70. unsigned b_resv2:1;
  71. unsigned b_cm:1;
  72. unsigned b_resv3:2;
  73. unsigned b_sdn:1;
  74. unsigned b_ddn:1;
  75. unsigned b_dgbl:1;
  76. unsigned b_dbo:2;
  77. unsigned b_resv4:1;
  78. unsigned b_ddtb:1;
  79. unsigned b_resv5:2;
  80. unsigned b_sgbl:1;
  81. unsigned b_sbo:2;
  82. unsigned b_resv6:1;
  83. unsigned b_sdtb:1;
  84. unsigned b_resv7:9;
  85. } ibdbits_t;
  86. #pragma pack(1)
  87. typedef union ibdbitsu {
  88. ibdbits_t b;
  89. uint i;
  90. } ibdbitsu_t;
  91. #pragma pack(1)
  92. typedef struct idma_buf_desc {
  93. ibdbitsu_t ibd_bits; /* Status and Control */
  94. uint ibd_datlen; /* Data length in buffer */
  95. uint ibd_sbuf; /* Source buffer addr in host mem */
  96. uint ibd_dbuf; /* Destination buffer addr in host mem */
  97. } ibd_t;
  98. #pragma pack(1)
  99. typedef struct dcmbits {
  100. unsigned b_fb:1;
  101. unsigned b_lp:1;
  102. unsigned b_resv1:3;
  103. unsigned b_tc2:1;
  104. unsigned b_resv2:1;
  105. unsigned b_wrap:3;
  106. unsigned b_sinc:1;
  107. unsigned b_dinc:1;
  108. unsigned b_erm:1;
  109. unsigned b_dt:1;
  110. unsigned b_sd:2;
  111. } dcmbits_t;
  112. #pragma pack(1)
  113. typedef union dcmbitsu {
  114. dcmbits_t b;
  115. ushort i;
  116. } dcmbitsu_t;
  117. #pragma pack(1)
  118. typedef struct pram_idma {
  119. ushort pi_ibase;
  120. dcmbitsu_t pi_dcmbits;
  121. ushort pi_ibdptr;
  122. ushort pi_dprbuf;
  123. ushort pi_bufinv; /* internal to CPM */
  124. ushort pi_ssmax;
  125. ushort pi_dprinptr; /* internal to CPM */
  126. ushort pi_sts;
  127. ushort pi_dproutptr; /* internal to CPM */
  128. ushort pi_seob;
  129. ushort pi_deob;
  130. ushort pi_dts;
  131. ushort pi_retadd;
  132. ushort pi_resv1; /* internal to CPM */
  133. uint pi_bdcnt;
  134. uint pi_sptr;
  135. uint pi_dptr;
  136. uint pi_istate;
  137. } pram_idma_t;
  138. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  139. volatile ibd_t *bdf;
  140. volatile pram_idma_t *piptr;
  141. volatile int dmadone;
  142. volatile int *dmadonep = &dmadone;
  143. void dmadone_handler (void *);
  144. int idma_init (void);
  145. void idma_start (int, int, int, uint, uint, int);
  146. uint dpalloc (uint, uint);
  147. uint dpinit_done = 0;
  148. #ifdef STANDALONE
  149. int ctrlc (void)
  150. {
  151. if (tstc()) {
  152. switch (getc ()) {
  153. case 0x03: /* ^C - Control C */
  154. return 1;
  155. default:
  156. break;
  157. }
  158. }
  159. return 0;
  160. }
  161. int memcmp(const void * cs,const void * ct,size_t count)
  162. {
  163. const unsigned char *su1, *su2;
  164. int res = 0;
  165. for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  166. if ((res = *su1 - *su2) != 0)
  167. break;
  168. return res;
  169. }
  170. #endif /* STANDALONE */
  171. #ifdef STANDALONE
  172. int mem_to_mem_idma2intr (int argc, char * const argv[])
  173. #else
  174. int do_idma (bd_t * bd, int argc, char * const argv[])
  175. #endif /* STANDALONE */
  176. {
  177. int i;
  178. app_startup(argv);
  179. dpinit_done = 0;
  180. idma_init ();
  181. DEBUG ("Installing dma handler\n");
  182. install_hdlr (7, dmadone_handler, (void *) bdf);
  183. memset ((void *) 0x100000, 'a', 512);
  184. memset ((void *) 0x200000, 'b', 512);
  185. for (i = 0; i < 32; i++) {
  186. printf ("Startin IDMA, iteration=%d\n", i);
  187. idma_start (1, 1, 512, 0x100000, 0x200000, 3);
  188. }
  189. DEBUG ("Uninstalling dma handler\n");
  190. free_hdlr (7);
  191. return 0;
  192. }
  193. void
  194. idma_start (int sinc, int dinc, int sz, uint sbuf, uint dbuf, int ttype)
  195. {
  196. /* ttype is for M-M, M-P, P-M or P-P: not used for now */
  197. piptr->pi_istate = 0; /* manual says: clear it before every START_IDMA */
  198. piptr->pi_dcmbits.b.b_resv1 = 0;
  199. if (sinc == 1)
  200. piptr->pi_dcmbits.b.b_sinc = 1;
  201. else
  202. piptr->pi_dcmbits.b.b_sinc = 0;
  203. if (dinc == 1)
  204. piptr->pi_dcmbits.b.b_dinc = 1;
  205. else
  206. piptr->pi_dcmbits.b.b_dinc = 0;
  207. piptr->pi_dcmbits.b.b_erm = 0;
  208. piptr->pi_dcmbits.b.b_sd = 0x00; /* M-M */
  209. bdf->ibd_sbuf = sbuf;
  210. bdf->ibd_dbuf = dbuf;
  211. bdf->ibd_bits.b.b_cm = 0;
  212. bdf->ibd_bits.b.b_interrupt = 1;
  213. bdf->ibd_bits.b.b_wrap = 1;
  214. bdf->ibd_bits.b.b_last = 1;
  215. bdf->ibd_bits.b.b_sdn = 0;
  216. bdf->ibd_bits.b.b_ddn = 0;
  217. bdf->ibd_bits.b.b_dgbl = 0;
  218. bdf->ibd_bits.b.b_ddtb = 0;
  219. bdf->ibd_bits.b.b_sgbl = 0;
  220. bdf->ibd_bits.b.b_sdtb = 0;
  221. bdf->ibd_bits.b.b_dbo = 1;
  222. bdf->ibd_bits.b.b_sbo = 1;
  223. bdf->ibd_bits.b.b_valid = 1;
  224. bdf->ibd_datlen = 512;
  225. *dmadonep = 0;
  226. immap->im_sdma.sdma_idmr2 = (uchar) 0xf;
  227. immap->im_cpm.cp_cpcr = mk_cr_cmd (CPM_CR_IDMA2_PAGE,
  228. CPM_CR_IDMA2_SBLOCK, 0x0,
  229. 0x9) | 0x00010000;
  230. while (*dmadonep != 1) {
  231. if (ctrlc ()) {
  232. DEBUG ("\nInterrupted waiting for DMA interrupt.\n");
  233. goto done;
  234. }
  235. printf ("Waiting for DMA interrupt (dmadone=%d b_valid = %d)...\n",
  236. dmadone, bdf->ibd_bits.b.b_valid);
  237. udelay (1000000);
  238. }
  239. printf ("DMA complete notification received!\n");
  240. done:
  241. DEBUG ("memcmp(0x%08x, 0x%08x, 512) = %d\n",
  242. sbuf, dbuf, memcmp ((void *) sbuf, (void *) dbuf, 512));
  243. return;
  244. }
  245. #define MAX_INT_BUFSZ 64
  246. #define DCM_WRAP 0 /* MUST be consistant with MAX_INT_BUFSZ */
  247. int idma_init (void)
  248. {
  249. uint memaddr;
  250. immap->im_cpm.cp_rccr &= ~0x00F3FFFF;
  251. immap->im_cpm.cp_rccr |= 0x00A00A00;
  252. memaddr = dpalloc (sizeof (pram_idma_t), 64);
  253. *(volatile u16 *)&immap->im_dprambase16
  254. [PROFF_IDMA2_BASE / sizeof(u16)] = memaddr;
  255. piptr = (volatile pram_idma_t *) ((uint) (immap) + memaddr);
  256. piptr->pi_resv1 = 0; /* manual says: clear it */
  257. piptr->pi_dcmbits.b.b_fb = 0;
  258. piptr->pi_dcmbits.b.b_lp = 1;
  259. piptr->pi_dcmbits.b.b_erm = 0;
  260. piptr->pi_dcmbits.b.b_dt = 0;
  261. memaddr = (uint) dpalloc (sizeof (ibd_t), 64);
  262. piptr->pi_ibase = piptr->pi_ibdptr = (volatile short) memaddr;
  263. bdf = (volatile ibd_t *) ((uint) (immap) + memaddr);
  264. bdf->ibd_bits.b.b_valid = 0;
  265. memaddr = (uint) dpalloc (64, 64);
  266. piptr->pi_dprbuf = (volatile ushort) memaddr;
  267. piptr->pi_dcmbits.b.b_wrap = 4;
  268. piptr->pi_ssmax = 32;
  269. piptr->pi_sts = piptr->pi_ssmax;
  270. piptr->pi_dts = piptr->pi_ssmax;
  271. return 1;
  272. }
  273. void dmadone_handler (void *arg)
  274. {
  275. immap->im_sdma.sdma_idmr2 = (uchar) 0x0;
  276. *dmadonep = 1;
  277. return;
  278. }
  279. static uint dpbase = 0;
  280. uint dpalloc (uint size, uint align)
  281. {
  282. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  283. uint retloc;
  284. uint align_mask, off;
  285. uint savebase;
  286. /* Pointer to initial global data area */
  287. if (dpinit_done == 0) {
  288. dpbase = gd->arch.dp_alloc_base;
  289. dpinit_done = 1;
  290. }
  291. align_mask = align - 1;
  292. savebase = dpbase;
  293. if ((off = (dpbase & align_mask)) != 0)
  294. dpbase += (align - off);
  295. if ((off = size & align_mask) != 0)
  296. size += align - off;
  297. if ((dpbase + size) >= gd->arch.dp_alloc_top) {
  298. dpbase = savebase;
  299. printf ("dpalloc: ran out of dual port ram!");
  300. return 0;
  301. }
  302. retloc = dpbase;
  303. dpbase += size;
  304. memset ((void *) &immr->im_dprambase[retloc], 0, size);
  305. return (retloc);
  306. }