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