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