apple-admac.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Audio DMA Controller (ADMAC) on t8103 (M1) and other Apple chips
  4. *
  5. * Copyright (C) The Asahi Linux Contributors
  6. */
  7. #include <linux/bits.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/device.h>
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_dma.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/reset.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/interrupt.h>
  18. #include "dmaengine.h"
  19. #define NCHANNELS_MAX 64
  20. #define IRQ_NOUTPUTS 4
  21. /*
  22. * For allocation purposes we split the cache
  23. * memory into blocks of fixed size (given in bytes).
  24. */
  25. #define SRAM_BLOCK 2048
  26. #define RING_WRITE_SLOT GENMASK(1, 0)
  27. #define RING_READ_SLOT GENMASK(5, 4)
  28. #define RING_FULL BIT(9)
  29. #define RING_EMPTY BIT(8)
  30. #define RING_ERR BIT(10)
  31. #define STATUS_DESC_DONE BIT(0)
  32. #define STATUS_ERR BIT(6)
  33. #define FLAG_DESC_NOTIFY BIT(16)
  34. #define REG_TX_START 0x0000
  35. #define REG_TX_STOP 0x0004
  36. #define REG_RX_START 0x0008
  37. #define REG_RX_STOP 0x000c
  38. #define REG_IMPRINT 0x0090
  39. #define REG_TX_SRAM_SIZE 0x0094
  40. #define REG_RX_SRAM_SIZE 0x0098
  41. #define REG_CHAN_CTL(ch) (0x8000 + (ch) * 0x200)
  42. #define REG_CHAN_CTL_RST_RINGS BIT(0)
  43. #define REG_DESC_RING(ch) (0x8070 + (ch) * 0x200)
  44. #define REG_REPORT_RING(ch) (0x8074 + (ch) * 0x200)
  45. #define REG_RESIDUE(ch) (0x8064 + (ch) * 0x200)
  46. #define REG_BUS_WIDTH(ch) (0x8040 + (ch) * 0x200)
  47. #define BUS_WIDTH_WORD_SIZE GENMASK(3, 0)
  48. #define BUS_WIDTH_FRAME_SIZE GENMASK(7, 4)
  49. #define BUS_WIDTH_8BIT 0x00
  50. #define BUS_WIDTH_16BIT 0x01
  51. #define BUS_WIDTH_32BIT 0x02
  52. #define BUS_WIDTH_FRAME_2_WORDS 0x10
  53. #define BUS_WIDTH_FRAME_4_WORDS 0x20
  54. #define REG_CHAN_SRAM_CARVEOUT(ch) (0x8050 + (ch) * 0x200)
  55. #define CHAN_SRAM_CARVEOUT_SIZE GENMASK(31, 16)
  56. #define CHAN_SRAM_CARVEOUT_BASE GENMASK(15, 0)
  57. #define REG_CHAN_FIFOCTL(ch) (0x8054 + (ch) * 0x200)
  58. #define CHAN_FIFOCTL_LIMIT GENMASK(31, 16)
  59. #define CHAN_FIFOCTL_THRESHOLD GENMASK(15, 0)
  60. #define REG_DESC_WRITE(ch) (0x10000 + ((ch) / 2) * 0x4 + ((ch) & 1) * 0x4000)
  61. #define REG_REPORT_READ(ch) (0x10100 + ((ch) / 2) * 0x4 + ((ch) & 1) * 0x4000)
  62. #define REG_TX_INTSTATE(idx) (0x0030 + (idx) * 4)
  63. #define REG_RX_INTSTATE(idx) (0x0040 + (idx) * 4)
  64. #define REG_GLOBAL_INTSTATE(idx) (0x0050 + (idx) * 4)
  65. #define REG_CHAN_INTSTATUS(ch, idx) (0x8010 + (ch) * 0x200 + (idx) * 4)
  66. #define REG_CHAN_INTMASK(ch, idx) (0x8020 + (ch) * 0x200 + (idx) * 4)
  67. struct admac_data;
  68. struct admac_tx;
  69. struct admac_chan {
  70. unsigned int no;
  71. struct admac_data *host;
  72. struct dma_chan chan;
  73. struct tasklet_struct tasklet;
  74. u32 carveout;
  75. spinlock_t lock;
  76. struct admac_tx *current_tx;
  77. int nperiod_acks;
  78. /*
  79. * We maintain a 'submitted' and 'issued' list mainly for interface
  80. * correctness. Typical use of the driver (per channel) will be
  81. * prepping, submitting and issuing a single cyclic transaction which
  82. * will stay current until terminate_all is called.
  83. */
  84. struct list_head submitted;
  85. struct list_head issued;
  86. struct list_head to_free;
  87. };
  88. struct admac_sram {
  89. u32 size;
  90. /*
  91. * SRAM_CARVEOUT has 16-bit fields, so the SRAM cannot be larger than
  92. * 64K and a 32-bit bitfield over 2K blocks covers it.
  93. */
  94. u32 allocated;
  95. };
  96. struct admac_data {
  97. struct dma_device dma;
  98. struct device *dev;
  99. __iomem void *base;
  100. struct reset_control *rstc;
  101. struct mutex cache_alloc_lock;
  102. struct admac_sram txcache, rxcache;
  103. int irq;
  104. int irq_index;
  105. int nchannels;
  106. struct admac_chan channels[] __counted_by(nchannels);
  107. };
  108. struct admac_tx {
  109. struct dma_async_tx_descriptor tx;
  110. bool cyclic;
  111. dma_addr_t buf_addr;
  112. dma_addr_t buf_end;
  113. size_t buf_len;
  114. size_t period_len;
  115. size_t submitted_pos;
  116. size_t reclaimed_pos;
  117. struct list_head node;
  118. };
  119. static int admac_alloc_sram_carveout(struct admac_data *ad,
  120. enum dma_transfer_direction dir,
  121. u32 *out)
  122. {
  123. struct admac_sram *sram;
  124. int i, ret = 0, nblocks;
  125. ad->txcache.size = readl_relaxed(ad->base + REG_TX_SRAM_SIZE);
  126. ad->rxcache.size = readl_relaxed(ad->base + REG_RX_SRAM_SIZE);
  127. if (dir == DMA_MEM_TO_DEV)
  128. sram = &ad->txcache;
  129. else
  130. sram = &ad->rxcache;
  131. mutex_lock(&ad->cache_alloc_lock);
  132. nblocks = sram->size / SRAM_BLOCK;
  133. for (i = 0; i < nblocks; i++)
  134. if (!(sram->allocated & BIT(i)))
  135. break;
  136. if (i < nblocks) {
  137. *out = FIELD_PREP(CHAN_SRAM_CARVEOUT_BASE, i * SRAM_BLOCK) |
  138. FIELD_PREP(CHAN_SRAM_CARVEOUT_SIZE, SRAM_BLOCK);
  139. sram->allocated |= BIT(i);
  140. } else {
  141. ret = -EBUSY;
  142. }
  143. mutex_unlock(&ad->cache_alloc_lock);
  144. return ret;
  145. }
  146. static void admac_free_sram_carveout(struct admac_data *ad,
  147. enum dma_transfer_direction dir,
  148. u32 carveout)
  149. {
  150. struct admac_sram *sram;
  151. u32 base = FIELD_GET(CHAN_SRAM_CARVEOUT_BASE, carveout);
  152. int i;
  153. if (dir == DMA_MEM_TO_DEV)
  154. sram = &ad->txcache;
  155. else
  156. sram = &ad->rxcache;
  157. if (WARN_ON(base >= sram->size))
  158. return;
  159. mutex_lock(&ad->cache_alloc_lock);
  160. i = base / SRAM_BLOCK;
  161. sram->allocated &= ~BIT(i);
  162. mutex_unlock(&ad->cache_alloc_lock);
  163. }
  164. static void admac_modify(struct admac_data *ad, int reg, u32 mask, u32 val)
  165. {
  166. void __iomem *addr = ad->base + reg;
  167. u32 curr = readl_relaxed(addr);
  168. writel_relaxed((curr & ~mask) | (val & mask), addr);
  169. }
  170. static struct admac_chan *to_admac_chan(struct dma_chan *chan)
  171. {
  172. return container_of(chan, struct admac_chan, chan);
  173. }
  174. static struct admac_tx *to_admac_tx(struct dma_async_tx_descriptor *tx)
  175. {
  176. return container_of(tx, struct admac_tx, tx);
  177. }
  178. static enum dma_transfer_direction admac_chan_direction(int channo)
  179. {
  180. /* Channel directions are hardwired */
  181. return (channo & 1) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
  182. }
  183. static dma_cookie_t admac_tx_submit(struct dma_async_tx_descriptor *tx)
  184. {
  185. struct admac_tx *adtx = to_admac_tx(tx);
  186. struct admac_chan *adchan = to_admac_chan(tx->chan);
  187. unsigned long flags;
  188. dma_cookie_t cookie;
  189. spin_lock_irqsave(&adchan->lock, flags);
  190. cookie = dma_cookie_assign(tx);
  191. list_add_tail(&adtx->node, &adchan->submitted);
  192. spin_unlock_irqrestore(&adchan->lock, flags);
  193. return cookie;
  194. }
  195. static int admac_desc_free(struct dma_async_tx_descriptor *tx)
  196. {
  197. kfree(to_admac_tx(tx));
  198. return 0;
  199. }
  200. static struct dma_async_tx_descriptor *admac_prep_dma_cyclic(
  201. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  202. size_t period_len, enum dma_transfer_direction direction,
  203. unsigned long flags)
  204. {
  205. struct admac_chan *adchan = container_of(chan, struct admac_chan, chan);
  206. struct admac_tx *adtx;
  207. if (direction != admac_chan_direction(adchan->no))
  208. return NULL;
  209. adtx = kzalloc(sizeof(*adtx), GFP_NOWAIT);
  210. if (!adtx)
  211. return NULL;
  212. adtx->cyclic = true;
  213. adtx->buf_addr = buf_addr;
  214. adtx->buf_len = buf_len;
  215. adtx->buf_end = buf_addr + buf_len;
  216. adtx->period_len = period_len;
  217. adtx->submitted_pos = 0;
  218. adtx->reclaimed_pos = 0;
  219. dma_async_tx_descriptor_init(&adtx->tx, chan);
  220. adtx->tx.tx_submit = admac_tx_submit;
  221. adtx->tx.desc_free = admac_desc_free;
  222. return &adtx->tx;
  223. }
  224. /*
  225. * Write one hardware descriptor for a dmaengine cyclic transaction.
  226. */
  227. static void admac_cyclic_write_one_desc(struct admac_data *ad, int channo,
  228. struct admac_tx *tx)
  229. {
  230. dma_addr_t addr;
  231. addr = tx->buf_addr + (tx->submitted_pos % tx->buf_len);
  232. /* If happens means we have buggy code */
  233. WARN_ON_ONCE(addr + tx->period_len > tx->buf_end);
  234. dev_dbg(ad->dev, "ch%d descriptor: addr=0x%pad len=0x%zx flags=0x%lx\n",
  235. channo, &addr, tx->period_len, FLAG_DESC_NOTIFY);
  236. writel_relaxed(lower_32_bits(addr), ad->base + REG_DESC_WRITE(channo));
  237. writel_relaxed(upper_32_bits(addr), ad->base + REG_DESC_WRITE(channo));
  238. writel_relaxed(tx->period_len, ad->base + REG_DESC_WRITE(channo));
  239. writel_relaxed(FLAG_DESC_NOTIFY, ad->base + REG_DESC_WRITE(channo));
  240. tx->submitted_pos += tx->period_len;
  241. tx->submitted_pos %= 2 * tx->buf_len;
  242. }
  243. /*
  244. * Write all the hardware descriptors for a dmaengine cyclic
  245. * transaction there is space for.
  246. */
  247. static void admac_cyclic_write_desc(struct admac_data *ad, int channo,
  248. struct admac_tx *tx)
  249. {
  250. int i;
  251. for (i = 0; i < 4; i++) {
  252. if (readl_relaxed(ad->base + REG_DESC_RING(channo)) & RING_FULL)
  253. break;
  254. admac_cyclic_write_one_desc(ad, channo, tx);
  255. }
  256. }
  257. static int admac_ring_noccupied_slots(int ringval)
  258. {
  259. int wrslot = FIELD_GET(RING_WRITE_SLOT, ringval);
  260. int rdslot = FIELD_GET(RING_READ_SLOT, ringval);
  261. if (wrslot != rdslot) {
  262. return (wrslot + 4 - rdslot) % 4;
  263. } else {
  264. WARN_ON((ringval & (RING_FULL | RING_EMPTY)) == 0);
  265. if (ringval & RING_FULL)
  266. return 4;
  267. else
  268. return 0;
  269. }
  270. }
  271. /*
  272. * Read from hardware the residue of a cyclic dmaengine transaction.
  273. */
  274. static u32 admac_cyclic_read_residue(struct admac_data *ad, int channo,
  275. struct admac_tx *adtx)
  276. {
  277. u32 ring1, ring2;
  278. u32 residue1, residue2;
  279. int nreports;
  280. size_t pos;
  281. ring1 = readl_relaxed(ad->base + REG_REPORT_RING(channo));
  282. residue1 = readl_relaxed(ad->base + REG_RESIDUE(channo));
  283. ring2 = readl_relaxed(ad->base + REG_REPORT_RING(channo));
  284. residue2 = readl_relaxed(ad->base + REG_RESIDUE(channo));
  285. if (residue2 > residue1) {
  286. /*
  287. * Controller must have loaded next descriptor between
  288. * the two residue reads
  289. */
  290. nreports = admac_ring_noccupied_slots(ring1) + 1;
  291. } else {
  292. /* No descriptor load between the two reads, ring2 is safe to use */
  293. nreports = admac_ring_noccupied_slots(ring2);
  294. }
  295. pos = adtx->reclaimed_pos + adtx->period_len * (nreports + 1) - residue2;
  296. return adtx->buf_len - pos % adtx->buf_len;
  297. }
  298. static enum dma_status admac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
  299. struct dma_tx_state *txstate)
  300. {
  301. struct admac_chan *adchan = to_admac_chan(chan);
  302. struct admac_data *ad = adchan->host;
  303. struct admac_tx *adtx;
  304. enum dma_status ret;
  305. size_t residue;
  306. unsigned long flags;
  307. ret = dma_cookie_status(chan, cookie, txstate);
  308. if (ret == DMA_COMPLETE || !txstate)
  309. return ret;
  310. spin_lock_irqsave(&adchan->lock, flags);
  311. adtx = adchan->current_tx;
  312. if (adtx && adtx->tx.cookie == cookie) {
  313. ret = DMA_IN_PROGRESS;
  314. residue = admac_cyclic_read_residue(ad, adchan->no, adtx);
  315. } else {
  316. ret = DMA_IN_PROGRESS;
  317. residue = 0;
  318. list_for_each_entry(adtx, &adchan->issued, node) {
  319. if (adtx->tx.cookie == cookie) {
  320. residue = adtx->buf_len;
  321. break;
  322. }
  323. }
  324. }
  325. spin_unlock_irqrestore(&adchan->lock, flags);
  326. dma_set_residue(txstate, residue);
  327. return ret;
  328. }
  329. static void admac_start_chan(struct admac_chan *adchan)
  330. {
  331. struct admac_data *ad = adchan->host;
  332. u32 startbit = 1 << (adchan->no / 2);
  333. writel_relaxed(STATUS_DESC_DONE | STATUS_ERR,
  334. ad->base + REG_CHAN_INTSTATUS(adchan->no, ad->irq_index));
  335. writel_relaxed(STATUS_DESC_DONE | STATUS_ERR,
  336. ad->base + REG_CHAN_INTMASK(adchan->no, ad->irq_index));
  337. switch (admac_chan_direction(adchan->no)) {
  338. case DMA_MEM_TO_DEV:
  339. writel_relaxed(startbit, ad->base + REG_TX_START);
  340. break;
  341. case DMA_DEV_TO_MEM:
  342. writel_relaxed(startbit, ad->base + REG_RX_START);
  343. break;
  344. default:
  345. break;
  346. }
  347. dev_dbg(adchan->host->dev, "ch%d start\n", adchan->no);
  348. }
  349. static void admac_stop_chan(struct admac_chan *adchan)
  350. {
  351. struct admac_data *ad = adchan->host;
  352. u32 stopbit = 1 << (adchan->no / 2);
  353. switch (admac_chan_direction(adchan->no)) {
  354. case DMA_MEM_TO_DEV:
  355. writel_relaxed(stopbit, ad->base + REG_TX_STOP);
  356. break;
  357. case DMA_DEV_TO_MEM:
  358. writel_relaxed(stopbit, ad->base + REG_RX_STOP);
  359. break;
  360. default:
  361. break;
  362. }
  363. dev_dbg(adchan->host->dev, "ch%d stop\n", adchan->no);
  364. }
  365. static void admac_reset_rings(struct admac_chan *adchan)
  366. {
  367. struct admac_data *ad = adchan->host;
  368. writel_relaxed(REG_CHAN_CTL_RST_RINGS,
  369. ad->base + REG_CHAN_CTL(adchan->no));
  370. writel_relaxed(0, ad->base + REG_CHAN_CTL(adchan->no));
  371. }
  372. static void admac_start_current_tx(struct admac_chan *adchan)
  373. {
  374. struct admac_data *ad = adchan->host;
  375. int ch = adchan->no;
  376. admac_reset_rings(adchan);
  377. writel_relaxed(0, ad->base + REG_CHAN_CTL(ch));
  378. admac_cyclic_write_one_desc(ad, ch, adchan->current_tx);
  379. admac_start_chan(adchan);
  380. admac_cyclic_write_desc(ad, ch, adchan->current_tx);
  381. }
  382. static void admac_issue_pending(struct dma_chan *chan)
  383. {
  384. struct admac_chan *adchan = to_admac_chan(chan);
  385. struct admac_tx *tx;
  386. unsigned long flags;
  387. spin_lock_irqsave(&adchan->lock, flags);
  388. list_splice_tail_init(&adchan->submitted, &adchan->issued);
  389. if (!list_empty(&adchan->issued) && !adchan->current_tx) {
  390. tx = list_first_entry(&adchan->issued, struct admac_tx, node);
  391. list_del(&tx->node);
  392. adchan->current_tx = tx;
  393. adchan->nperiod_acks = 0;
  394. admac_start_current_tx(adchan);
  395. }
  396. spin_unlock_irqrestore(&adchan->lock, flags);
  397. }
  398. static int admac_pause(struct dma_chan *chan)
  399. {
  400. struct admac_chan *adchan = to_admac_chan(chan);
  401. admac_stop_chan(adchan);
  402. return 0;
  403. }
  404. static int admac_resume(struct dma_chan *chan)
  405. {
  406. struct admac_chan *adchan = to_admac_chan(chan);
  407. admac_start_chan(adchan);
  408. return 0;
  409. }
  410. static int admac_terminate_all(struct dma_chan *chan)
  411. {
  412. struct admac_chan *adchan = to_admac_chan(chan);
  413. unsigned long flags;
  414. spin_lock_irqsave(&adchan->lock, flags);
  415. admac_stop_chan(adchan);
  416. admac_reset_rings(adchan);
  417. if (adchan->current_tx) {
  418. list_add_tail(&adchan->current_tx->node, &adchan->to_free);
  419. adchan->current_tx = NULL;
  420. }
  421. /*
  422. * Descriptors can only be freed after the tasklet
  423. * has been killed (in admac_synchronize).
  424. */
  425. list_splice_tail_init(&adchan->submitted, &adchan->to_free);
  426. list_splice_tail_init(&adchan->issued, &adchan->to_free);
  427. spin_unlock_irqrestore(&adchan->lock, flags);
  428. return 0;
  429. }
  430. static void admac_synchronize(struct dma_chan *chan)
  431. {
  432. struct admac_chan *adchan = to_admac_chan(chan);
  433. struct admac_tx *adtx, *_adtx;
  434. unsigned long flags;
  435. LIST_HEAD(head);
  436. spin_lock_irqsave(&adchan->lock, flags);
  437. list_splice_tail_init(&adchan->to_free, &head);
  438. spin_unlock_irqrestore(&adchan->lock, flags);
  439. tasklet_kill(&adchan->tasklet);
  440. list_for_each_entry_safe(adtx, _adtx, &head, node) {
  441. list_del(&adtx->node);
  442. admac_desc_free(&adtx->tx);
  443. }
  444. }
  445. static int admac_alloc_chan_resources(struct dma_chan *chan)
  446. {
  447. struct admac_chan *adchan = to_admac_chan(chan);
  448. struct admac_data *ad = adchan->host;
  449. int ret;
  450. dma_cookie_init(&adchan->chan);
  451. ret = admac_alloc_sram_carveout(ad, admac_chan_direction(adchan->no),
  452. &adchan->carveout);
  453. if (ret < 0)
  454. return ret;
  455. writel_relaxed(adchan->carveout,
  456. ad->base + REG_CHAN_SRAM_CARVEOUT(adchan->no));
  457. return 0;
  458. }
  459. static void admac_free_chan_resources(struct dma_chan *chan)
  460. {
  461. struct admac_chan *adchan = to_admac_chan(chan);
  462. admac_terminate_all(chan);
  463. admac_synchronize(chan);
  464. admac_free_sram_carveout(adchan->host, admac_chan_direction(adchan->no),
  465. adchan->carveout);
  466. }
  467. static struct dma_chan *admac_dma_of_xlate(struct of_phandle_args *dma_spec,
  468. struct of_dma *ofdma)
  469. {
  470. struct admac_data *ad = (struct admac_data *) ofdma->of_dma_data;
  471. unsigned int index;
  472. if (dma_spec->args_count != 1)
  473. return NULL;
  474. index = dma_spec->args[0];
  475. if (index >= ad->nchannels) {
  476. dev_err(ad->dev, "channel index %u out of bounds\n", index);
  477. return NULL;
  478. }
  479. return dma_get_slave_channel(&ad->channels[index].chan);
  480. }
  481. static int admac_drain_reports(struct admac_data *ad, int channo)
  482. {
  483. int count;
  484. for (count = 0; count < 4; count++) {
  485. u32 countval_hi, countval_lo, unk1, flags;
  486. if (readl_relaxed(ad->base + REG_REPORT_RING(channo)) & RING_EMPTY)
  487. break;
  488. countval_lo = readl_relaxed(ad->base + REG_REPORT_READ(channo));
  489. countval_hi = readl_relaxed(ad->base + REG_REPORT_READ(channo));
  490. unk1 = readl_relaxed(ad->base + REG_REPORT_READ(channo));
  491. flags = readl_relaxed(ad->base + REG_REPORT_READ(channo));
  492. dev_dbg(ad->dev, "ch%d report: countval=0x%llx unk1=0x%x flags=0x%x\n",
  493. channo, ((u64) countval_hi) << 32 | countval_lo, unk1, flags);
  494. }
  495. return count;
  496. }
  497. static void admac_handle_status_err(struct admac_data *ad, int channo)
  498. {
  499. bool handled = false;
  500. if (readl_relaxed(ad->base + REG_DESC_RING(channo)) & RING_ERR) {
  501. writel_relaxed(RING_ERR, ad->base + REG_DESC_RING(channo));
  502. dev_err_ratelimited(ad->dev, "ch%d descriptor ring error\n", channo);
  503. handled = true;
  504. }
  505. if (readl_relaxed(ad->base + REG_REPORT_RING(channo)) & RING_ERR) {
  506. writel_relaxed(RING_ERR, ad->base + REG_REPORT_RING(channo));
  507. dev_err_ratelimited(ad->dev, "ch%d report ring error\n", channo);
  508. handled = true;
  509. }
  510. if (unlikely(!handled)) {
  511. dev_err(ad->dev, "ch%d unknown error, masking errors as cause of IRQs\n", channo);
  512. admac_modify(ad, REG_CHAN_INTMASK(channo, ad->irq_index),
  513. STATUS_ERR, 0);
  514. }
  515. }
  516. static void admac_handle_status_desc_done(struct admac_data *ad, int channo)
  517. {
  518. struct admac_chan *adchan = &ad->channels[channo];
  519. unsigned long flags;
  520. int nreports;
  521. writel_relaxed(STATUS_DESC_DONE,
  522. ad->base + REG_CHAN_INTSTATUS(channo, ad->irq_index));
  523. spin_lock_irqsave(&adchan->lock, flags);
  524. nreports = admac_drain_reports(ad, channo);
  525. if (adchan->current_tx) {
  526. struct admac_tx *tx = adchan->current_tx;
  527. adchan->nperiod_acks += nreports;
  528. tx->reclaimed_pos += nreports * tx->period_len;
  529. tx->reclaimed_pos %= 2 * tx->buf_len;
  530. admac_cyclic_write_desc(ad, channo, tx);
  531. tasklet_schedule(&adchan->tasklet);
  532. }
  533. spin_unlock_irqrestore(&adchan->lock, flags);
  534. }
  535. static void admac_handle_chan_int(struct admac_data *ad, int no)
  536. {
  537. u32 cause = readl_relaxed(ad->base + REG_CHAN_INTSTATUS(no, ad->irq_index));
  538. if (cause & STATUS_ERR)
  539. admac_handle_status_err(ad, no);
  540. if (cause & STATUS_DESC_DONE)
  541. admac_handle_status_desc_done(ad, no);
  542. }
  543. static irqreturn_t admac_interrupt(int irq, void *devid)
  544. {
  545. struct admac_data *ad = devid;
  546. u32 rx_intstate, tx_intstate, global_intstate;
  547. int i;
  548. rx_intstate = readl_relaxed(ad->base + REG_RX_INTSTATE(ad->irq_index));
  549. tx_intstate = readl_relaxed(ad->base + REG_TX_INTSTATE(ad->irq_index));
  550. global_intstate = readl_relaxed(ad->base + REG_GLOBAL_INTSTATE(ad->irq_index));
  551. if (!tx_intstate && !rx_intstate && !global_intstate)
  552. return IRQ_NONE;
  553. for (i = 0; i < ad->nchannels; i += 2) {
  554. if (tx_intstate & 1)
  555. admac_handle_chan_int(ad, i);
  556. tx_intstate >>= 1;
  557. }
  558. for (i = 1; i < ad->nchannels; i += 2) {
  559. if (rx_intstate & 1)
  560. admac_handle_chan_int(ad, i);
  561. rx_intstate >>= 1;
  562. }
  563. if (global_intstate) {
  564. dev_warn(ad->dev, "clearing unknown global interrupt flag: %x\n",
  565. global_intstate);
  566. writel_relaxed(~(u32) 0, ad->base + REG_GLOBAL_INTSTATE(ad->irq_index));
  567. }
  568. return IRQ_HANDLED;
  569. }
  570. static void admac_chan_tasklet(struct tasklet_struct *t)
  571. {
  572. struct admac_chan *adchan = from_tasklet(adchan, t, tasklet);
  573. struct admac_tx *adtx;
  574. struct dmaengine_desc_callback cb;
  575. struct dmaengine_result tx_result;
  576. int nacks;
  577. spin_lock_irq(&adchan->lock);
  578. adtx = adchan->current_tx;
  579. nacks = adchan->nperiod_acks;
  580. adchan->nperiod_acks = 0;
  581. spin_unlock_irq(&adchan->lock);
  582. if (!adtx || !nacks)
  583. return;
  584. tx_result.result = DMA_TRANS_NOERROR;
  585. tx_result.residue = 0;
  586. dmaengine_desc_get_callback(&adtx->tx, &cb);
  587. while (nacks--)
  588. dmaengine_desc_callback_invoke(&cb, &tx_result);
  589. }
  590. static int admac_device_config(struct dma_chan *chan,
  591. struct dma_slave_config *config)
  592. {
  593. struct admac_chan *adchan = to_admac_chan(chan);
  594. struct admac_data *ad = adchan->host;
  595. bool is_tx = admac_chan_direction(adchan->no) == DMA_MEM_TO_DEV;
  596. int wordsize = 0;
  597. u32 bus_width = readl_relaxed(ad->base + REG_BUS_WIDTH(adchan->no)) &
  598. ~(BUS_WIDTH_WORD_SIZE | BUS_WIDTH_FRAME_SIZE);
  599. switch (is_tx ? config->dst_addr_width : config->src_addr_width) {
  600. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  601. wordsize = 1;
  602. bus_width |= BUS_WIDTH_8BIT;
  603. break;
  604. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  605. wordsize = 2;
  606. bus_width |= BUS_WIDTH_16BIT;
  607. break;
  608. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  609. wordsize = 4;
  610. bus_width |= BUS_WIDTH_32BIT;
  611. break;
  612. default:
  613. return -EINVAL;
  614. }
  615. /*
  616. * We take port_window_size to be the number of words in a frame.
  617. *
  618. * The controller has some means of out-of-band signalling, to the peripheral,
  619. * of words position in a frame. That's where the importance of this control
  620. * comes from.
  621. */
  622. switch (is_tx ? config->dst_port_window_size : config->src_port_window_size) {
  623. case 0 ... 1:
  624. break;
  625. case 2:
  626. bus_width |= BUS_WIDTH_FRAME_2_WORDS;
  627. break;
  628. case 4:
  629. bus_width |= BUS_WIDTH_FRAME_4_WORDS;
  630. break;
  631. default:
  632. return -EINVAL;
  633. }
  634. writel_relaxed(bus_width, ad->base + REG_BUS_WIDTH(adchan->no));
  635. /*
  636. * By FIFOCTL_LIMIT we seem to set the maximal number of bytes allowed to be
  637. * held in controller's per-channel FIFO. Transfers seem to be triggered
  638. * around the time FIFO occupancy touches FIFOCTL_THRESHOLD.
  639. *
  640. * The numbers we set are more or less arbitrary.
  641. */
  642. writel_relaxed(FIELD_PREP(CHAN_FIFOCTL_LIMIT, 0x30 * wordsize)
  643. | FIELD_PREP(CHAN_FIFOCTL_THRESHOLD, 0x18 * wordsize),
  644. ad->base + REG_CHAN_FIFOCTL(adchan->no));
  645. return 0;
  646. }
  647. static int admac_probe(struct platform_device *pdev)
  648. {
  649. struct device_node *np = pdev->dev.of_node;
  650. struct admac_data *ad;
  651. struct dma_device *dma;
  652. int nchannels;
  653. int err, irq, i;
  654. err = of_property_read_u32(np, "dma-channels", &nchannels);
  655. if (err || nchannels > NCHANNELS_MAX) {
  656. dev_err(&pdev->dev, "missing or invalid dma-channels property\n");
  657. return -EINVAL;
  658. }
  659. ad = devm_kzalloc(&pdev->dev, struct_size(ad, channels, nchannels), GFP_KERNEL);
  660. if (!ad)
  661. return -ENOMEM;
  662. platform_set_drvdata(pdev, ad);
  663. ad->dev = &pdev->dev;
  664. ad->nchannels = nchannels;
  665. mutex_init(&ad->cache_alloc_lock);
  666. /*
  667. * The controller has 4 IRQ outputs. Try them all until
  668. * we find one we can use.
  669. */
  670. for (i = 0; i < IRQ_NOUTPUTS; i++) {
  671. irq = platform_get_irq_optional(pdev, i);
  672. if (irq >= 0) {
  673. ad->irq_index = i;
  674. break;
  675. }
  676. }
  677. if (irq < 0)
  678. return dev_err_probe(&pdev->dev, irq, "no usable interrupt\n");
  679. ad->irq = irq;
  680. ad->base = devm_platform_ioremap_resource(pdev, 0);
  681. if (IS_ERR(ad->base))
  682. return dev_err_probe(&pdev->dev, PTR_ERR(ad->base),
  683. "unable to obtain MMIO resource\n");
  684. ad->rstc = devm_reset_control_get_optional_shared(&pdev->dev, NULL);
  685. if (IS_ERR(ad->rstc))
  686. return PTR_ERR(ad->rstc);
  687. dma = &ad->dma;
  688. dma_cap_set(DMA_PRIVATE, dma->cap_mask);
  689. dma_cap_set(DMA_CYCLIC, dma->cap_mask);
  690. dma->dev = &pdev->dev;
  691. dma->device_alloc_chan_resources = admac_alloc_chan_resources;
  692. dma->device_free_chan_resources = admac_free_chan_resources;
  693. dma->device_tx_status = admac_tx_status;
  694. dma->device_issue_pending = admac_issue_pending;
  695. dma->device_terminate_all = admac_terminate_all;
  696. dma->device_synchronize = admac_synchronize;
  697. dma->device_prep_dma_cyclic = admac_prep_dma_cyclic;
  698. dma->device_config = admac_device_config;
  699. dma->device_pause = admac_pause;
  700. dma->device_resume = admac_resume;
  701. dma->directions = BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
  702. dma->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  703. dma->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  704. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  705. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  706. dma->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
  707. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
  708. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  709. INIT_LIST_HEAD(&dma->channels);
  710. for (i = 0; i < nchannels; i++) {
  711. struct admac_chan *adchan = &ad->channels[i];
  712. adchan->host = ad;
  713. adchan->no = i;
  714. adchan->chan.device = &ad->dma;
  715. spin_lock_init(&adchan->lock);
  716. INIT_LIST_HEAD(&adchan->submitted);
  717. INIT_LIST_HEAD(&adchan->issued);
  718. INIT_LIST_HEAD(&adchan->to_free);
  719. list_add_tail(&adchan->chan.device_node, &dma->channels);
  720. tasklet_setup(&adchan->tasklet, admac_chan_tasklet);
  721. }
  722. err = reset_control_reset(ad->rstc);
  723. if (err)
  724. return dev_err_probe(&pdev->dev, err,
  725. "unable to trigger reset\n");
  726. err = request_irq(irq, admac_interrupt, 0, dev_name(&pdev->dev), ad);
  727. if (err) {
  728. dev_err_probe(&pdev->dev, err,
  729. "unable to register interrupt\n");
  730. goto free_reset;
  731. }
  732. err = dma_async_device_register(&ad->dma);
  733. if (err) {
  734. dev_err_probe(&pdev->dev, err, "failed to register DMA device\n");
  735. goto free_irq;
  736. }
  737. err = of_dma_controller_register(pdev->dev.of_node, admac_dma_of_xlate, ad);
  738. if (err) {
  739. dma_async_device_unregister(&ad->dma);
  740. dev_err_probe(&pdev->dev, err, "failed to register with OF\n");
  741. goto free_irq;
  742. }
  743. dev_info(&pdev->dev, "Audio DMA Controller\n");
  744. return 0;
  745. free_irq:
  746. free_irq(ad->irq, ad);
  747. free_reset:
  748. reset_control_rearm(ad->rstc);
  749. return err;
  750. }
  751. static void admac_remove(struct platform_device *pdev)
  752. {
  753. struct admac_data *ad = platform_get_drvdata(pdev);
  754. of_dma_controller_free(pdev->dev.of_node);
  755. dma_async_device_unregister(&ad->dma);
  756. free_irq(ad->irq, ad);
  757. reset_control_rearm(ad->rstc);
  758. }
  759. static const struct of_device_id admac_of_match[] = {
  760. { .compatible = "apple,admac", },
  761. { }
  762. };
  763. MODULE_DEVICE_TABLE(of, admac_of_match);
  764. static struct platform_driver apple_admac_driver = {
  765. .driver = {
  766. .name = "apple-admac",
  767. .of_match_table = admac_of_match,
  768. },
  769. .probe = admac_probe,
  770. .remove_new = admac_remove,
  771. };
  772. module_platform_driver(apple_admac_driver);
  773. MODULE_AUTHOR("Martin Povišer <povik+lin@cutebit.org>");
  774. MODULE_DESCRIPTION("Driver for Audio DMA Controller (ADMAC) on Apple SoCs");
  775. MODULE_LICENSE("GPL");