img-mdc-dma.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * IMG Multi-threaded DMA Controller (MDC)
  3. *
  4. * Copyright (C) 2009,2012,2013 Imagination Technologies Ltd.
  5. * Copyright (C) 2014 Google, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/dmapool.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/irq.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_dma.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/regmap.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include "dmaengine.h"
  29. #include "virt-dma.h"
  30. #define MDC_MAX_DMA_CHANNELS 32
  31. #define MDC_GENERAL_CONFIG 0x000
  32. #define MDC_GENERAL_CONFIG_LIST_IEN BIT(31)
  33. #define MDC_GENERAL_CONFIG_IEN BIT(29)
  34. #define MDC_GENERAL_CONFIG_LEVEL_INT BIT(28)
  35. #define MDC_GENERAL_CONFIG_INC_W BIT(12)
  36. #define MDC_GENERAL_CONFIG_INC_R BIT(8)
  37. #define MDC_GENERAL_CONFIG_PHYSICAL_W BIT(7)
  38. #define MDC_GENERAL_CONFIG_WIDTH_W_SHIFT 4
  39. #define MDC_GENERAL_CONFIG_WIDTH_W_MASK 0x7
  40. #define MDC_GENERAL_CONFIG_PHYSICAL_R BIT(3)
  41. #define MDC_GENERAL_CONFIG_WIDTH_R_SHIFT 0
  42. #define MDC_GENERAL_CONFIG_WIDTH_R_MASK 0x7
  43. #define MDC_READ_PORT_CONFIG 0x004
  44. #define MDC_READ_PORT_CONFIG_STHREAD_SHIFT 28
  45. #define MDC_READ_PORT_CONFIG_STHREAD_MASK 0xf
  46. #define MDC_READ_PORT_CONFIG_RTHREAD_SHIFT 24
  47. #define MDC_READ_PORT_CONFIG_RTHREAD_MASK 0xf
  48. #define MDC_READ_PORT_CONFIG_WTHREAD_SHIFT 16
  49. #define MDC_READ_PORT_CONFIG_WTHREAD_MASK 0xf
  50. #define MDC_READ_PORT_CONFIG_BURST_SIZE_SHIFT 4
  51. #define MDC_READ_PORT_CONFIG_BURST_SIZE_MASK 0xff
  52. #define MDC_READ_PORT_CONFIG_DREQ_ENABLE BIT(1)
  53. #define MDC_READ_ADDRESS 0x008
  54. #define MDC_WRITE_ADDRESS 0x00c
  55. #define MDC_TRANSFER_SIZE 0x010
  56. #define MDC_TRANSFER_SIZE_MASK 0xffffff
  57. #define MDC_LIST_NODE_ADDRESS 0x014
  58. #define MDC_CMDS_PROCESSED 0x018
  59. #define MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT 16
  60. #define MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK 0x3f
  61. #define MDC_CMDS_PROCESSED_INT_ACTIVE BIT(8)
  62. #define MDC_CMDS_PROCESSED_CMDS_DONE_SHIFT 0
  63. #define MDC_CMDS_PROCESSED_CMDS_DONE_MASK 0x3f
  64. #define MDC_CONTROL_AND_STATUS 0x01c
  65. #define MDC_CONTROL_AND_STATUS_CANCEL BIT(20)
  66. #define MDC_CONTROL_AND_STATUS_LIST_EN BIT(4)
  67. #define MDC_CONTROL_AND_STATUS_EN BIT(0)
  68. #define MDC_ACTIVE_TRANSFER_SIZE 0x030
  69. #define MDC_GLOBAL_CONFIG_A 0x900
  70. #define MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_SHIFT 16
  71. #define MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_MASK 0xff
  72. #define MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_SHIFT 8
  73. #define MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_MASK 0xff
  74. #define MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_SHIFT 0
  75. #define MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_MASK 0xff
  76. struct mdc_hw_list_desc {
  77. u32 gen_conf;
  78. u32 readport_conf;
  79. u32 read_addr;
  80. u32 write_addr;
  81. u32 xfer_size;
  82. u32 node_addr;
  83. u32 cmds_done;
  84. u32 ctrl_status;
  85. /*
  86. * Not part of the list descriptor, but instead used by the CPU to
  87. * traverse the list.
  88. */
  89. struct mdc_hw_list_desc *next_desc;
  90. };
  91. struct mdc_tx_desc {
  92. struct mdc_chan *chan;
  93. struct virt_dma_desc vd;
  94. dma_addr_t list_phys;
  95. struct mdc_hw_list_desc *list;
  96. bool cyclic;
  97. bool cmd_loaded;
  98. unsigned int list_len;
  99. unsigned int list_period_len;
  100. size_t list_xfer_size;
  101. unsigned int list_cmds_done;
  102. };
  103. struct mdc_chan {
  104. struct mdc_dma *mdma;
  105. struct virt_dma_chan vc;
  106. struct dma_slave_config config;
  107. struct mdc_tx_desc *desc;
  108. int irq;
  109. unsigned int periph;
  110. unsigned int thread;
  111. unsigned int chan_nr;
  112. };
  113. struct mdc_dma_soc_data {
  114. void (*enable_chan)(struct mdc_chan *mchan);
  115. void (*disable_chan)(struct mdc_chan *mchan);
  116. };
  117. struct mdc_dma {
  118. struct dma_device dma_dev;
  119. void __iomem *regs;
  120. struct clk *clk;
  121. struct dma_pool *desc_pool;
  122. struct regmap *periph_regs;
  123. spinlock_t lock;
  124. unsigned int nr_threads;
  125. unsigned int nr_channels;
  126. unsigned int bus_width;
  127. unsigned int max_burst_mult;
  128. unsigned int max_xfer_size;
  129. const struct mdc_dma_soc_data *soc;
  130. struct mdc_chan channels[MDC_MAX_DMA_CHANNELS];
  131. };
  132. static inline u32 mdc_readl(struct mdc_dma *mdma, u32 reg)
  133. {
  134. return readl(mdma->regs + reg);
  135. }
  136. static inline void mdc_writel(struct mdc_dma *mdma, u32 val, u32 reg)
  137. {
  138. writel(val, mdma->regs + reg);
  139. }
  140. static inline u32 mdc_chan_readl(struct mdc_chan *mchan, u32 reg)
  141. {
  142. return mdc_readl(mchan->mdma, mchan->chan_nr * 0x040 + reg);
  143. }
  144. static inline void mdc_chan_writel(struct mdc_chan *mchan, u32 val, u32 reg)
  145. {
  146. mdc_writel(mchan->mdma, val, mchan->chan_nr * 0x040 + reg);
  147. }
  148. static inline struct mdc_chan *to_mdc_chan(struct dma_chan *c)
  149. {
  150. return container_of(to_virt_chan(c), struct mdc_chan, vc);
  151. }
  152. static inline struct mdc_tx_desc *to_mdc_desc(struct dma_async_tx_descriptor *t)
  153. {
  154. struct virt_dma_desc *vdesc = container_of(t, struct virt_dma_desc, tx);
  155. return container_of(vdesc, struct mdc_tx_desc, vd);
  156. }
  157. static inline struct device *mdma2dev(struct mdc_dma *mdma)
  158. {
  159. return mdma->dma_dev.dev;
  160. }
  161. static inline unsigned int to_mdc_width(unsigned int bytes)
  162. {
  163. return ffs(bytes) - 1;
  164. }
  165. static inline void mdc_set_read_width(struct mdc_hw_list_desc *ldesc,
  166. unsigned int bytes)
  167. {
  168. ldesc->gen_conf |= to_mdc_width(bytes) <<
  169. MDC_GENERAL_CONFIG_WIDTH_R_SHIFT;
  170. }
  171. static inline void mdc_set_write_width(struct mdc_hw_list_desc *ldesc,
  172. unsigned int bytes)
  173. {
  174. ldesc->gen_conf |= to_mdc_width(bytes) <<
  175. MDC_GENERAL_CONFIG_WIDTH_W_SHIFT;
  176. }
  177. static void mdc_list_desc_config(struct mdc_chan *mchan,
  178. struct mdc_hw_list_desc *ldesc,
  179. enum dma_transfer_direction dir,
  180. dma_addr_t src, dma_addr_t dst, size_t len)
  181. {
  182. struct mdc_dma *mdma = mchan->mdma;
  183. unsigned int max_burst, burst_size;
  184. ldesc->gen_conf = MDC_GENERAL_CONFIG_IEN | MDC_GENERAL_CONFIG_LIST_IEN |
  185. MDC_GENERAL_CONFIG_LEVEL_INT | MDC_GENERAL_CONFIG_PHYSICAL_W |
  186. MDC_GENERAL_CONFIG_PHYSICAL_R;
  187. ldesc->readport_conf =
  188. (mchan->thread << MDC_READ_PORT_CONFIG_STHREAD_SHIFT) |
  189. (mchan->thread << MDC_READ_PORT_CONFIG_RTHREAD_SHIFT) |
  190. (mchan->thread << MDC_READ_PORT_CONFIG_WTHREAD_SHIFT);
  191. ldesc->read_addr = src;
  192. ldesc->write_addr = dst;
  193. ldesc->xfer_size = len - 1;
  194. ldesc->node_addr = 0;
  195. ldesc->cmds_done = 0;
  196. ldesc->ctrl_status = MDC_CONTROL_AND_STATUS_LIST_EN |
  197. MDC_CONTROL_AND_STATUS_EN;
  198. ldesc->next_desc = NULL;
  199. if (IS_ALIGNED(dst, mdma->bus_width) &&
  200. IS_ALIGNED(src, mdma->bus_width))
  201. max_burst = mdma->bus_width * mdma->max_burst_mult;
  202. else
  203. max_burst = mdma->bus_width * (mdma->max_burst_mult - 1);
  204. if (dir == DMA_MEM_TO_DEV) {
  205. ldesc->gen_conf |= MDC_GENERAL_CONFIG_INC_R;
  206. ldesc->readport_conf |= MDC_READ_PORT_CONFIG_DREQ_ENABLE;
  207. mdc_set_read_width(ldesc, mdma->bus_width);
  208. mdc_set_write_width(ldesc, mchan->config.dst_addr_width);
  209. burst_size = min(max_burst, mchan->config.dst_maxburst *
  210. mchan->config.dst_addr_width);
  211. } else if (dir == DMA_DEV_TO_MEM) {
  212. ldesc->gen_conf |= MDC_GENERAL_CONFIG_INC_W;
  213. ldesc->readport_conf |= MDC_READ_PORT_CONFIG_DREQ_ENABLE;
  214. mdc_set_read_width(ldesc, mchan->config.src_addr_width);
  215. mdc_set_write_width(ldesc, mdma->bus_width);
  216. burst_size = min(max_burst, mchan->config.src_maxburst *
  217. mchan->config.src_addr_width);
  218. } else {
  219. ldesc->gen_conf |= MDC_GENERAL_CONFIG_INC_R |
  220. MDC_GENERAL_CONFIG_INC_W;
  221. mdc_set_read_width(ldesc, mdma->bus_width);
  222. mdc_set_write_width(ldesc, mdma->bus_width);
  223. burst_size = max_burst;
  224. }
  225. ldesc->readport_conf |= (burst_size - 1) <<
  226. MDC_READ_PORT_CONFIG_BURST_SIZE_SHIFT;
  227. }
  228. static void mdc_list_desc_free(struct mdc_tx_desc *mdesc)
  229. {
  230. struct mdc_dma *mdma = mdesc->chan->mdma;
  231. struct mdc_hw_list_desc *curr, *next;
  232. dma_addr_t curr_phys, next_phys;
  233. curr = mdesc->list;
  234. curr_phys = mdesc->list_phys;
  235. while (curr) {
  236. next = curr->next_desc;
  237. next_phys = curr->node_addr;
  238. dma_pool_free(mdma->desc_pool, curr, curr_phys);
  239. curr = next;
  240. curr_phys = next_phys;
  241. }
  242. }
  243. static void mdc_desc_free(struct virt_dma_desc *vd)
  244. {
  245. struct mdc_tx_desc *mdesc = to_mdc_desc(&vd->tx);
  246. mdc_list_desc_free(mdesc);
  247. kfree(mdesc);
  248. }
  249. static struct dma_async_tx_descriptor *mdc_prep_dma_memcpy(
  250. struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, size_t len,
  251. unsigned long flags)
  252. {
  253. struct mdc_chan *mchan = to_mdc_chan(chan);
  254. struct mdc_dma *mdma = mchan->mdma;
  255. struct mdc_tx_desc *mdesc;
  256. struct mdc_hw_list_desc *curr, *prev = NULL;
  257. dma_addr_t curr_phys, prev_phys;
  258. if (!len)
  259. return NULL;
  260. mdesc = kzalloc(sizeof(*mdesc), GFP_NOWAIT);
  261. if (!mdesc)
  262. return NULL;
  263. mdesc->chan = mchan;
  264. mdesc->list_xfer_size = len;
  265. while (len > 0) {
  266. size_t xfer_size;
  267. curr = dma_pool_alloc(mdma->desc_pool, GFP_NOWAIT, &curr_phys);
  268. if (!curr)
  269. goto free_desc;
  270. if (prev) {
  271. prev->node_addr = curr_phys;
  272. prev->next_desc = curr;
  273. } else {
  274. mdesc->list_phys = curr_phys;
  275. mdesc->list = curr;
  276. }
  277. xfer_size = min_t(size_t, mdma->max_xfer_size, len);
  278. mdc_list_desc_config(mchan, curr, DMA_MEM_TO_MEM, src, dest,
  279. xfer_size);
  280. prev = curr;
  281. prev_phys = curr_phys;
  282. mdesc->list_len++;
  283. src += xfer_size;
  284. dest += xfer_size;
  285. len -= xfer_size;
  286. }
  287. return vchan_tx_prep(&mchan->vc, &mdesc->vd, flags);
  288. free_desc:
  289. mdc_desc_free(&mdesc->vd);
  290. return NULL;
  291. }
  292. static int mdc_check_slave_width(struct mdc_chan *mchan,
  293. enum dma_transfer_direction dir)
  294. {
  295. enum dma_slave_buswidth width;
  296. if (dir == DMA_MEM_TO_DEV)
  297. width = mchan->config.dst_addr_width;
  298. else
  299. width = mchan->config.src_addr_width;
  300. switch (width) {
  301. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  302. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  303. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  304. case DMA_SLAVE_BUSWIDTH_8_BYTES:
  305. break;
  306. default:
  307. return -EINVAL;
  308. }
  309. if (width > mchan->mdma->bus_width)
  310. return -EINVAL;
  311. return 0;
  312. }
  313. static struct dma_async_tx_descriptor *mdc_prep_dma_cyclic(
  314. struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  315. size_t period_len, enum dma_transfer_direction dir,
  316. unsigned long flags)
  317. {
  318. struct mdc_chan *mchan = to_mdc_chan(chan);
  319. struct mdc_dma *mdma = mchan->mdma;
  320. struct mdc_tx_desc *mdesc;
  321. struct mdc_hw_list_desc *curr, *prev = NULL;
  322. dma_addr_t curr_phys, prev_phys;
  323. if (!buf_len && !period_len)
  324. return NULL;
  325. if (!is_slave_direction(dir))
  326. return NULL;
  327. if (mdc_check_slave_width(mchan, dir) < 0)
  328. return NULL;
  329. mdesc = kzalloc(sizeof(*mdesc), GFP_NOWAIT);
  330. if (!mdesc)
  331. return NULL;
  332. mdesc->chan = mchan;
  333. mdesc->cyclic = true;
  334. mdesc->list_xfer_size = buf_len;
  335. mdesc->list_period_len = DIV_ROUND_UP(period_len,
  336. mdma->max_xfer_size);
  337. while (buf_len > 0) {
  338. size_t remainder = min(period_len, buf_len);
  339. while (remainder > 0) {
  340. size_t xfer_size;
  341. curr = dma_pool_alloc(mdma->desc_pool, GFP_NOWAIT,
  342. &curr_phys);
  343. if (!curr)
  344. goto free_desc;
  345. if (!prev) {
  346. mdesc->list_phys = curr_phys;
  347. mdesc->list = curr;
  348. } else {
  349. prev->node_addr = curr_phys;
  350. prev->next_desc = curr;
  351. }
  352. xfer_size = min_t(size_t, mdma->max_xfer_size,
  353. remainder);
  354. if (dir == DMA_MEM_TO_DEV) {
  355. mdc_list_desc_config(mchan, curr, dir,
  356. buf_addr,
  357. mchan->config.dst_addr,
  358. xfer_size);
  359. } else {
  360. mdc_list_desc_config(mchan, curr, dir,
  361. mchan->config.src_addr,
  362. buf_addr,
  363. xfer_size);
  364. }
  365. prev = curr;
  366. prev_phys = curr_phys;
  367. mdesc->list_len++;
  368. buf_addr += xfer_size;
  369. buf_len -= xfer_size;
  370. remainder -= xfer_size;
  371. }
  372. }
  373. prev->node_addr = mdesc->list_phys;
  374. return vchan_tx_prep(&mchan->vc, &mdesc->vd, flags);
  375. free_desc:
  376. mdc_desc_free(&mdesc->vd);
  377. return NULL;
  378. }
  379. static struct dma_async_tx_descriptor *mdc_prep_slave_sg(
  380. struct dma_chan *chan, struct scatterlist *sgl,
  381. unsigned int sg_len, enum dma_transfer_direction dir,
  382. unsigned long flags, void *context)
  383. {
  384. struct mdc_chan *mchan = to_mdc_chan(chan);
  385. struct mdc_dma *mdma = mchan->mdma;
  386. struct mdc_tx_desc *mdesc;
  387. struct scatterlist *sg;
  388. struct mdc_hw_list_desc *curr, *prev = NULL;
  389. dma_addr_t curr_phys, prev_phys;
  390. unsigned int i;
  391. if (!sgl)
  392. return NULL;
  393. if (!is_slave_direction(dir))
  394. return NULL;
  395. if (mdc_check_slave_width(mchan, dir) < 0)
  396. return NULL;
  397. mdesc = kzalloc(sizeof(*mdesc), GFP_NOWAIT);
  398. if (!mdesc)
  399. return NULL;
  400. mdesc->chan = mchan;
  401. for_each_sg(sgl, sg, sg_len, i) {
  402. dma_addr_t buf = sg_dma_address(sg);
  403. size_t buf_len = sg_dma_len(sg);
  404. while (buf_len > 0) {
  405. size_t xfer_size;
  406. curr = dma_pool_alloc(mdma->desc_pool, GFP_NOWAIT,
  407. &curr_phys);
  408. if (!curr)
  409. goto free_desc;
  410. if (!prev) {
  411. mdesc->list_phys = curr_phys;
  412. mdesc->list = curr;
  413. } else {
  414. prev->node_addr = curr_phys;
  415. prev->next_desc = curr;
  416. }
  417. xfer_size = min_t(size_t, mdma->max_xfer_size,
  418. buf_len);
  419. if (dir == DMA_MEM_TO_DEV) {
  420. mdc_list_desc_config(mchan, curr, dir, buf,
  421. mchan->config.dst_addr,
  422. xfer_size);
  423. } else {
  424. mdc_list_desc_config(mchan, curr, dir,
  425. mchan->config.src_addr,
  426. buf, xfer_size);
  427. }
  428. prev = curr;
  429. prev_phys = curr_phys;
  430. mdesc->list_len++;
  431. mdesc->list_xfer_size += xfer_size;
  432. buf += xfer_size;
  433. buf_len -= xfer_size;
  434. }
  435. }
  436. return vchan_tx_prep(&mchan->vc, &mdesc->vd, flags);
  437. free_desc:
  438. mdc_desc_free(&mdesc->vd);
  439. return NULL;
  440. }
  441. static void mdc_issue_desc(struct mdc_chan *mchan)
  442. {
  443. struct mdc_dma *mdma = mchan->mdma;
  444. struct virt_dma_desc *vd;
  445. struct mdc_tx_desc *mdesc;
  446. u32 val;
  447. vd = vchan_next_desc(&mchan->vc);
  448. if (!vd)
  449. return;
  450. list_del(&vd->node);
  451. mdesc = to_mdc_desc(&vd->tx);
  452. mchan->desc = mdesc;
  453. dev_dbg(mdma2dev(mdma), "Issuing descriptor on channel %d\n",
  454. mchan->chan_nr);
  455. mdma->soc->enable_chan(mchan);
  456. val = mdc_chan_readl(mchan, MDC_GENERAL_CONFIG);
  457. val |= MDC_GENERAL_CONFIG_LIST_IEN | MDC_GENERAL_CONFIG_IEN |
  458. MDC_GENERAL_CONFIG_LEVEL_INT | MDC_GENERAL_CONFIG_PHYSICAL_W |
  459. MDC_GENERAL_CONFIG_PHYSICAL_R;
  460. mdc_chan_writel(mchan, val, MDC_GENERAL_CONFIG);
  461. val = (mchan->thread << MDC_READ_PORT_CONFIG_STHREAD_SHIFT) |
  462. (mchan->thread << MDC_READ_PORT_CONFIG_RTHREAD_SHIFT) |
  463. (mchan->thread << MDC_READ_PORT_CONFIG_WTHREAD_SHIFT);
  464. mdc_chan_writel(mchan, val, MDC_READ_PORT_CONFIG);
  465. mdc_chan_writel(mchan, mdesc->list_phys, MDC_LIST_NODE_ADDRESS);
  466. val = mdc_chan_readl(mchan, MDC_CONTROL_AND_STATUS);
  467. val |= MDC_CONTROL_AND_STATUS_LIST_EN;
  468. mdc_chan_writel(mchan, val, MDC_CONTROL_AND_STATUS);
  469. }
  470. static void mdc_issue_pending(struct dma_chan *chan)
  471. {
  472. struct mdc_chan *mchan = to_mdc_chan(chan);
  473. unsigned long flags;
  474. spin_lock_irqsave(&mchan->vc.lock, flags);
  475. if (vchan_issue_pending(&mchan->vc) && !mchan->desc)
  476. mdc_issue_desc(mchan);
  477. spin_unlock_irqrestore(&mchan->vc.lock, flags);
  478. }
  479. static enum dma_status mdc_tx_status(struct dma_chan *chan,
  480. dma_cookie_t cookie, struct dma_tx_state *txstate)
  481. {
  482. struct mdc_chan *mchan = to_mdc_chan(chan);
  483. struct mdc_tx_desc *mdesc;
  484. struct virt_dma_desc *vd;
  485. unsigned long flags;
  486. size_t bytes = 0;
  487. int ret;
  488. ret = dma_cookie_status(chan, cookie, txstate);
  489. if (ret == DMA_COMPLETE)
  490. return ret;
  491. if (!txstate)
  492. return ret;
  493. spin_lock_irqsave(&mchan->vc.lock, flags);
  494. vd = vchan_find_desc(&mchan->vc, cookie);
  495. if (vd) {
  496. mdesc = to_mdc_desc(&vd->tx);
  497. bytes = mdesc->list_xfer_size;
  498. } else if (mchan->desc && mchan->desc->vd.tx.cookie == cookie) {
  499. struct mdc_hw_list_desc *ldesc;
  500. u32 val1, val2, done, processed, residue;
  501. int i, cmds;
  502. mdesc = mchan->desc;
  503. /*
  504. * Determine the number of commands that haven't been
  505. * processed (handled by the IRQ handler) yet.
  506. */
  507. do {
  508. val1 = mdc_chan_readl(mchan, MDC_CMDS_PROCESSED) &
  509. ~MDC_CMDS_PROCESSED_INT_ACTIVE;
  510. residue = mdc_chan_readl(mchan,
  511. MDC_ACTIVE_TRANSFER_SIZE);
  512. val2 = mdc_chan_readl(mchan, MDC_CMDS_PROCESSED) &
  513. ~MDC_CMDS_PROCESSED_INT_ACTIVE;
  514. } while (val1 != val2);
  515. done = (val1 >> MDC_CMDS_PROCESSED_CMDS_DONE_SHIFT) &
  516. MDC_CMDS_PROCESSED_CMDS_DONE_MASK;
  517. processed = (val1 >> MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT) &
  518. MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK;
  519. cmds = (done - processed) %
  520. (MDC_CMDS_PROCESSED_CMDS_DONE_MASK + 1);
  521. /*
  522. * If the command loaded event hasn't been processed yet, then
  523. * the difference above includes an extra command.
  524. */
  525. if (!mdesc->cmd_loaded)
  526. cmds--;
  527. else
  528. cmds += mdesc->list_cmds_done;
  529. bytes = mdesc->list_xfer_size;
  530. ldesc = mdesc->list;
  531. for (i = 0; i < cmds; i++) {
  532. bytes -= ldesc->xfer_size + 1;
  533. ldesc = ldesc->next_desc;
  534. }
  535. if (ldesc) {
  536. if (residue != MDC_TRANSFER_SIZE_MASK)
  537. bytes -= ldesc->xfer_size - residue;
  538. else
  539. bytes -= ldesc->xfer_size + 1;
  540. }
  541. }
  542. spin_unlock_irqrestore(&mchan->vc.lock, flags);
  543. dma_set_residue(txstate, bytes);
  544. return ret;
  545. }
  546. static unsigned int mdc_get_new_events(struct mdc_chan *mchan)
  547. {
  548. u32 val, processed, done1, done2;
  549. unsigned int ret;
  550. val = mdc_chan_readl(mchan, MDC_CMDS_PROCESSED);
  551. processed = (val >> MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT) &
  552. MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK;
  553. /*
  554. * CMDS_DONE may have incremented between reading CMDS_PROCESSED
  555. * and clearing INT_ACTIVE. Re-read CMDS_PROCESSED to ensure we
  556. * didn't miss a command completion.
  557. */
  558. do {
  559. val = mdc_chan_readl(mchan, MDC_CMDS_PROCESSED);
  560. done1 = (val >> MDC_CMDS_PROCESSED_CMDS_DONE_SHIFT) &
  561. MDC_CMDS_PROCESSED_CMDS_DONE_MASK;
  562. val &= ~((MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK <<
  563. MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT) |
  564. MDC_CMDS_PROCESSED_INT_ACTIVE);
  565. val |= done1 << MDC_CMDS_PROCESSED_CMDS_PROCESSED_SHIFT;
  566. mdc_chan_writel(mchan, val, MDC_CMDS_PROCESSED);
  567. val = mdc_chan_readl(mchan, MDC_CMDS_PROCESSED);
  568. done2 = (val >> MDC_CMDS_PROCESSED_CMDS_DONE_SHIFT) &
  569. MDC_CMDS_PROCESSED_CMDS_DONE_MASK;
  570. } while (done1 != done2);
  571. if (done1 >= processed)
  572. ret = done1 - processed;
  573. else
  574. ret = ((MDC_CMDS_PROCESSED_CMDS_PROCESSED_MASK + 1) -
  575. processed) + done1;
  576. return ret;
  577. }
  578. static int mdc_terminate_all(struct dma_chan *chan)
  579. {
  580. struct mdc_chan *mchan = to_mdc_chan(chan);
  581. struct mdc_tx_desc *mdesc;
  582. unsigned long flags;
  583. LIST_HEAD(head);
  584. spin_lock_irqsave(&mchan->vc.lock, flags);
  585. mdc_chan_writel(mchan, MDC_CONTROL_AND_STATUS_CANCEL,
  586. MDC_CONTROL_AND_STATUS);
  587. mdesc = mchan->desc;
  588. mchan->desc = NULL;
  589. vchan_get_all_descriptors(&mchan->vc, &head);
  590. mdc_get_new_events(mchan);
  591. spin_unlock_irqrestore(&mchan->vc.lock, flags);
  592. if (mdesc)
  593. mdc_desc_free(&mdesc->vd);
  594. vchan_dma_desc_free_list(&mchan->vc, &head);
  595. return 0;
  596. }
  597. static int mdc_slave_config(struct dma_chan *chan,
  598. struct dma_slave_config *config)
  599. {
  600. struct mdc_chan *mchan = to_mdc_chan(chan);
  601. unsigned long flags;
  602. spin_lock_irqsave(&mchan->vc.lock, flags);
  603. mchan->config = *config;
  604. spin_unlock_irqrestore(&mchan->vc.lock, flags);
  605. return 0;
  606. }
  607. static void mdc_free_chan_resources(struct dma_chan *chan)
  608. {
  609. struct mdc_chan *mchan = to_mdc_chan(chan);
  610. struct mdc_dma *mdma = mchan->mdma;
  611. mdc_terminate_all(chan);
  612. mdma->soc->disable_chan(mchan);
  613. }
  614. static irqreturn_t mdc_chan_irq(int irq, void *dev_id)
  615. {
  616. struct mdc_chan *mchan = (struct mdc_chan *)dev_id;
  617. struct mdc_tx_desc *mdesc;
  618. unsigned int i, new_events;
  619. spin_lock(&mchan->vc.lock);
  620. dev_dbg(mdma2dev(mchan->mdma), "IRQ on channel %d\n", mchan->chan_nr);
  621. new_events = mdc_get_new_events(mchan);
  622. if (!new_events)
  623. goto out;
  624. mdesc = mchan->desc;
  625. if (!mdesc) {
  626. dev_warn(mdma2dev(mchan->mdma),
  627. "IRQ with no active descriptor on channel %d\n",
  628. mchan->chan_nr);
  629. goto out;
  630. }
  631. for (i = 0; i < new_events; i++) {
  632. /*
  633. * The first interrupt in a transfer indicates that the
  634. * command list has been loaded, not that a command has
  635. * been completed.
  636. */
  637. if (!mdesc->cmd_loaded) {
  638. mdesc->cmd_loaded = true;
  639. continue;
  640. }
  641. mdesc->list_cmds_done++;
  642. if (mdesc->cyclic) {
  643. mdesc->list_cmds_done %= mdesc->list_len;
  644. if (mdesc->list_cmds_done % mdesc->list_period_len == 0)
  645. vchan_cyclic_callback(&mdesc->vd);
  646. } else if (mdesc->list_cmds_done == mdesc->list_len) {
  647. mchan->desc = NULL;
  648. vchan_cookie_complete(&mdesc->vd);
  649. mdc_issue_desc(mchan);
  650. break;
  651. }
  652. }
  653. out:
  654. spin_unlock(&mchan->vc.lock);
  655. return IRQ_HANDLED;
  656. }
  657. static struct dma_chan *mdc_of_xlate(struct of_phandle_args *dma_spec,
  658. struct of_dma *ofdma)
  659. {
  660. struct mdc_dma *mdma = ofdma->of_dma_data;
  661. struct dma_chan *chan;
  662. if (dma_spec->args_count != 3)
  663. return NULL;
  664. list_for_each_entry(chan, &mdma->dma_dev.channels, device_node) {
  665. struct mdc_chan *mchan = to_mdc_chan(chan);
  666. if (!(dma_spec->args[1] & BIT(mchan->chan_nr)))
  667. continue;
  668. if (dma_get_slave_channel(chan)) {
  669. mchan->periph = dma_spec->args[0];
  670. mchan->thread = dma_spec->args[2];
  671. return chan;
  672. }
  673. }
  674. return NULL;
  675. }
  676. #define PISTACHIO_CR_PERIPH_DMA_ROUTE(ch) (0x120 + 0x4 * ((ch) / 4))
  677. #define PISTACHIO_CR_PERIPH_DMA_ROUTE_SHIFT(ch) (8 * ((ch) % 4))
  678. #define PISTACHIO_CR_PERIPH_DMA_ROUTE_MASK 0x3f
  679. static void pistachio_mdc_enable_chan(struct mdc_chan *mchan)
  680. {
  681. struct mdc_dma *mdma = mchan->mdma;
  682. regmap_update_bits(mdma->periph_regs,
  683. PISTACHIO_CR_PERIPH_DMA_ROUTE(mchan->chan_nr),
  684. PISTACHIO_CR_PERIPH_DMA_ROUTE_MASK <<
  685. PISTACHIO_CR_PERIPH_DMA_ROUTE_SHIFT(mchan->chan_nr),
  686. mchan->periph <<
  687. PISTACHIO_CR_PERIPH_DMA_ROUTE_SHIFT(mchan->chan_nr));
  688. }
  689. static void pistachio_mdc_disable_chan(struct mdc_chan *mchan)
  690. {
  691. struct mdc_dma *mdma = mchan->mdma;
  692. regmap_update_bits(mdma->periph_regs,
  693. PISTACHIO_CR_PERIPH_DMA_ROUTE(mchan->chan_nr),
  694. PISTACHIO_CR_PERIPH_DMA_ROUTE_MASK <<
  695. PISTACHIO_CR_PERIPH_DMA_ROUTE_SHIFT(mchan->chan_nr),
  696. 0);
  697. }
  698. static const struct mdc_dma_soc_data pistachio_mdc_data = {
  699. .enable_chan = pistachio_mdc_enable_chan,
  700. .disable_chan = pistachio_mdc_disable_chan,
  701. };
  702. static const struct of_device_id mdc_dma_of_match[] = {
  703. { .compatible = "img,pistachio-mdc-dma", .data = &pistachio_mdc_data, },
  704. { },
  705. };
  706. MODULE_DEVICE_TABLE(of, mdc_dma_of_match);
  707. static int mdc_dma_probe(struct platform_device *pdev)
  708. {
  709. struct mdc_dma *mdma;
  710. struct resource *res;
  711. unsigned int i;
  712. u32 val;
  713. int ret;
  714. mdma = devm_kzalloc(&pdev->dev, sizeof(*mdma), GFP_KERNEL);
  715. if (!mdma)
  716. return -ENOMEM;
  717. platform_set_drvdata(pdev, mdma);
  718. mdma->soc = of_device_get_match_data(&pdev->dev);
  719. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  720. mdma->regs = devm_ioremap_resource(&pdev->dev, res);
  721. if (IS_ERR(mdma->regs))
  722. return PTR_ERR(mdma->regs);
  723. mdma->periph_regs = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  724. "img,cr-periph");
  725. if (IS_ERR(mdma->periph_regs))
  726. return PTR_ERR(mdma->periph_regs);
  727. mdma->clk = devm_clk_get(&pdev->dev, "sys");
  728. if (IS_ERR(mdma->clk))
  729. return PTR_ERR(mdma->clk);
  730. ret = clk_prepare_enable(mdma->clk);
  731. if (ret)
  732. return ret;
  733. dma_cap_zero(mdma->dma_dev.cap_mask);
  734. dma_cap_set(DMA_SLAVE, mdma->dma_dev.cap_mask);
  735. dma_cap_set(DMA_PRIVATE, mdma->dma_dev.cap_mask);
  736. dma_cap_set(DMA_CYCLIC, mdma->dma_dev.cap_mask);
  737. dma_cap_set(DMA_MEMCPY, mdma->dma_dev.cap_mask);
  738. val = mdc_readl(mdma, MDC_GLOBAL_CONFIG_A);
  739. mdma->nr_channels = (val >> MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_SHIFT) &
  740. MDC_GLOBAL_CONFIG_A_DMA_CONTEXTS_MASK;
  741. mdma->nr_threads =
  742. 1 << ((val >> MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_SHIFT) &
  743. MDC_GLOBAL_CONFIG_A_THREAD_ID_WIDTH_MASK);
  744. mdma->bus_width =
  745. (1 << ((val >> MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_SHIFT) &
  746. MDC_GLOBAL_CONFIG_A_SYS_DAT_WIDTH_MASK)) / 8;
  747. /*
  748. * Although transfer sizes of up to MDC_TRANSFER_SIZE_MASK + 1 bytes
  749. * are supported, this makes it possible for the value reported in
  750. * MDC_ACTIVE_TRANSFER_SIZE to be ambiguous - an active transfer size
  751. * of MDC_TRANSFER_SIZE_MASK may indicate either that 0 bytes or
  752. * MDC_TRANSFER_SIZE_MASK + 1 bytes are remaining. To eliminate this
  753. * ambiguity, restrict transfer sizes to one bus-width less than the
  754. * actual maximum.
  755. */
  756. mdma->max_xfer_size = MDC_TRANSFER_SIZE_MASK + 1 - mdma->bus_width;
  757. of_property_read_u32(pdev->dev.of_node, "dma-channels",
  758. &mdma->nr_channels);
  759. ret = of_property_read_u32(pdev->dev.of_node,
  760. "img,max-burst-multiplier",
  761. &mdma->max_burst_mult);
  762. if (ret)
  763. goto disable_clk;
  764. mdma->dma_dev.dev = &pdev->dev;
  765. mdma->dma_dev.device_prep_slave_sg = mdc_prep_slave_sg;
  766. mdma->dma_dev.device_prep_dma_cyclic = mdc_prep_dma_cyclic;
  767. mdma->dma_dev.device_prep_dma_memcpy = mdc_prep_dma_memcpy;
  768. mdma->dma_dev.device_free_chan_resources = mdc_free_chan_resources;
  769. mdma->dma_dev.device_tx_status = mdc_tx_status;
  770. mdma->dma_dev.device_issue_pending = mdc_issue_pending;
  771. mdma->dma_dev.device_terminate_all = mdc_terminate_all;
  772. mdma->dma_dev.device_config = mdc_slave_config;
  773. mdma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  774. mdma->dma_dev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  775. for (i = 1; i <= mdma->bus_width; i <<= 1) {
  776. mdma->dma_dev.src_addr_widths |= BIT(i);
  777. mdma->dma_dev.dst_addr_widths |= BIT(i);
  778. }
  779. INIT_LIST_HEAD(&mdma->dma_dev.channels);
  780. for (i = 0; i < mdma->nr_channels; i++) {
  781. struct mdc_chan *mchan = &mdma->channels[i];
  782. mchan->mdma = mdma;
  783. mchan->chan_nr = i;
  784. mchan->irq = platform_get_irq(pdev, i);
  785. if (mchan->irq < 0) {
  786. ret = mchan->irq;
  787. goto disable_clk;
  788. }
  789. ret = devm_request_irq(&pdev->dev, mchan->irq, mdc_chan_irq,
  790. IRQ_TYPE_LEVEL_HIGH,
  791. dev_name(&pdev->dev), mchan);
  792. if (ret < 0)
  793. goto disable_clk;
  794. mchan->vc.desc_free = mdc_desc_free;
  795. vchan_init(&mchan->vc, &mdma->dma_dev);
  796. }
  797. mdma->desc_pool = dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
  798. sizeof(struct mdc_hw_list_desc),
  799. 4, 0);
  800. if (!mdma->desc_pool) {
  801. ret = -ENOMEM;
  802. goto disable_clk;
  803. }
  804. ret = dma_async_device_register(&mdma->dma_dev);
  805. if (ret)
  806. goto disable_clk;
  807. ret = of_dma_controller_register(pdev->dev.of_node, mdc_of_xlate, mdma);
  808. if (ret)
  809. goto unregister;
  810. dev_info(&pdev->dev, "MDC with %u channels and %u threads\n",
  811. mdma->nr_channels, mdma->nr_threads);
  812. return 0;
  813. unregister:
  814. dma_async_device_unregister(&mdma->dma_dev);
  815. disable_clk:
  816. clk_disable_unprepare(mdma->clk);
  817. return ret;
  818. }
  819. static int mdc_dma_remove(struct platform_device *pdev)
  820. {
  821. struct mdc_dma *mdma = platform_get_drvdata(pdev);
  822. struct mdc_chan *mchan, *next;
  823. of_dma_controller_free(pdev->dev.of_node);
  824. dma_async_device_unregister(&mdma->dma_dev);
  825. list_for_each_entry_safe(mchan, next, &mdma->dma_dev.channels,
  826. vc.chan.device_node) {
  827. list_del(&mchan->vc.chan.device_node);
  828. devm_free_irq(&pdev->dev, mchan->irq, mchan);
  829. tasklet_kill(&mchan->vc.task);
  830. }
  831. clk_disable_unprepare(mdma->clk);
  832. return 0;
  833. }
  834. static struct platform_driver mdc_dma_driver = {
  835. .driver = {
  836. .name = "img-mdc-dma",
  837. .of_match_table = of_match_ptr(mdc_dma_of_match),
  838. },
  839. .probe = mdc_dma_probe,
  840. .remove = mdc_dma_remove,
  841. };
  842. module_platform_driver(mdc_dma_driver);
  843. MODULE_DESCRIPTION("IMG Multi-threaded DMA Controller (MDC) driver");
  844. MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
  845. MODULE_LICENSE("GPL v2");