pl041.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. /*
  2. * Arm PrimeCell PL041 Advanced Audio Codec Interface
  3. *
  4. * Copyright (c) 2011
  5. * Written by Mathieu Sonet - www.elasticsheep.com
  6. *
  7. * This code is licenced under the GPL.
  8. *
  9. * *****************************************************************
  10. *
  11. * This driver emulates the ARM AACI interface
  12. * connected to a LM4549 codec.
  13. *
  14. * Limitations:
  15. * - Supports only a playback on one channel (Versatile/Vexpress)
  16. * - Supports only one TX FIFO in compact-mode or non-compact mode.
  17. * - Supports playback of 12, 16, 18 and 20 bits samples.
  18. * - Record is not supported.
  19. * - The PL041 is hardwired to a LM4549 codec.
  20. *
  21. */
  22. #include "sysbus.h"
  23. #include "pl041.h"
  24. #include "lm4549.h"
  25. #if 0
  26. #define PL041_DEBUG_LEVEL 1
  27. #endif
  28. #if defined(PL041_DEBUG_LEVEL) && (PL041_DEBUG_LEVEL >= 1)
  29. #define DBG_L1(fmt, ...) \
  30. do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0)
  31. #else
  32. #define DBG_L1(fmt, ...) \
  33. do { } while (0)
  34. #endif
  35. #if defined(PL041_DEBUG_LEVEL) && (PL041_DEBUG_LEVEL >= 2)
  36. #define DBG_L2(fmt, ...) \
  37. do { printf("pl041: " fmt , ## __VA_ARGS__); } while (0)
  38. #else
  39. #define DBG_L2(fmt, ...) \
  40. do { } while (0)
  41. #endif
  42. #define MAX_FIFO_DEPTH (1024)
  43. #define DEFAULT_FIFO_DEPTH (8)
  44. #define SLOT1_RW (1 << 19)
  45. /* This FIFO only stores 20-bit samples on 32-bit words.
  46. So its level is independent of the selected mode */
  47. typedef struct {
  48. uint32_t level;
  49. uint32_t data[MAX_FIFO_DEPTH];
  50. } pl041_fifo;
  51. typedef struct {
  52. pl041_fifo tx_fifo;
  53. uint8_t tx_enabled;
  54. uint8_t tx_compact_mode;
  55. uint8_t tx_sample_size;
  56. pl041_fifo rx_fifo;
  57. uint8_t rx_enabled;
  58. uint8_t rx_compact_mode;
  59. uint8_t rx_sample_size;
  60. } pl041_channel;
  61. typedef struct {
  62. SysBusDevice busdev;
  63. MemoryRegion iomem;
  64. qemu_irq irq;
  65. uint32_t fifo_depth; /* FIFO depth in non-compact mode */
  66. pl041_regfile regs;
  67. pl041_channel fifo1;
  68. lm4549_state codec;
  69. } pl041_state;
  70. static const unsigned char pl041_default_id[8] = {
  71. 0x41, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
  72. };
  73. #if defined(PL041_DEBUG_LEVEL)
  74. #define REGISTER(name, offset) #name,
  75. static const char *pl041_regs_name[] = {
  76. #include "pl041.hx"
  77. };
  78. #undef REGISTER
  79. #endif
  80. #if defined(PL041_DEBUG_LEVEL)
  81. static const char *get_reg_name(target_phys_addr_t offset)
  82. {
  83. if (offset <= PL041_dr1_7) {
  84. return pl041_regs_name[offset >> 2];
  85. }
  86. return "unknown";
  87. }
  88. #endif
  89. static uint8_t pl041_compute_periphid3(pl041_state *s)
  90. {
  91. uint8_t id3 = 1; /* One channel */
  92. /* Add the fifo depth information */
  93. switch (s->fifo_depth) {
  94. case 8:
  95. id3 |= 0 << 3;
  96. break;
  97. case 32:
  98. id3 |= 1 << 3;
  99. break;
  100. case 64:
  101. id3 |= 2 << 3;
  102. break;
  103. case 128:
  104. id3 |= 3 << 3;
  105. break;
  106. case 256:
  107. id3 |= 4 << 3;
  108. break;
  109. case 512:
  110. id3 |= 5 << 3;
  111. break;
  112. case 1024:
  113. id3 |= 6 << 3;
  114. break;
  115. case 2048:
  116. id3 |= 7 << 3;
  117. break;
  118. }
  119. return id3;
  120. }
  121. static void pl041_reset(pl041_state *s)
  122. {
  123. DBG_L1("pl041_reset\n");
  124. memset(&s->regs, 0x00, sizeof(pl041_regfile));
  125. s->regs.slfr = SL1TXEMPTY | SL2TXEMPTY | SL12TXEMPTY;
  126. s->regs.sr1 = TXFE | RXFE | TXHE;
  127. s->regs.isr1 = 0;
  128. memset(&s->fifo1, 0x00, sizeof(s->fifo1));
  129. }
  130. static void pl041_fifo1_write(pl041_state *s, uint32_t value)
  131. {
  132. pl041_channel *channel = &s->fifo1;
  133. pl041_fifo *fifo = &s->fifo1.tx_fifo;
  134. /* Push the value in the FIFO */
  135. if (channel->tx_compact_mode == 0) {
  136. /* Non-compact mode */
  137. if (fifo->level < s->fifo_depth) {
  138. /* Pad the value with 0 to obtain a 20-bit sample */
  139. switch (channel->tx_sample_size) {
  140. case 12:
  141. value = (value << 8) & 0xFFFFF;
  142. break;
  143. case 16:
  144. value = (value << 4) & 0xFFFFF;
  145. break;
  146. case 18:
  147. value = (value << 2) & 0xFFFFF;
  148. break;
  149. case 20:
  150. default:
  151. break;
  152. }
  153. /* Store the sample in the FIFO */
  154. fifo->data[fifo->level++] = value;
  155. }
  156. #if defined(PL041_DEBUG_LEVEL)
  157. else {
  158. DBG_L1("fifo1 write: overrun\n");
  159. }
  160. #endif
  161. } else {
  162. /* Compact mode */
  163. if ((fifo->level + 2) < s->fifo_depth) {
  164. uint32_t i = 0;
  165. uint32_t sample = 0;
  166. for (i = 0; i < 2; i++) {
  167. sample = value & 0xFFFF;
  168. value = value >> 16;
  169. /* Pad each sample with 0 to obtain a 20-bit sample */
  170. switch (channel->tx_sample_size) {
  171. case 12:
  172. sample = sample << 8;
  173. break;
  174. case 16:
  175. default:
  176. sample = sample << 4;
  177. break;
  178. }
  179. /* Store the sample in the FIFO */
  180. fifo->data[fifo->level++] = sample;
  181. }
  182. }
  183. #if defined(PL041_DEBUG_LEVEL)
  184. else {
  185. DBG_L1("fifo1 write: overrun\n");
  186. }
  187. #endif
  188. }
  189. /* Update the status register */
  190. if (fifo->level > 0) {
  191. s->regs.sr1 &= ~(TXUNDERRUN | TXFE);
  192. }
  193. if (fifo->level >= (s->fifo_depth / 2)) {
  194. s->regs.sr1 &= ~TXHE;
  195. }
  196. if (fifo->level >= s->fifo_depth) {
  197. s->regs.sr1 |= TXFF;
  198. }
  199. DBG_L2("fifo1_push sr1 = 0x%08x\n", s->regs.sr1);
  200. }
  201. static void pl041_fifo1_transmit(pl041_state *s)
  202. {
  203. pl041_channel *channel = &s->fifo1;
  204. pl041_fifo *fifo = &s->fifo1.tx_fifo;
  205. uint32_t slots = s->regs.txcr1 & TXSLOT_MASK;
  206. uint32_t written_samples;
  207. /* Check if FIFO1 transmit is enabled */
  208. if ((channel->tx_enabled) && (slots & (TXSLOT3 | TXSLOT4))) {
  209. if (fifo->level >= (s->fifo_depth / 2)) {
  210. int i;
  211. DBG_L1("Transfer FIFO level = %i\n", fifo->level);
  212. /* Try to transfer the whole FIFO */
  213. for (i = 0; i < (fifo->level / 2); i++) {
  214. uint32_t left = fifo->data[i * 2];
  215. uint32_t right = fifo->data[i * 2 + 1];
  216. /* Transmit two 20-bit samples to the codec */
  217. if (lm4549_write_samples(&s->codec, left, right) == 0) {
  218. DBG_L1("Codec buffer full\n");
  219. break;
  220. }
  221. }
  222. written_samples = i * 2;
  223. if (written_samples > 0) {
  224. /* Update the FIFO level */
  225. fifo->level -= written_samples;
  226. /* Move back the pending samples to the start of the FIFO */
  227. for (i = 0; i < fifo->level; i++) {
  228. fifo->data[i] = fifo->data[written_samples + i];
  229. }
  230. /* Update the status register */
  231. s->regs.sr1 &= ~TXFF;
  232. if (fifo->level <= (s->fifo_depth / 2)) {
  233. s->regs.sr1 |= TXHE;
  234. }
  235. if (fifo->level == 0) {
  236. s->regs.sr1 |= TXFE | TXUNDERRUN;
  237. DBG_L1("Empty FIFO\n");
  238. }
  239. }
  240. }
  241. }
  242. }
  243. static void pl041_isr1_update(pl041_state *s)
  244. {
  245. /* Update ISR1 */
  246. if (s->regs.sr1 & TXUNDERRUN) {
  247. s->regs.isr1 |= URINTR;
  248. } else {
  249. s->regs.isr1 &= ~URINTR;
  250. }
  251. if (s->regs.sr1 & TXHE) {
  252. s->regs.isr1 |= TXINTR;
  253. } else {
  254. s->regs.isr1 &= ~TXINTR;
  255. }
  256. if (!(s->regs.sr1 & TXBUSY) && (s->regs.sr1 & TXFE)) {
  257. s->regs.isr1 |= TXCINTR;
  258. } else {
  259. s->regs.isr1 &= ~TXCINTR;
  260. }
  261. /* Update the irq state */
  262. qemu_set_irq(s->irq, ((s->regs.isr1 & s->regs.ie1) > 0) ? 1 : 0);
  263. DBG_L2("Set interrupt sr1 = 0x%08x isr1 = 0x%08x masked = 0x%08x\n",
  264. s->regs.sr1, s->regs.isr1, s->regs.isr1 & s->regs.ie1);
  265. }
  266. static void pl041_request_data(void *opaque)
  267. {
  268. pl041_state *s = (pl041_state *)opaque;
  269. /* Trigger pending transfers */
  270. pl041_fifo1_transmit(s);
  271. pl041_isr1_update(s);
  272. }
  273. static uint64_t pl041_read(void *opaque, target_phys_addr_t offset,
  274. unsigned size)
  275. {
  276. pl041_state *s = (pl041_state *)opaque;
  277. int value;
  278. if ((offset >= PL041_periphid0) && (offset <= PL041_pcellid3)) {
  279. if (offset == PL041_periphid3) {
  280. value = pl041_compute_periphid3(s);
  281. } else {
  282. value = pl041_default_id[(offset - PL041_periphid0) >> 2];
  283. }
  284. DBG_L1("pl041_read [0x%08x] => 0x%08x\n", offset, value);
  285. return value;
  286. } else if (offset <= PL041_dr4_7) {
  287. value = *((uint32_t *)&s->regs + (offset >> 2));
  288. } else {
  289. DBG_L1("pl041_read: Reserved offset %x\n", (int)offset);
  290. return 0;
  291. }
  292. switch (offset) {
  293. case PL041_allints:
  294. value = s->regs.isr1 & 0x7F;
  295. break;
  296. }
  297. DBG_L1("pl041_read [0x%08x] %s => 0x%08x\n", offset,
  298. get_reg_name(offset), value);
  299. return value;
  300. }
  301. static void pl041_write(void *opaque, target_phys_addr_t offset,
  302. uint64_t value, unsigned size)
  303. {
  304. pl041_state *s = (pl041_state *)opaque;
  305. uint16_t control, data;
  306. uint32_t result;
  307. DBG_L1("pl041_write [0x%08x] %s <= 0x%08x\n", offset,
  308. get_reg_name(offset), (unsigned int)value);
  309. /* Write the register */
  310. if (offset <= PL041_dr4_7) {
  311. *((uint32_t *)&s->regs + (offset >> 2)) = value;
  312. } else {
  313. DBG_L1("pl041_write: Reserved offset %x\n", (int)offset);
  314. return;
  315. }
  316. /* Execute the actions */
  317. switch (offset) {
  318. case PL041_txcr1:
  319. {
  320. pl041_channel *channel = &s->fifo1;
  321. uint32_t txen = s->regs.txcr1 & TXEN;
  322. uint32_t tsize = (s->regs.txcr1 & TSIZE_MASK) >> TSIZE_MASK_BIT;
  323. uint32_t compact_mode = (s->regs.txcr1 & TXCOMPACT) ? 1 : 0;
  324. #if defined(PL041_DEBUG_LEVEL)
  325. uint32_t slots = (s->regs.txcr1 & TXSLOT_MASK) >> TXSLOT_MASK_BIT;
  326. uint32_t txfen = (s->regs.txcr1 & TXFEN) > 0 ? 1 : 0;
  327. #endif
  328. DBG_L1("=> txen = %i slots = 0x%01x tsize = %i compact = %i "
  329. "txfen = %i\n", txen, slots, tsize, compact_mode, txfen);
  330. channel->tx_enabled = txen;
  331. channel->tx_compact_mode = compact_mode;
  332. switch (tsize) {
  333. case 0:
  334. channel->tx_sample_size = 16;
  335. break;
  336. case 1:
  337. channel->tx_sample_size = 18;
  338. break;
  339. case 2:
  340. channel->tx_sample_size = 20;
  341. break;
  342. case 3:
  343. channel->tx_sample_size = 12;
  344. break;
  345. }
  346. DBG_L1("TX enabled = %i\n", channel->tx_enabled);
  347. DBG_L1("TX compact mode = %i\n", channel->tx_compact_mode);
  348. DBG_L1("TX sample width = %i\n", channel->tx_sample_size);
  349. /* Check if compact mode is allowed with selected tsize */
  350. if (channel->tx_compact_mode == 1) {
  351. if ((channel->tx_sample_size == 18) ||
  352. (channel->tx_sample_size == 20)) {
  353. channel->tx_compact_mode = 0;
  354. DBG_L1("Compact mode not allowed with 18/20-bit sample size\n");
  355. }
  356. }
  357. break;
  358. }
  359. case PL041_sl1tx:
  360. s->regs.slfr &= ~SL1TXEMPTY;
  361. control = (s->regs.sl1tx >> 12) & 0x7F;
  362. data = (s->regs.sl2tx >> 4) & 0xFFFF;
  363. if ((s->regs.sl1tx & SLOT1_RW) == 0) {
  364. /* Write operation */
  365. lm4549_write(&s->codec, control, data);
  366. } else {
  367. /* Read operation */
  368. result = lm4549_read(&s->codec, control);
  369. /* Store the returned value */
  370. s->regs.sl1rx = s->regs.sl1tx & ~SLOT1_RW;
  371. s->regs.sl2rx = result << 4;
  372. s->regs.slfr &= ~(SL1RXBUSY | SL2RXBUSY);
  373. s->regs.slfr |= SL1RXVALID | SL2RXVALID;
  374. }
  375. break;
  376. case PL041_sl2tx:
  377. s->regs.sl2tx = value;
  378. s->regs.slfr &= ~SL2TXEMPTY;
  379. break;
  380. case PL041_intclr:
  381. DBG_L1("=> Clear interrupt intclr = 0x%08x isr1 = 0x%08x\n",
  382. s->regs.intclr, s->regs.isr1);
  383. if (s->regs.intclr & TXUEC1) {
  384. s->regs.sr1 &= ~TXUNDERRUN;
  385. }
  386. break;
  387. case PL041_maincr:
  388. {
  389. #if defined(PL041_DEBUG_LEVEL)
  390. char debug[] = " AACIFE SL1RXEN SL1TXEN";
  391. if (!(value & AACIFE)) {
  392. debug[0] = '!';
  393. }
  394. if (!(value & SL1RXEN)) {
  395. debug[8] = '!';
  396. }
  397. if (!(value & SL1TXEN)) {
  398. debug[17] = '!';
  399. }
  400. DBG_L1("%s\n", debug);
  401. #endif
  402. if ((s->regs.maincr & AACIFE) == 0) {
  403. pl041_reset(s);
  404. }
  405. break;
  406. }
  407. case PL041_dr1_0:
  408. case PL041_dr1_1:
  409. case PL041_dr1_2:
  410. case PL041_dr1_3:
  411. pl041_fifo1_write(s, value);
  412. break;
  413. }
  414. /* Transmit the FIFO content */
  415. pl041_fifo1_transmit(s);
  416. /* Update the ISR1 register */
  417. pl041_isr1_update(s);
  418. }
  419. static void pl041_device_reset(DeviceState *d)
  420. {
  421. pl041_state *s = DO_UPCAST(pl041_state, busdev.qdev, d);
  422. pl041_reset(s);
  423. }
  424. static const MemoryRegionOps pl041_ops = {
  425. .read = pl041_read,
  426. .write = pl041_write,
  427. .endianness = DEVICE_NATIVE_ENDIAN,
  428. };
  429. static int pl041_init(SysBusDevice *dev)
  430. {
  431. pl041_state *s = FROM_SYSBUS(pl041_state, dev);
  432. DBG_L1("pl041_init 0x%08x\n", (uint32_t)s);
  433. /* Check the device properties */
  434. switch (s->fifo_depth) {
  435. case 8:
  436. case 32:
  437. case 64:
  438. case 128:
  439. case 256:
  440. case 512:
  441. case 1024:
  442. case 2048:
  443. break;
  444. case 16:
  445. default:
  446. /* NC FIFO depth of 16 is not allowed because its id bits in
  447. AACIPERIPHID3 overlap with the id for the default NC FIFO depth */
  448. fprintf(stderr, "pl041: unsupported non-compact fifo depth [%i]\n",
  449. s->fifo_depth);
  450. return -1;
  451. }
  452. /* Connect the device to the sysbus */
  453. memory_region_init_io(&s->iomem, &pl041_ops, s, "pl041", 0x1000);
  454. sysbus_init_mmio_region(dev, &s->iomem);
  455. sysbus_init_irq(dev, &s->irq);
  456. /* Init the codec */
  457. lm4549_init(&s->codec, &pl041_request_data, (void *)s);
  458. return 0;
  459. }
  460. static const VMStateDescription vmstate_pl041_regfile = {
  461. .name = "pl041_regfile",
  462. .version_id = 1,
  463. .minimum_version_id = 1,
  464. .minimum_version_id_old = 1,
  465. .fields = (VMStateField[]) {
  466. #define REGISTER(name, offset) VMSTATE_UINT32(name, pl041_regfile),
  467. #include "pl041.hx"
  468. #undef REGISTER
  469. VMSTATE_END_OF_LIST()
  470. }
  471. };
  472. static const VMStateDescription vmstate_pl041_fifo = {
  473. .name = "pl041_fifo",
  474. .version_id = 1,
  475. .minimum_version_id = 1,
  476. .minimum_version_id_old = 1,
  477. .fields = (VMStateField[]) {
  478. VMSTATE_UINT32(level, pl041_fifo),
  479. VMSTATE_UINT32_ARRAY(data, pl041_fifo, MAX_FIFO_DEPTH),
  480. VMSTATE_END_OF_LIST()
  481. }
  482. };
  483. static const VMStateDescription vmstate_pl041_channel = {
  484. .name = "pl041_channel",
  485. .version_id = 1,
  486. .minimum_version_id = 1,
  487. .minimum_version_id_old = 1,
  488. .fields = (VMStateField[]) {
  489. VMSTATE_STRUCT(tx_fifo, pl041_channel, 0,
  490. vmstate_pl041_fifo, pl041_fifo),
  491. VMSTATE_UINT8(tx_enabled, pl041_channel),
  492. VMSTATE_UINT8(tx_compact_mode, pl041_channel),
  493. VMSTATE_UINT8(tx_sample_size, pl041_channel),
  494. VMSTATE_STRUCT(rx_fifo, pl041_channel, 0,
  495. vmstate_pl041_fifo, pl041_fifo),
  496. VMSTATE_UINT8(rx_enabled, pl041_channel),
  497. VMSTATE_UINT8(rx_compact_mode, pl041_channel),
  498. VMSTATE_UINT8(rx_sample_size, pl041_channel),
  499. VMSTATE_END_OF_LIST()
  500. }
  501. };
  502. static const VMStateDescription vmstate_pl041 = {
  503. .name = "pl041",
  504. .version_id = 1,
  505. .minimum_version_id = 1,
  506. .fields = (VMStateField[]) {
  507. VMSTATE_UINT32(fifo_depth, pl041_state),
  508. VMSTATE_STRUCT(regs, pl041_state, 0,
  509. vmstate_pl041_regfile, pl041_regfile),
  510. VMSTATE_STRUCT(fifo1, pl041_state, 0,
  511. vmstate_pl041_channel, pl041_channel),
  512. VMSTATE_STRUCT(codec, pl041_state, 0,
  513. vmstate_lm4549_state, lm4549_state),
  514. VMSTATE_END_OF_LIST()
  515. }
  516. };
  517. static SysBusDeviceInfo pl041_device_info = {
  518. .init = pl041_init,
  519. .qdev.name = "pl041",
  520. .qdev.size = sizeof(pl041_state),
  521. .qdev.vmsd = &vmstate_pl041,
  522. .qdev.reset = pl041_device_reset,
  523. .qdev.no_user = 1,
  524. .qdev.props = (Property[]) {
  525. /* Non-compact FIFO depth property */
  526. DEFINE_PROP_UINT32("nc_fifo_depth", pl041_state,
  527. fifo_depth, DEFAULT_FIFO_DEPTH),
  528. DEFINE_PROP_END_OF_LIST(),
  529. },
  530. };
  531. static void pl041_register_device(void)
  532. {
  533. sysbus_register_withprop(&pl041_device_info);
  534. }
  535. device_init(pl041_register_device)