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