2
0

pl041.c 17 KB

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