pxa2xx_dma.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Intel XScale PXA255/270 DMA controller.
  3. *
  4. * Copyright (c) 2006 Openedhand Ltd.
  5. * Copyright (c) 2006 Thorsten Zitterell
  6. * Written by Andrzej Zaborowski <balrog@zabor.org>
  7. *
  8. * This code is licenced under the GPL.
  9. */
  10. #include "hw.h"
  11. #include "pxa.h"
  12. struct pxa2xx_dma_channel_s {
  13. target_phys_addr_t descr;
  14. target_phys_addr_t src;
  15. target_phys_addr_t dest;
  16. uint32_t cmd;
  17. uint32_t state;
  18. int request;
  19. };
  20. /* Allow the DMA to be used as a PIC. */
  21. typedef void (*pxa2xx_dma_handler_t)(void *opaque, int irq, int level);
  22. struct pxa2xx_dma_state_s {
  23. pxa2xx_dma_handler_t handler;
  24. qemu_irq irq;
  25. uint32_t stopintr;
  26. uint32_t eorintr;
  27. uint32_t rasintr;
  28. uint32_t startintr;
  29. uint32_t endintr;
  30. uint32_t align;
  31. uint32_t pio;
  32. int channels;
  33. struct pxa2xx_dma_channel_s *chan;
  34. uint8_t *req;
  35. /* Flag to avoid recursive DMA invocations. */
  36. int running;
  37. };
  38. #define PXA255_DMA_NUM_CHANNELS 16
  39. #define PXA27X_DMA_NUM_CHANNELS 32
  40. #define PXA2XX_DMA_NUM_REQUESTS 75
  41. #define DCSR0 0x0000 /* DMA Control / Status register for Channel 0 */
  42. #define DCSR31 0x007c /* DMA Control / Status register for Channel 31 */
  43. #define DALGN 0x00a0 /* DMA Alignment register */
  44. #define DPCSR 0x00a4 /* DMA Programmed I/O Control Status register */
  45. #define DRQSR0 0x00e0 /* DMA DREQ<0> Status register */
  46. #define DRQSR1 0x00e4 /* DMA DREQ<1> Status register */
  47. #define DRQSR2 0x00e8 /* DMA DREQ<2> Status register */
  48. #define DINT 0x00f0 /* DMA Interrupt register */
  49. #define DRCMR0 0x0100 /* Request to Channel Map register 0 */
  50. #define DRCMR63 0x01fc /* Request to Channel Map register 63 */
  51. #define D_CH0 0x0200 /* Channel 0 Descriptor start */
  52. #define DRCMR64 0x1100 /* Request to Channel Map register 64 */
  53. #define DRCMR74 0x1128 /* Request to Channel Map register 74 */
  54. /* Per-channel register */
  55. #define DDADR 0x00
  56. #define DSADR 0x01
  57. #define DTADR 0x02
  58. #define DCMD 0x03
  59. /* Bit-field masks */
  60. #define DRCMR_CHLNUM 0x1f
  61. #define DRCMR_MAPVLD (1 << 7)
  62. #define DDADR_STOP (1 << 0)
  63. #define DDADR_BREN (1 << 1)
  64. #define DCMD_LEN 0x1fff
  65. #define DCMD_WIDTH(x) (1 << ((((x) >> 14) & 3) - 1))
  66. #define DCMD_SIZE(x) (4 << (((x) >> 16) & 3))
  67. #define DCMD_FLYBYT (1 << 19)
  68. #define DCMD_FLYBYS (1 << 20)
  69. #define DCMD_ENDIRQEN (1 << 21)
  70. #define DCMD_STARTIRQEN (1 << 22)
  71. #define DCMD_CMPEN (1 << 25)
  72. #define DCMD_FLOWTRG (1 << 28)
  73. #define DCMD_FLOWSRC (1 << 29)
  74. #define DCMD_INCTRGADDR (1 << 30)
  75. #define DCMD_INCSRCADDR (1 << 31)
  76. #define DCSR_BUSERRINTR (1 << 0)
  77. #define DCSR_STARTINTR (1 << 1)
  78. #define DCSR_ENDINTR (1 << 2)
  79. #define DCSR_STOPINTR (1 << 3)
  80. #define DCSR_RASINTR (1 << 4)
  81. #define DCSR_REQPEND (1 << 8)
  82. #define DCSR_EORINT (1 << 9)
  83. #define DCSR_CMPST (1 << 10)
  84. #define DCSR_MASKRUN (1 << 22)
  85. #define DCSR_RASIRQEN (1 << 23)
  86. #define DCSR_CLRCMPST (1 << 24)
  87. #define DCSR_SETCMPST (1 << 25)
  88. #define DCSR_EORSTOPEN (1 << 26)
  89. #define DCSR_EORJMPEN (1 << 27)
  90. #define DCSR_EORIRQEN (1 << 28)
  91. #define DCSR_STOPIRQEN (1 << 29)
  92. #define DCSR_NODESCFETCH (1 << 30)
  93. #define DCSR_RUN (1 << 31)
  94. static inline void pxa2xx_dma_update(struct pxa2xx_dma_state_s *s, int ch)
  95. {
  96. if (ch >= 0) {
  97. if ((s->chan[ch].state & DCSR_STOPIRQEN) &&
  98. (s->chan[ch].state & DCSR_STOPINTR))
  99. s->stopintr |= 1 << ch;
  100. else
  101. s->stopintr &= ~(1 << ch);
  102. if ((s->chan[ch].state & DCSR_EORIRQEN) &&
  103. (s->chan[ch].state & DCSR_EORINT))
  104. s->eorintr |= 1 << ch;
  105. else
  106. s->eorintr &= ~(1 << ch);
  107. if ((s->chan[ch].state & DCSR_RASIRQEN) &&
  108. (s->chan[ch].state & DCSR_RASINTR))
  109. s->rasintr |= 1 << ch;
  110. else
  111. s->rasintr &= ~(1 << ch);
  112. if (s->chan[ch].state & DCSR_STARTINTR)
  113. s->startintr |= 1 << ch;
  114. else
  115. s->startintr &= ~(1 << ch);
  116. if (s->chan[ch].state & DCSR_ENDINTR)
  117. s->endintr |= 1 << ch;
  118. else
  119. s->endintr &= ~(1 << ch);
  120. }
  121. if (s->stopintr | s->eorintr | s->rasintr | s->startintr | s->endintr)
  122. qemu_irq_raise(s->irq);
  123. else
  124. qemu_irq_lower(s->irq);
  125. }
  126. static inline void pxa2xx_dma_descriptor_fetch(
  127. struct pxa2xx_dma_state_s *s, int ch)
  128. {
  129. uint32_t desc[4];
  130. target_phys_addr_t daddr = s->chan[ch].descr & ~0xf;
  131. if ((s->chan[ch].descr & DDADR_BREN) && (s->chan[ch].state & DCSR_CMPST))
  132. daddr += 32;
  133. cpu_physical_memory_read(daddr, (uint8_t *) desc, 16);
  134. s->chan[ch].descr = desc[DDADR];
  135. s->chan[ch].src = desc[DSADR];
  136. s->chan[ch].dest = desc[DTADR];
  137. s->chan[ch].cmd = desc[DCMD];
  138. if (s->chan[ch].cmd & DCMD_FLOWSRC)
  139. s->chan[ch].src &= ~3;
  140. if (s->chan[ch].cmd & DCMD_FLOWTRG)
  141. s->chan[ch].dest &= ~3;
  142. if (s->chan[ch].cmd & (DCMD_CMPEN | DCMD_FLYBYS | DCMD_FLYBYT))
  143. printf("%s: unsupported mode in channel %i\n", __FUNCTION__, ch);
  144. if (s->chan[ch].cmd & DCMD_STARTIRQEN)
  145. s->chan[ch].state |= DCSR_STARTINTR;
  146. }
  147. static void pxa2xx_dma_run(struct pxa2xx_dma_state_s *s)
  148. {
  149. int c, srcinc, destinc;
  150. uint32_t n, size;
  151. uint32_t width;
  152. uint32_t length;
  153. uint8_t buffer[32];
  154. struct pxa2xx_dma_channel_s *ch;
  155. if (s->running ++)
  156. return;
  157. while (s->running) {
  158. s->running = 1;
  159. for (c = 0; c < s->channels; c ++) {
  160. ch = &s->chan[c];
  161. while ((ch->state & DCSR_RUN) && !(ch->state & DCSR_STOPINTR)) {
  162. /* Test for pending requests */
  163. if ((ch->cmd & (DCMD_FLOWSRC | DCMD_FLOWTRG)) && !ch->request)
  164. break;
  165. length = ch->cmd & DCMD_LEN;
  166. size = DCMD_SIZE(ch->cmd);
  167. width = DCMD_WIDTH(ch->cmd);
  168. srcinc = (ch->cmd & DCMD_INCSRCADDR) ? width : 0;
  169. destinc = (ch->cmd & DCMD_INCTRGADDR) ? width : 0;
  170. while (length) {
  171. size = MIN(length, size);
  172. for (n = 0; n < size; n += width) {
  173. cpu_physical_memory_read(ch->src, buffer + n, width);
  174. ch->src += srcinc;
  175. }
  176. for (n = 0; n < size; n += width) {
  177. cpu_physical_memory_write(ch->dest, buffer + n, width);
  178. ch->dest += destinc;
  179. }
  180. length -= size;
  181. if ((ch->cmd & (DCMD_FLOWSRC | DCMD_FLOWTRG)) &&
  182. !ch->request) {
  183. ch->state |= DCSR_EORINT;
  184. if (ch->state & DCSR_EORSTOPEN)
  185. ch->state |= DCSR_STOPINTR;
  186. if ((ch->state & DCSR_EORJMPEN) &&
  187. !(ch->state & DCSR_NODESCFETCH))
  188. pxa2xx_dma_descriptor_fetch(s, c);
  189. break;
  190. }
  191. }
  192. ch->cmd = (ch->cmd & ~DCMD_LEN) | length;
  193. /* Is the transfer complete now? */
  194. if (!length) {
  195. if (ch->cmd & DCMD_ENDIRQEN)
  196. ch->state |= DCSR_ENDINTR;
  197. if ((ch->state & DCSR_NODESCFETCH) ||
  198. (ch->descr & DDADR_STOP) ||
  199. (ch->state & DCSR_EORSTOPEN)) {
  200. ch->state |= DCSR_STOPINTR;
  201. ch->state &= ~DCSR_RUN;
  202. break;
  203. }
  204. ch->state |= DCSR_STOPINTR;
  205. break;
  206. }
  207. }
  208. }
  209. s->running --;
  210. }
  211. }
  212. static uint32_t pxa2xx_dma_read(void *opaque, target_phys_addr_t offset)
  213. {
  214. struct pxa2xx_dma_state_s *s = (struct pxa2xx_dma_state_s *) opaque;
  215. unsigned int channel;
  216. switch (offset) {
  217. case DRCMR64 ... DRCMR74:
  218. offset -= DRCMR64 - DRCMR0 - (64 << 2);
  219. /* Fall through */
  220. case DRCMR0 ... DRCMR63:
  221. channel = (offset - DRCMR0) >> 2;
  222. return s->req[channel];
  223. case DRQSR0:
  224. case DRQSR1:
  225. case DRQSR2:
  226. return 0;
  227. case DCSR0 ... DCSR31:
  228. channel = offset >> 2;
  229. if (s->chan[channel].request)
  230. return s->chan[channel].state | DCSR_REQPEND;
  231. return s->chan[channel].state;
  232. case DINT:
  233. return s->stopintr | s->eorintr | s->rasintr |
  234. s->startintr | s->endintr;
  235. case DALGN:
  236. return s->align;
  237. case DPCSR:
  238. return s->pio;
  239. }
  240. if (offset >= D_CH0 && offset < D_CH0 + (s->channels << 4)) {
  241. channel = (offset - D_CH0) >> 4;
  242. switch ((offset & 0x0f) >> 2) {
  243. case DDADR:
  244. return s->chan[channel].descr;
  245. case DSADR:
  246. return s->chan[channel].src;
  247. case DTADR:
  248. return s->chan[channel].dest;
  249. case DCMD:
  250. return s->chan[channel].cmd;
  251. }
  252. }
  253. cpu_abort(cpu_single_env,
  254. "%s: Bad offset 0x" TARGET_FMT_plx "\n", __FUNCTION__, offset);
  255. return 7;
  256. }
  257. static void pxa2xx_dma_write(void *opaque,
  258. target_phys_addr_t offset, uint32_t value)
  259. {
  260. struct pxa2xx_dma_state_s *s = (struct pxa2xx_dma_state_s *) opaque;
  261. unsigned int channel;
  262. switch (offset) {
  263. case DRCMR64 ... DRCMR74:
  264. offset -= DRCMR64 - DRCMR0 - (64 << 2);
  265. /* Fall through */
  266. case DRCMR0 ... DRCMR63:
  267. channel = (offset - DRCMR0) >> 2;
  268. if (value & DRCMR_MAPVLD)
  269. if ((value & DRCMR_CHLNUM) > s->channels)
  270. cpu_abort(cpu_single_env, "%s: Bad DMA channel %i\n",
  271. __FUNCTION__, value & DRCMR_CHLNUM);
  272. s->req[channel] = value;
  273. break;
  274. case DRQSR0:
  275. case DRQSR1:
  276. case DRQSR2:
  277. /* Nothing to do */
  278. break;
  279. case DCSR0 ... DCSR31:
  280. channel = offset >> 2;
  281. s->chan[channel].state &= 0x0000071f & ~(value &
  282. (DCSR_EORINT | DCSR_ENDINTR |
  283. DCSR_STARTINTR | DCSR_BUSERRINTR));
  284. s->chan[channel].state |= value & 0xfc800000;
  285. if (s->chan[channel].state & DCSR_STOPIRQEN)
  286. s->chan[channel].state &= ~DCSR_STOPINTR;
  287. if (value & DCSR_NODESCFETCH) {
  288. /* No-descriptor-fetch mode */
  289. if (value & DCSR_RUN) {
  290. s->chan[channel].state &= ~DCSR_STOPINTR;
  291. pxa2xx_dma_run(s);
  292. }
  293. } else {
  294. /* Descriptor-fetch mode */
  295. if (value & DCSR_RUN) {
  296. s->chan[channel].state &= ~DCSR_STOPINTR;
  297. pxa2xx_dma_descriptor_fetch(s, channel);
  298. pxa2xx_dma_run(s);
  299. }
  300. }
  301. /* Shouldn't matter as our DMA is synchronous. */
  302. if (!(value & (DCSR_RUN | DCSR_MASKRUN)))
  303. s->chan[channel].state |= DCSR_STOPINTR;
  304. if (value & DCSR_CLRCMPST)
  305. s->chan[channel].state &= ~DCSR_CMPST;
  306. if (value & DCSR_SETCMPST)
  307. s->chan[channel].state |= DCSR_CMPST;
  308. pxa2xx_dma_update(s, channel);
  309. break;
  310. case DALGN:
  311. s->align = value;
  312. break;
  313. case DPCSR:
  314. s->pio = value & 0x80000001;
  315. break;
  316. default:
  317. if (offset >= D_CH0 && offset < D_CH0 + (s->channels << 4)) {
  318. channel = (offset - D_CH0) >> 4;
  319. switch ((offset & 0x0f) >> 2) {
  320. case DDADR:
  321. s->chan[channel].descr = value;
  322. break;
  323. case DSADR:
  324. s->chan[channel].src = value;
  325. break;
  326. case DTADR:
  327. s->chan[channel].dest = value;
  328. break;
  329. case DCMD:
  330. s->chan[channel].cmd = value;
  331. break;
  332. default:
  333. goto fail;
  334. }
  335. break;
  336. }
  337. fail:
  338. cpu_abort(cpu_single_env, "%s: Bad offset " TARGET_FMT_plx "\n",
  339. __FUNCTION__, offset);
  340. }
  341. }
  342. static uint32_t pxa2xx_dma_readbad(void *opaque, target_phys_addr_t offset)
  343. {
  344. cpu_abort(cpu_single_env, "%s: Bad access width\n", __FUNCTION__);
  345. return 5;
  346. }
  347. static void pxa2xx_dma_writebad(void *opaque,
  348. target_phys_addr_t offset, uint32_t value)
  349. {
  350. cpu_abort(cpu_single_env, "%s: Bad access width\n", __FUNCTION__);
  351. }
  352. static CPUReadMemoryFunc *pxa2xx_dma_readfn[] = {
  353. pxa2xx_dma_readbad,
  354. pxa2xx_dma_readbad,
  355. pxa2xx_dma_read
  356. };
  357. static CPUWriteMemoryFunc *pxa2xx_dma_writefn[] = {
  358. pxa2xx_dma_writebad,
  359. pxa2xx_dma_writebad,
  360. pxa2xx_dma_write
  361. };
  362. static void pxa2xx_dma_save(QEMUFile *f, void *opaque)
  363. {
  364. struct pxa2xx_dma_state_s *s = (struct pxa2xx_dma_state_s *) opaque;
  365. int i;
  366. qemu_put_be32(f, s->channels);
  367. qemu_put_be32s(f, &s->stopintr);
  368. qemu_put_be32s(f, &s->eorintr);
  369. qemu_put_be32s(f, &s->rasintr);
  370. qemu_put_be32s(f, &s->startintr);
  371. qemu_put_be32s(f, &s->endintr);
  372. qemu_put_be32s(f, &s->align);
  373. qemu_put_be32s(f, &s->pio);
  374. qemu_put_buffer(f, s->req, PXA2XX_DMA_NUM_REQUESTS);
  375. for (i = 0; i < s->channels; i ++) {
  376. qemu_put_betl(f, s->chan[i].descr);
  377. qemu_put_betl(f, s->chan[i].src);
  378. qemu_put_betl(f, s->chan[i].dest);
  379. qemu_put_be32s(f, &s->chan[i].cmd);
  380. qemu_put_be32s(f, &s->chan[i].state);
  381. qemu_put_be32(f, s->chan[i].request);
  382. };
  383. }
  384. static int pxa2xx_dma_load(QEMUFile *f, void *opaque, int version_id)
  385. {
  386. struct pxa2xx_dma_state_s *s = (struct pxa2xx_dma_state_s *) opaque;
  387. int i;
  388. if (qemu_get_be32(f) != s->channels)
  389. return -EINVAL;
  390. qemu_get_be32s(f, &s->stopintr);
  391. qemu_get_be32s(f, &s->eorintr);
  392. qemu_get_be32s(f, &s->rasintr);
  393. qemu_get_be32s(f, &s->startintr);
  394. qemu_get_be32s(f, &s->endintr);
  395. qemu_get_be32s(f, &s->align);
  396. qemu_get_be32s(f, &s->pio);
  397. qemu_get_buffer(f, s->req, PXA2XX_DMA_NUM_REQUESTS);
  398. for (i = 0; i < s->channels; i ++) {
  399. s->chan[i].descr = qemu_get_betl(f);
  400. s->chan[i].src = qemu_get_betl(f);
  401. s->chan[i].dest = qemu_get_betl(f);
  402. qemu_get_be32s(f, &s->chan[i].cmd);
  403. qemu_get_be32s(f, &s->chan[i].state);
  404. s->chan[i].request = qemu_get_be32(f);
  405. };
  406. return 0;
  407. }
  408. static struct pxa2xx_dma_state_s *pxa2xx_dma_init(target_phys_addr_t base,
  409. qemu_irq irq, int channels)
  410. {
  411. int i, iomemtype;
  412. struct pxa2xx_dma_state_s *s;
  413. s = (struct pxa2xx_dma_state_s *)
  414. qemu_mallocz(sizeof(struct pxa2xx_dma_state_s));
  415. s->channels = channels;
  416. s->chan = qemu_mallocz(sizeof(struct pxa2xx_dma_channel_s) * s->channels);
  417. s->irq = irq;
  418. s->handler = (pxa2xx_dma_handler_t) pxa2xx_dma_request;
  419. s->req = qemu_mallocz(sizeof(uint8_t) * PXA2XX_DMA_NUM_REQUESTS);
  420. memset(s->chan, 0, sizeof(struct pxa2xx_dma_channel_s) * s->channels);
  421. for (i = 0; i < s->channels; i ++)
  422. s->chan[i].state = DCSR_STOPINTR;
  423. memset(s->req, 0, sizeof(uint8_t) * PXA2XX_DMA_NUM_REQUESTS);
  424. iomemtype = cpu_register_io_memory(0, pxa2xx_dma_readfn,
  425. pxa2xx_dma_writefn, s);
  426. cpu_register_physical_memory(base, 0x00010000, iomemtype);
  427. register_savevm("pxa2xx_dma", 0, 0, pxa2xx_dma_save, pxa2xx_dma_load, s);
  428. return s;
  429. }
  430. struct pxa2xx_dma_state_s *pxa27x_dma_init(target_phys_addr_t base,
  431. qemu_irq irq)
  432. {
  433. return pxa2xx_dma_init(base, irq, PXA27X_DMA_NUM_CHANNELS);
  434. }
  435. struct pxa2xx_dma_state_s *pxa255_dma_init(target_phys_addr_t base,
  436. qemu_irq irq)
  437. {
  438. return pxa2xx_dma_init(base, irq, PXA255_DMA_NUM_CHANNELS);
  439. }
  440. void pxa2xx_dma_request(struct pxa2xx_dma_state_s *s, int req_num, int on)
  441. {
  442. int ch;
  443. if (req_num < 0 || req_num >= PXA2XX_DMA_NUM_REQUESTS)
  444. cpu_abort(cpu_single_env,
  445. "%s: Bad DMA request %i\n", __FUNCTION__, req_num);
  446. if (!(s->req[req_num] & DRCMR_MAPVLD))
  447. return;
  448. ch = s->req[req_num] & DRCMR_CHLNUM;
  449. if (!s->chan[ch].request && on)
  450. s->chan[ch].state |= DCSR_RASINTR;
  451. else
  452. s->chan[ch].state &= ~DCSR_RASINTR;
  453. if (s->chan[ch].request && !on)
  454. s->chan[ch].state |= DCSR_EORINT;
  455. s->chan[ch].request = on;
  456. if (on) {
  457. pxa2xx_dma_run(s);
  458. pxa2xx_dma_update(s, ch);
  459. }
  460. }