mem_to_mem_idma2intr.c 8.6 KB

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