mem_to_mem_idma2intr.c 8.4 KB

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