tegra20-apb-dma.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  1. /*
  2. * DMA driver for Nvidia's Tegra20 APB DMA controller.
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/bitops.h>
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/mm.h>
  27. #include <linux/module.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/slab.h>
  33. #include <mach/clk.h>
  34. #include "dmaengine.h"
  35. #define TEGRA_APBDMA_GENERAL 0x0
  36. #define TEGRA_APBDMA_GENERAL_ENABLE BIT(31)
  37. #define TEGRA_APBDMA_CONTROL 0x010
  38. #define TEGRA_APBDMA_IRQ_MASK 0x01c
  39. #define TEGRA_APBDMA_IRQ_MASK_SET 0x020
  40. /* CSR register */
  41. #define TEGRA_APBDMA_CHAN_CSR 0x00
  42. #define TEGRA_APBDMA_CSR_ENB BIT(31)
  43. #define TEGRA_APBDMA_CSR_IE_EOC BIT(30)
  44. #define TEGRA_APBDMA_CSR_HOLD BIT(29)
  45. #define TEGRA_APBDMA_CSR_DIR BIT(28)
  46. #define TEGRA_APBDMA_CSR_ONCE BIT(27)
  47. #define TEGRA_APBDMA_CSR_FLOW BIT(21)
  48. #define TEGRA_APBDMA_CSR_REQ_SEL_SHIFT 16
  49. #define TEGRA_APBDMA_CSR_WCOUNT_MASK 0xFFFC
  50. /* STATUS register */
  51. #define TEGRA_APBDMA_CHAN_STATUS 0x004
  52. #define TEGRA_APBDMA_STATUS_BUSY BIT(31)
  53. #define TEGRA_APBDMA_STATUS_ISE_EOC BIT(30)
  54. #define TEGRA_APBDMA_STATUS_HALT BIT(29)
  55. #define TEGRA_APBDMA_STATUS_PING_PONG BIT(28)
  56. #define TEGRA_APBDMA_STATUS_COUNT_SHIFT 2
  57. #define TEGRA_APBDMA_STATUS_COUNT_MASK 0xFFFC
  58. #define TEGRA_APBDMA_CHAN_CSRE 0x00C
  59. #define TEGRA_APBDMA_CHAN_CSRE_PAUSE (1 << 31)
  60. /* AHB memory address */
  61. #define TEGRA_APBDMA_CHAN_AHBPTR 0x010
  62. /* AHB sequence register */
  63. #define TEGRA_APBDMA_CHAN_AHBSEQ 0x14
  64. #define TEGRA_APBDMA_AHBSEQ_INTR_ENB BIT(31)
  65. #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_8 (0 << 28)
  66. #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_16 (1 << 28)
  67. #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32 (2 << 28)
  68. #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_64 (3 << 28)
  69. #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_128 (4 << 28)
  70. #define TEGRA_APBDMA_AHBSEQ_DATA_SWAP BIT(27)
  71. #define TEGRA_APBDMA_AHBSEQ_BURST_1 (4 << 24)
  72. #define TEGRA_APBDMA_AHBSEQ_BURST_4 (5 << 24)
  73. #define TEGRA_APBDMA_AHBSEQ_BURST_8 (6 << 24)
  74. #define TEGRA_APBDMA_AHBSEQ_DBL_BUF BIT(19)
  75. #define TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT 16
  76. #define TEGRA_APBDMA_AHBSEQ_WRAP_NONE 0
  77. /* APB address */
  78. #define TEGRA_APBDMA_CHAN_APBPTR 0x018
  79. /* APB sequence register */
  80. #define TEGRA_APBDMA_CHAN_APBSEQ 0x01c
  81. #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_8 (0 << 28)
  82. #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_16 (1 << 28)
  83. #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32 (2 << 28)
  84. #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_64 (3 << 28)
  85. #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_128 (4 << 28)
  86. #define TEGRA_APBDMA_APBSEQ_DATA_SWAP BIT(27)
  87. #define TEGRA_APBDMA_APBSEQ_WRAP_WORD_1 (1 << 16)
  88. /*
  89. * If any burst is in flight and DMA paused then this is the time to complete
  90. * on-flight burst and update DMA status register.
  91. */
  92. #define TEGRA_APBDMA_BURST_COMPLETE_TIME 20
  93. /* Channel base address offset from APBDMA base address */
  94. #define TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET 0x1000
  95. /* DMA channel register space size */
  96. #define TEGRA_APBDMA_CHANNEL_REGISTER_SIZE 0x20
  97. struct tegra_dma;
  98. /*
  99. * tegra_dma_chip_data Tegra chip specific DMA data
  100. * @nr_channels: Number of channels available in the controller.
  101. * @max_dma_count: Maximum DMA transfer count supported by DMA controller.
  102. * @support_channel_pause: Support channel wise pause of dma.
  103. */
  104. struct tegra_dma_chip_data {
  105. int nr_channels;
  106. int max_dma_count;
  107. bool support_channel_pause;
  108. };
  109. /* DMA channel registers */
  110. struct tegra_dma_channel_regs {
  111. unsigned long csr;
  112. unsigned long ahb_ptr;
  113. unsigned long apb_ptr;
  114. unsigned long ahb_seq;
  115. unsigned long apb_seq;
  116. };
  117. /*
  118. * tegra_dma_sg_req: Dma request details to configure hardware. This
  119. * contains the details for one transfer to configure DMA hw.
  120. * The client's request for data transfer can be broken into multiple
  121. * sub-transfer as per requester details and hw support.
  122. * This sub transfer get added in the list of transfer and point to Tegra
  123. * DMA descriptor which manages the transfer details.
  124. */
  125. struct tegra_dma_sg_req {
  126. struct tegra_dma_channel_regs ch_regs;
  127. int req_len;
  128. bool configured;
  129. bool last_sg;
  130. bool half_done;
  131. struct list_head node;
  132. struct tegra_dma_desc *dma_desc;
  133. };
  134. /*
  135. * tegra_dma_desc: Tegra DMA descriptors which manages the client requests.
  136. * This descriptor keep track of transfer status, callbacks and request
  137. * counts etc.
  138. */
  139. struct tegra_dma_desc {
  140. struct dma_async_tx_descriptor txd;
  141. int bytes_requested;
  142. int bytes_transferred;
  143. enum dma_status dma_status;
  144. struct list_head node;
  145. struct list_head tx_list;
  146. struct list_head cb_node;
  147. int cb_count;
  148. };
  149. struct tegra_dma_channel;
  150. typedef void (*dma_isr_handler)(struct tegra_dma_channel *tdc,
  151. bool to_terminate);
  152. /* tegra_dma_channel: Channel specific information */
  153. struct tegra_dma_channel {
  154. struct dma_chan dma_chan;
  155. char name[30];
  156. bool config_init;
  157. int id;
  158. int irq;
  159. unsigned long chan_base_offset;
  160. spinlock_t lock;
  161. bool busy;
  162. struct tegra_dma *tdma;
  163. bool cyclic;
  164. /* Different lists for managing the requests */
  165. struct list_head free_sg_req;
  166. struct list_head pending_sg_req;
  167. struct list_head free_dma_desc;
  168. struct list_head cb_desc;
  169. /* ISR handler and tasklet for bottom half of isr handling */
  170. dma_isr_handler isr_handler;
  171. struct tasklet_struct tasklet;
  172. dma_async_tx_callback callback;
  173. void *callback_param;
  174. /* Channel-slave specific configuration */
  175. struct dma_slave_config dma_sconfig;
  176. };
  177. /* tegra_dma: Tegra DMA specific information */
  178. struct tegra_dma {
  179. struct dma_device dma_dev;
  180. struct device *dev;
  181. struct clk *dma_clk;
  182. spinlock_t global_lock;
  183. void __iomem *base_addr;
  184. const struct tegra_dma_chip_data *chip_data;
  185. /* Some register need to be cache before suspend */
  186. u32 reg_gen;
  187. /* Last member of the structure */
  188. struct tegra_dma_channel channels[0];
  189. };
  190. static inline void tdma_write(struct tegra_dma *tdma, u32 reg, u32 val)
  191. {
  192. writel(val, tdma->base_addr + reg);
  193. }
  194. static inline u32 tdma_read(struct tegra_dma *tdma, u32 reg)
  195. {
  196. return readl(tdma->base_addr + reg);
  197. }
  198. static inline void tdc_write(struct tegra_dma_channel *tdc,
  199. u32 reg, u32 val)
  200. {
  201. writel(val, tdc->tdma->base_addr + tdc->chan_base_offset + reg);
  202. }
  203. static inline u32 tdc_read(struct tegra_dma_channel *tdc, u32 reg)
  204. {
  205. return readl(tdc->tdma->base_addr + tdc->chan_base_offset + reg);
  206. }
  207. static inline struct tegra_dma_channel *to_tegra_dma_chan(struct dma_chan *dc)
  208. {
  209. return container_of(dc, struct tegra_dma_channel, dma_chan);
  210. }
  211. static inline struct tegra_dma_desc *txd_to_tegra_dma_desc(
  212. struct dma_async_tx_descriptor *td)
  213. {
  214. return container_of(td, struct tegra_dma_desc, txd);
  215. }
  216. static inline struct device *tdc2dev(struct tegra_dma_channel *tdc)
  217. {
  218. return &tdc->dma_chan.dev->device;
  219. }
  220. static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *tx);
  221. static int tegra_dma_runtime_suspend(struct device *dev);
  222. static int tegra_dma_runtime_resume(struct device *dev);
  223. /* Get DMA desc from free list, if not there then allocate it. */
  224. static struct tegra_dma_desc *tegra_dma_desc_get(
  225. struct tegra_dma_channel *tdc)
  226. {
  227. struct tegra_dma_desc *dma_desc;
  228. unsigned long flags;
  229. spin_lock_irqsave(&tdc->lock, flags);
  230. /* Do not allocate if desc are waiting for ack */
  231. list_for_each_entry(dma_desc, &tdc->free_dma_desc, node) {
  232. if (async_tx_test_ack(&dma_desc->txd)) {
  233. list_del(&dma_desc->node);
  234. spin_unlock_irqrestore(&tdc->lock, flags);
  235. return dma_desc;
  236. }
  237. }
  238. spin_unlock_irqrestore(&tdc->lock, flags);
  239. /* Allocate DMA desc */
  240. dma_desc = kzalloc(sizeof(*dma_desc), GFP_ATOMIC);
  241. if (!dma_desc) {
  242. dev_err(tdc2dev(tdc), "dma_desc alloc failed\n");
  243. return NULL;
  244. }
  245. dma_async_tx_descriptor_init(&dma_desc->txd, &tdc->dma_chan);
  246. dma_desc->txd.tx_submit = tegra_dma_tx_submit;
  247. dma_desc->txd.flags = 0;
  248. return dma_desc;
  249. }
  250. static void tegra_dma_desc_put(struct tegra_dma_channel *tdc,
  251. struct tegra_dma_desc *dma_desc)
  252. {
  253. unsigned long flags;
  254. spin_lock_irqsave(&tdc->lock, flags);
  255. if (!list_empty(&dma_desc->tx_list))
  256. list_splice_init(&dma_desc->tx_list, &tdc->free_sg_req);
  257. list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
  258. spin_unlock_irqrestore(&tdc->lock, flags);
  259. }
  260. static struct tegra_dma_sg_req *tegra_dma_sg_req_get(
  261. struct tegra_dma_channel *tdc)
  262. {
  263. struct tegra_dma_sg_req *sg_req = NULL;
  264. unsigned long flags;
  265. spin_lock_irqsave(&tdc->lock, flags);
  266. if (!list_empty(&tdc->free_sg_req)) {
  267. sg_req = list_first_entry(&tdc->free_sg_req,
  268. typeof(*sg_req), node);
  269. list_del(&sg_req->node);
  270. spin_unlock_irqrestore(&tdc->lock, flags);
  271. return sg_req;
  272. }
  273. spin_unlock_irqrestore(&tdc->lock, flags);
  274. sg_req = kzalloc(sizeof(struct tegra_dma_sg_req), GFP_ATOMIC);
  275. if (!sg_req)
  276. dev_err(tdc2dev(tdc), "sg_req alloc failed\n");
  277. return sg_req;
  278. }
  279. static int tegra_dma_slave_config(struct dma_chan *dc,
  280. struct dma_slave_config *sconfig)
  281. {
  282. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  283. if (!list_empty(&tdc->pending_sg_req)) {
  284. dev_err(tdc2dev(tdc), "Configuration not allowed\n");
  285. return -EBUSY;
  286. }
  287. memcpy(&tdc->dma_sconfig, sconfig, sizeof(*sconfig));
  288. tdc->config_init = true;
  289. return 0;
  290. }
  291. static void tegra_dma_global_pause(struct tegra_dma_channel *tdc,
  292. bool wait_for_burst_complete)
  293. {
  294. struct tegra_dma *tdma = tdc->tdma;
  295. spin_lock(&tdma->global_lock);
  296. tdma_write(tdma, TEGRA_APBDMA_GENERAL, 0);
  297. if (wait_for_burst_complete)
  298. udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
  299. }
  300. static void tegra_dma_global_resume(struct tegra_dma_channel *tdc)
  301. {
  302. struct tegra_dma *tdma = tdc->tdma;
  303. tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE);
  304. spin_unlock(&tdma->global_lock);
  305. }
  306. static void tegra_dma_pause(struct tegra_dma_channel *tdc,
  307. bool wait_for_burst_complete)
  308. {
  309. struct tegra_dma *tdma = tdc->tdma;
  310. if (tdma->chip_data->support_channel_pause) {
  311. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSRE,
  312. TEGRA_APBDMA_CHAN_CSRE_PAUSE);
  313. if (wait_for_burst_complete)
  314. udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
  315. } else {
  316. tegra_dma_global_pause(tdc, wait_for_burst_complete);
  317. }
  318. }
  319. static void tegra_dma_resume(struct tegra_dma_channel *tdc)
  320. {
  321. struct tegra_dma *tdma = tdc->tdma;
  322. if (tdma->chip_data->support_channel_pause) {
  323. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSRE, 0);
  324. } else {
  325. tegra_dma_global_resume(tdc);
  326. }
  327. }
  328. static void tegra_dma_stop(struct tegra_dma_channel *tdc)
  329. {
  330. u32 csr;
  331. u32 status;
  332. /* Disable interrupts */
  333. csr = tdc_read(tdc, TEGRA_APBDMA_CHAN_CSR);
  334. csr &= ~TEGRA_APBDMA_CSR_IE_EOC;
  335. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, csr);
  336. /* Disable DMA */
  337. csr &= ~TEGRA_APBDMA_CSR_ENB;
  338. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, csr);
  339. /* Clear interrupt status if it is there */
  340. status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
  341. if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
  342. dev_dbg(tdc2dev(tdc), "%s():clearing interrupt\n", __func__);
  343. tdc_write(tdc, TEGRA_APBDMA_CHAN_STATUS, status);
  344. }
  345. tdc->busy = false;
  346. }
  347. static void tegra_dma_start(struct tegra_dma_channel *tdc,
  348. struct tegra_dma_sg_req *sg_req)
  349. {
  350. struct tegra_dma_channel_regs *ch_regs = &sg_req->ch_regs;
  351. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, ch_regs->csr);
  352. tdc_write(tdc, TEGRA_APBDMA_CHAN_APBSEQ, ch_regs->apb_seq);
  353. tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, ch_regs->apb_ptr);
  354. tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBSEQ, ch_regs->ahb_seq);
  355. tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, ch_regs->ahb_ptr);
  356. /* Start DMA */
  357. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
  358. ch_regs->csr | TEGRA_APBDMA_CSR_ENB);
  359. }
  360. static void tegra_dma_configure_for_next(struct tegra_dma_channel *tdc,
  361. struct tegra_dma_sg_req *nsg_req)
  362. {
  363. unsigned long status;
  364. /*
  365. * The DMA controller reloads the new configuration for next transfer
  366. * after last burst of current transfer completes.
  367. * If there is no IEC status then this makes sure that last burst
  368. * has not be completed. There may be case that last burst is on
  369. * flight and so it can complete but because DMA is paused, it
  370. * will not generates interrupt as well as not reload the new
  371. * configuration.
  372. * If there is already IEC status then interrupt handler need to
  373. * load new configuration.
  374. */
  375. tegra_dma_pause(tdc, false);
  376. status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
  377. /*
  378. * If interrupt is pending then do nothing as the ISR will handle
  379. * the programing for new request.
  380. */
  381. if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
  382. dev_err(tdc2dev(tdc),
  383. "Skipping new configuration as interrupt is pending\n");
  384. tegra_dma_resume(tdc);
  385. return;
  386. }
  387. /* Safe to program new configuration */
  388. tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, nsg_req->ch_regs.apb_ptr);
  389. tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, nsg_req->ch_regs.ahb_ptr);
  390. tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
  391. nsg_req->ch_regs.csr | TEGRA_APBDMA_CSR_ENB);
  392. nsg_req->configured = true;
  393. tegra_dma_resume(tdc);
  394. }
  395. static void tdc_start_head_req(struct tegra_dma_channel *tdc)
  396. {
  397. struct tegra_dma_sg_req *sg_req;
  398. if (list_empty(&tdc->pending_sg_req))
  399. return;
  400. sg_req = list_first_entry(&tdc->pending_sg_req,
  401. typeof(*sg_req), node);
  402. tegra_dma_start(tdc, sg_req);
  403. sg_req->configured = true;
  404. tdc->busy = true;
  405. }
  406. static void tdc_configure_next_head_desc(struct tegra_dma_channel *tdc)
  407. {
  408. struct tegra_dma_sg_req *hsgreq;
  409. struct tegra_dma_sg_req *hnsgreq;
  410. if (list_empty(&tdc->pending_sg_req))
  411. return;
  412. hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
  413. if (!list_is_last(&hsgreq->node, &tdc->pending_sg_req)) {
  414. hnsgreq = list_first_entry(&hsgreq->node,
  415. typeof(*hnsgreq), node);
  416. tegra_dma_configure_for_next(tdc, hnsgreq);
  417. }
  418. }
  419. static inline int get_current_xferred_count(struct tegra_dma_channel *tdc,
  420. struct tegra_dma_sg_req *sg_req, unsigned long status)
  421. {
  422. return sg_req->req_len - (status & TEGRA_APBDMA_STATUS_COUNT_MASK) - 4;
  423. }
  424. static void tegra_dma_abort_all(struct tegra_dma_channel *tdc)
  425. {
  426. struct tegra_dma_sg_req *sgreq;
  427. struct tegra_dma_desc *dma_desc;
  428. while (!list_empty(&tdc->pending_sg_req)) {
  429. sgreq = list_first_entry(&tdc->pending_sg_req,
  430. typeof(*sgreq), node);
  431. list_move_tail(&sgreq->node, &tdc->free_sg_req);
  432. if (sgreq->last_sg) {
  433. dma_desc = sgreq->dma_desc;
  434. dma_desc->dma_status = DMA_ERROR;
  435. list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
  436. /* Add in cb list if it is not there. */
  437. if (!dma_desc->cb_count)
  438. list_add_tail(&dma_desc->cb_node,
  439. &tdc->cb_desc);
  440. dma_desc->cb_count++;
  441. }
  442. }
  443. tdc->isr_handler = NULL;
  444. }
  445. static bool handle_continuous_head_request(struct tegra_dma_channel *tdc,
  446. struct tegra_dma_sg_req *last_sg_req, bool to_terminate)
  447. {
  448. struct tegra_dma_sg_req *hsgreq = NULL;
  449. if (list_empty(&tdc->pending_sg_req)) {
  450. dev_err(tdc2dev(tdc), "Dma is running without req\n");
  451. tegra_dma_stop(tdc);
  452. return false;
  453. }
  454. /*
  455. * Check that head req on list should be in flight.
  456. * If it is not in flight then abort transfer as
  457. * looping of transfer can not continue.
  458. */
  459. hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
  460. if (!hsgreq->configured) {
  461. tegra_dma_stop(tdc);
  462. dev_err(tdc2dev(tdc), "Error in dma transfer, aborting dma\n");
  463. tegra_dma_abort_all(tdc);
  464. return false;
  465. }
  466. /* Configure next request */
  467. if (!to_terminate)
  468. tdc_configure_next_head_desc(tdc);
  469. return true;
  470. }
  471. static void handle_once_dma_done(struct tegra_dma_channel *tdc,
  472. bool to_terminate)
  473. {
  474. struct tegra_dma_sg_req *sgreq;
  475. struct tegra_dma_desc *dma_desc;
  476. tdc->busy = false;
  477. sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
  478. dma_desc = sgreq->dma_desc;
  479. dma_desc->bytes_transferred += sgreq->req_len;
  480. list_del(&sgreq->node);
  481. if (sgreq->last_sg) {
  482. dma_desc->dma_status = DMA_SUCCESS;
  483. dma_cookie_complete(&dma_desc->txd);
  484. if (!dma_desc->cb_count)
  485. list_add_tail(&dma_desc->cb_node, &tdc->cb_desc);
  486. dma_desc->cb_count++;
  487. list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
  488. }
  489. list_add_tail(&sgreq->node, &tdc->free_sg_req);
  490. /* Do not start DMA if it is going to be terminate */
  491. if (to_terminate || list_empty(&tdc->pending_sg_req))
  492. return;
  493. tdc_start_head_req(tdc);
  494. return;
  495. }
  496. static void handle_cont_sngl_cycle_dma_done(struct tegra_dma_channel *tdc,
  497. bool to_terminate)
  498. {
  499. struct tegra_dma_sg_req *sgreq;
  500. struct tegra_dma_desc *dma_desc;
  501. bool st;
  502. sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
  503. dma_desc = sgreq->dma_desc;
  504. dma_desc->bytes_transferred += sgreq->req_len;
  505. /* Callback need to be call */
  506. if (!dma_desc->cb_count)
  507. list_add_tail(&dma_desc->cb_node, &tdc->cb_desc);
  508. dma_desc->cb_count++;
  509. /* If not last req then put at end of pending list */
  510. if (!list_is_last(&sgreq->node, &tdc->pending_sg_req)) {
  511. list_move_tail(&sgreq->node, &tdc->pending_sg_req);
  512. sgreq->configured = false;
  513. st = handle_continuous_head_request(tdc, sgreq, to_terminate);
  514. if (!st)
  515. dma_desc->dma_status = DMA_ERROR;
  516. }
  517. return;
  518. }
  519. static void tegra_dma_tasklet(unsigned long data)
  520. {
  521. struct tegra_dma_channel *tdc = (struct tegra_dma_channel *)data;
  522. dma_async_tx_callback callback = NULL;
  523. void *callback_param = NULL;
  524. struct tegra_dma_desc *dma_desc;
  525. unsigned long flags;
  526. int cb_count;
  527. spin_lock_irqsave(&tdc->lock, flags);
  528. while (!list_empty(&tdc->cb_desc)) {
  529. dma_desc = list_first_entry(&tdc->cb_desc,
  530. typeof(*dma_desc), cb_node);
  531. list_del(&dma_desc->cb_node);
  532. callback = dma_desc->txd.callback;
  533. callback_param = dma_desc->txd.callback_param;
  534. cb_count = dma_desc->cb_count;
  535. dma_desc->cb_count = 0;
  536. spin_unlock_irqrestore(&tdc->lock, flags);
  537. while (cb_count-- && callback)
  538. callback(callback_param);
  539. spin_lock_irqsave(&tdc->lock, flags);
  540. }
  541. spin_unlock_irqrestore(&tdc->lock, flags);
  542. }
  543. static irqreturn_t tegra_dma_isr(int irq, void *dev_id)
  544. {
  545. struct tegra_dma_channel *tdc = dev_id;
  546. unsigned long status;
  547. unsigned long flags;
  548. spin_lock_irqsave(&tdc->lock, flags);
  549. status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
  550. if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
  551. tdc_write(tdc, TEGRA_APBDMA_CHAN_STATUS, status);
  552. tdc->isr_handler(tdc, false);
  553. tasklet_schedule(&tdc->tasklet);
  554. spin_unlock_irqrestore(&tdc->lock, flags);
  555. return IRQ_HANDLED;
  556. }
  557. spin_unlock_irqrestore(&tdc->lock, flags);
  558. dev_info(tdc2dev(tdc),
  559. "Interrupt already served status 0x%08lx\n", status);
  560. return IRQ_NONE;
  561. }
  562. static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *txd)
  563. {
  564. struct tegra_dma_desc *dma_desc = txd_to_tegra_dma_desc(txd);
  565. struct tegra_dma_channel *tdc = to_tegra_dma_chan(txd->chan);
  566. unsigned long flags;
  567. dma_cookie_t cookie;
  568. spin_lock_irqsave(&tdc->lock, flags);
  569. dma_desc->dma_status = DMA_IN_PROGRESS;
  570. cookie = dma_cookie_assign(&dma_desc->txd);
  571. list_splice_tail_init(&dma_desc->tx_list, &tdc->pending_sg_req);
  572. spin_unlock_irqrestore(&tdc->lock, flags);
  573. return cookie;
  574. }
  575. static void tegra_dma_issue_pending(struct dma_chan *dc)
  576. {
  577. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  578. unsigned long flags;
  579. spin_lock_irqsave(&tdc->lock, flags);
  580. if (list_empty(&tdc->pending_sg_req)) {
  581. dev_err(tdc2dev(tdc), "No DMA request\n");
  582. goto end;
  583. }
  584. if (!tdc->busy) {
  585. tdc_start_head_req(tdc);
  586. /* Continuous single mode: Configure next req */
  587. if (tdc->cyclic) {
  588. /*
  589. * Wait for 1 burst time for configure DMA for
  590. * next transfer.
  591. */
  592. udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
  593. tdc_configure_next_head_desc(tdc);
  594. }
  595. }
  596. end:
  597. spin_unlock_irqrestore(&tdc->lock, flags);
  598. return;
  599. }
  600. static void tegra_dma_terminate_all(struct dma_chan *dc)
  601. {
  602. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  603. struct tegra_dma_sg_req *sgreq;
  604. struct tegra_dma_desc *dma_desc;
  605. unsigned long flags;
  606. unsigned long status;
  607. bool was_busy;
  608. spin_lock_irqsave(&tdc->lock, flags);
  609. if (list_empty(&tdc->pending_sg_req)) {
  610. spin_unlock_irqrestore(&tdc->lock, flags);
  611. return;
  612. }
  613. if (!tdc->busy)
  614. goto skip_dma_stop;
  615. /* Pause DMA before checking the queue status */
  616. tegra_dma_pause(tdc, true);
  617. status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
  618. if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
  619. dev_dbg(tdc2dev(tdc), "%s():handling isr\n", __func__);
  620. tdc->isr_handler(tdc, true);
  621. status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
  622. }
  623. was_busy = tdc->busy;
  624. tegra_dma_stop(tdc);
  625. if (!list_empty(&tdc->pending_sg_req) && was_busy) {
  626. sgreq = list_first_entry(&tdc->pending_sg_req,
  627. typeof(*sgreq), node);
  628. sgreq->dma_desc->bytes_transferred +=
  629. get_current_xferred_count(tdc, sgreq, status);
  630. }
  631. tegra_dma_resume(tdc);
  632. skip_dma_stop:
  633. tegra_dma_abort_all(tdc);
  634. while (!list_empty(&tdc->cb_desc)) {
  635. dma_desc = list_first_entry(&tdc->cb_desc,
  636. typeof(*dma_desc), cb_node);
  637. list_del(&dma_desc->cb_node);
  638. dma_desc->cb_count = 0;
  639. }
  640. spin_unlock_irqrestore(&tdc->lock, flags);
  641. }
  642. static enum dma_status tegra_dma_tx_status(struct dma_chan *dc,
  643. dma_cookie_t cookie, struct dma_tx_state *txstate)
  644. {
  645. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  646. struct tegra_dma_desc *dma_desc;
  647. struct tegra_dma_sg_req *sg_req;
  648. enum dma_status ret;
  649. unsigned long flags;
  650. unsigned int residual;
  651. spin_lock_irqsave(&tdc->lock, flags);
  652. ret = dma_cookie_status(dc, cookie, txstate);
  653. if (ret == DMA_SUCCESS) {
  654. spin_unlock_irqrestore(&tdc->lock, flags);
  655. return ret;
  656. }
  657. /* Check on wait_ack desc status */
  658. list_for_each_entry(dma_desc, &tdc->free_dma_desc, node) {
  659. if (dma_desc->txd.cookie == cookie) {
  660. residual = dma_desc->bytes_requested -
  661. (dma_desc->bytes_transferred %
  662. dma_desc->bytes_requested);
  663. dma_set_residue(txstate, residual);
  664. ret = dma_desc->dma_status;
  665. spin_unlock_irqrestore(&tdc->lock, flags);
  666. return ret;
  667. }
  668. }
  669. /* Check in pending list */
  670. list_for_each_entry(sg_req, &tdc->pending_sg_req, node) {
  671. dma_desc = sg_req->dma_desc;
  672. if (dma_desc->txd.cookie == cookie) {
  673. residual = dma_desc->bytes_requested -
  674. (dma_desc->bytes_transferred %
  675. dma_desc->bytes_requested);
  676. dma_set_residue(txstate, residual);
  677. ret = dma_desc->dma_status;
  678. spin_unlock_irqrestore(&tdc->lock, flags);
  679. return ret;
  680. }
  681. }
  682. dev_dbg(tdc2dev(tdc), "cookie %d does not found\n", cookie);
  683. spin_unlock_irqrestore(&tdc->lock, flags);
  684. return ret;
  685. }
  686. static int tegra_dma_device_control(struct dma_chan *dc, enum dma_ctrl_cmd cmd,
  687. unsigned long arg)
  688. {
  689. switch (cmd) {
  690. case DMA_SLAVE_CONFIG:
  691. return tegra_dma_slave_config(dc,
  692. (struct dma_slave_config *)arg);
  693. case DMA_TERMINATE_ALL:
  694. tegra_dma_terminate_all(dc);
  695. return 0;
  696. default:
  697. break;
  698. }
  699. return -ENXIO;
  700. }
  701. static inline int get_bus_width(struct tegra_dma_channel *tdc,
  702. enum dma_slave_buswidth slave_bw)
  703. {
  704. switch (slave_bw) {
  705. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  706. return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_8;
  707. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  708. return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_16;
  709. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  710. return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32;
  711. case DMA_SLAVE_BUSWIDTH_8_BYTES:
  712. return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_64;
  713. default:
  714. dev_warn(tdc2dev(tdc),
  715. "slave bw is not supported, using 32bits\n");
  716. return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32;
  717. }
  718. }
  719. static inline int get_burst_size(struct tegra_dma_channel *tdc,
  720. u32 burst_size, enum dma_slave_buswidth slave_bw, int len)
  721. {
  722. int burst_byte;
  723. int burst_ahb_width;
  724. /*
  725. * burst_size from client is in terms of the bus_width.
  726. * convert them into AHB memory width which is 4 byte.
  727. */
  728. burst_byte = burst_size * slave_bw;
  729. burst_ahb_width = burst_byte / 4;
  730. /* If burst size is 0 then calculate the burst size based on length */
  731. if (!burst_ahb_width) {
  732. if (len & 0xF)
  733. return TEGRA_APBDMA_AHBSEQ_BURST_1;
  734. else if ((len >> 4) & 0x1)
  735. return TEGRA_APBDMA_AHBSEQ_BURST_4;
  736. else
  737. return TEGRA_APBDMA_AHBSEQ_BURST_8;
  738. }
  739. if (burst_ahb_width < 4)
  740. return TEGRA_APBDMA_AHBSEQ_BURST_1;
  741. else if (burst_ahb_width < 8)
  742. return TEGRA_APBDMA_AHBSEQ_BURST_4;
  743. else
  744. return TEGRA_APBDMA_AHBSEQ_BURST_8;
  745. }
  746. static int get_transfer_param(struct tegra_dma_channel *tdc,
  747. enum dma_transfer_direction direction, unsigned long *apb_addr,
  748. unsigned long *apb_seq, unsigned long *csr, unsigned int *burst_size,
  749. enum dma_slave_buswidth *slave_bw)
  750. {
  751. switch (direction) {
  752. case DMA_MEM_TO_DEV:
  753. *apb_addr = tdc->dma_sconfig.dst_addr;
  754. *apb_seq = get_bus_width(tdc, tdc->dma_sconfig.dst_addr_width);
  755. *burst_size = tdc->dma_sconfig.dst_maxburst;
  756. *slave_bw = tdc->dma_sconfig.dst_addr_width;
  757. *csr = TEGRA_APBDMA_CSR_DIR;
  758. return 0;
  759. case DMA_DEV_TO_MEM:
  760. *apb_addr = tdc->dma_sconfig.src_addr;
  761. *apb_seq = get_bus_width(tdc, tdc->dma_sconfig.src_addr_width);
  762. *burst_size = tdc->dma_sconfig.src_maxburst;
  763. *slave_bw = tdc->dma_sconfig.src_addr_width;
  764. *csr = 0;
  765. return 0;
  766. default:
  767. dev_err(tdc2dev(tdc), "Dma direction is not supported\n");
  768. return -EINVAL;
  769. }
  770. return -EINVAL;
  771. }
  772. static struct dma_async_tx_descriptor *tegra_dma_prep_slave_sg(
  773. struct dma_chan *dc, struct scatterlist *sgl, unsigned int sg_len,
  774. enum dma_transfer_direction direction, unsigned long flags,
  775. void *context)
  776. {
  777. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  778. struct tegra_dma_desc *dma_desc;
  779. unsigned int i;
  780. struct scatterlist *sg;
  781. unsigned long csr, ahb_seq, apb_ptr, apb_seq;
  782. struct list_head req_list;
  783. struct tegra_dma_sg_req *sg_req = NULL;
  784. u32 burst_size;
  785. enum dma_slave_buswidth slave_bw;
  786. int ret;
  787. if (!tdc->config_init) {
  788. dev_err(tdc2dev(tdc), "dma channel is not configured\n");
  789. return NULL;
  790. }
  791. if (sg_len < 1) {
  792. dev_err(tdc2dev(tdc), "Invalid segment length %d\n", sg_len);
  793. return NULL;
  794. }
  795. ret = get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
  796. &burst_size, &slave_bw);
  797. if (ret < 0)
  798. return NULL;
  799. INIT_LIST_HEAD(&req_list);
  800. ahb_seq = TEGRA_APBDMA_AHBSEQ_INTR_ENB;
  801. ahb_seq |= TEGRA_APBDMA_AHBSEQ_WRAP_NONE <<
  802. TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT;
  803. ahb_seq |= TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32;
  804. csr |= TEGRA_APBDMA_CSR_ONCE | TEGRA_APBDMA_CSR_FLOW;
  805. csr |= tdc->dma_sconfig.slave_id << TEGRA_APBDMA_CSR_REQ_SEL_SHIFT;
  806. if (flags & DMA_PREP_INTERRUPT)
  807. csr |= TEGRA_APBDMA_CSR_IE_EOC;
  808. apb_seq |= TEGRA_APBDMA_APBSEQ_WRAP_WORD_1;
  809. dma_desc = tegra_dma_desc_get(tdc);
  810. if (!dma_desc) {
  811. dev_err(tdc2dev(tdc), "Dma descriptors not available\n");
  812. return NULL;
  813. }
  814. INIT_LIST_HEAD(&dma_desc->tx_list);
  815. INIT_LIST_HEAD(&dma_desc->cb_node);
  816. dma_desc->cb_count = 0;
  817. dma_desc->bytes_requested = 0;
  818. dma_desc->bytes_transferred = 0;
  819. dma_desc->dma_status = DMA_IN_PROGRESS;
  820. /* Make transfer requests */
  821. for_each_sg(sgl, sg, sg_len, i) {
  822. u32 len, mem;
  823. mem = sg_dma_address(sg);
  824. len = sg_dma_len(sg);
  825. if ((len & 3) || (mem & 3) ||
  826. (len > tdc->tdma->chip_data->max_dma_count)) {
  827. dev_err(tdc2dev(tdc),
  828. "Dma length/memory address is not supported\n");
  829. tegra_dma_desc_put(tdc, dma_desc);
  830. return NULL;
  831. }
  832. sg_req = tegra_dma_sg_req_get(tdc);
  833. if (!sg_req) {
  834. dev_err(tdc2dev(tdc), "Dma sg-req not available\n");
  835. tegra_dma_desc_put(tdc, dma_desc);
  836. return NULL;
  837. }
  838. ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len);
  839. dma_desc->bytes_requested += len;
  840. sg_req->ch_regs.apb_ptr = apb_ptr;
  841. sg_req->ch_regs.ahb_ptr = mem;
  842. sg_req->ch_regs.csr = csr | ((len - 4) & 0xFFFC);
  843. sg_req->ch_regs.apb_seq = apb_seq;
  844. sg_req->ch_regs.ahb_seq = ahb_seq;
  845. sg_req->configured = false;
  846. sg_req->last_sg = false;
  847. sg_req->dma_desc = dma_desc;
  848. sg_req->req_len = len;
  849. list_add_tail(&sg_req->node, &dma_desc->tx_list);
  850. }
  851. sg_req->last_sg = true;
  852. if (flags & DMA_CTRL_ACK)
  853. dma_desc->txd.flags = DMA_CTRL_ACK;
  854. /*
  855. * Make sure that mode should not be conflicting with currently
  856. * configured mode.
  857. */
  858. if (!tdc->isr_handler) {
  859. tdc->isr_handler = handle_once_dma_done;
  860. tdc->cyclic = false;
  861. } else {
  862. if (tdc->cyclic) {
  863. dev_err(tdc2dev(tdc), "DMA configured in cyclic mode\n");
  864. tegra_dma_desc_put(tdc, dma_desc);
  865. return NULL;
  866. }
  867. }
  868. return &dma_desc->txd;
  869. }
  870. struct dma_async_tx_descriptor *tegra_dma_prep_dma_cyclic(
  871. struct dma_chan *dc, dma_addr_t buf_addr, size_t buf_len,
  872. size_t period_len, enum dma_transfer_direction direction,
  873. unsigned long flags, void *context)
  874. {
  875. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  876. struct tegra_dma_desc *dma_desc = NULL;
  877. struct tegra_dma_sg_req *sg_req = NULL;
  878. unsigned long csr, ahb_seq, apb_ptr, apb_seq;
  879. int len;
  880. size_t remain_len;
  881. dma_addr_t mem = buf_addr;
  882. u32 burst_size;
  883. enum dma_slave_buswidth slave_bw;
  884. int ret;
  885. if (!buf_len || !period_len) {
  886. dev_err(tdc2dev(tdc), "Invalid buffer/period len\n");
  887. return NULL;
  888. }
  889. if (!tdc->config_init) {
  890. dev_err(tdc2dev(tdc), "DMA slave is not configured\n");
  891. return NULL;
  892. }
  893. /*
  894. * We allow to take more number of requests till DMA is
  895. * not started. The driver will loop over all requests.
  896. * Once DMA is started then new requests can be queued only after
  897. * terminating the DMA.
  898. */
  899. if (tdc->busy) {
  900. dev_err(tdc2dev(tdc), "Request not allowed when dma running\n");
  901. return NULL;
  902. }
  903. /*
  904. * We only support cycle transfer when buf_len is multiple of
  905. * period_len.
  906. */
  907. if (buf_len % period_len) {
  908. dev_err(tdc2dev(tdc), "buf_len is not multiple of period_len\n");
  909. return NULL;
  910. }
  911. len = period_len;
  912. if ((len & 3) || (buf_addr & 3) ||
  913. (len > tdc->tdma->chip_data->max_dma_count)) {
  914. dev_err(tdc2dev(tdc), "Req len/mem address is not correct\n");
  915. return NULL;
  916. }
  917. ret = get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
  918. &burst_size, &slave_bw);
  919. if (ret < 0)
  920. return NULL;
  921. ahb_seq = TEGRA_APBDMA_AHBSEQ_INTR_ENB;
  922. ahb_seq |= TEGRA_APBDMA_AHBSEQ_WRAP_NONE <<
  923. TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT;
  924. ahb_seq |= TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32;
  925. csr |= TEGRA_APBDMA_CSR_FLOW | TEGRA_APBDMA_CSR_IE_EOC;
  926. csr |= tdc->dma_sconfig.slave_id << TEGRA_APBDMA_CSR_REQ_SEL_SHIFT;
  927. apb_seq |= TEGRA_APBDMA_APBSEQ_WRAP_WORD_1;
  928. dma_desc = tegra_dma_desc_get(tdc);
  929. if (!dma_desc) {
  930. dev_err(tdc2dev(tdc), "not enough descriptors available\n");
  931. return NULL;
  932. }
  933. INIT_LIST_HEAD(&dma_desc->tx_list);
  934. INIT_LIST_HEAD(&dma_desc->cb_node);
  935. dma_desc->cb_count = 0;
  936. dma_desc->bytes_transferred = 0;
  937. dma_desc->bytes_requested = buf_len;
  938. remain_len = buf_len;
  939. /* Split transfer equal to period size */
  940. while (remain_len) {
  941. sg_req = tegra_dma_sg_req_get(tdc);
  942. if (!sg_req) {
  943. dev_err(tdc2dev(tdc), "Dma sg-req not available\n");
  944. tegra_dma_desc_put(tdc, dma_desc);
  945. return NULL;
  946. }
  947. ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len);
  948. sg_req->ch_regs.apb_ptr = apb_ptr;
  949. sg_req->ch_regs.ahb_ptr = mem;
  950. sg_req->ch_regs.csr = csr | ((len - 4) & 0xFFFC);
  951. sg_req->ch_regs.apb_seq = apb_seq;
  952. sg_req->ch_regs.ahb_seq = ahb_seq;
  953. sg_req->configured = false;
  954. sg_req->half_done = false;
  955. sg_req->last_sg = false;
  956. sg_req->dma_desc = dma_desc;
  957. sg_req->req_len = len;
  958. list_add_tail(&sg_req->node, &dma_desc->tx_list);
  959. remain_len -= len;
  960. mem += len;
  961. }
  962. sg_req->last_sg = true;
  963. dma_desc->txd.flags = 0;
  964. /*
  965. * Make sure that mode should not be conflicting with currently
  966. * configured mode.
  967. */
  968. if (!tdc->isr_handler) {
  969. tdc->isr_handler = handle_cont_sngl_cycle_dma_done;
  970. tdc->cyclic = true;
  971. } else {
  972. if (!tdc->cyclic) {
  973. dev_err(tdc2dev(tdc), "DMA configuration conflict\n");
  974. tegra_dma_desc_put(tdc, dma_desc);
  975. return NULL;
  976. }
  977. }
  978. return &dma_desc->txd;
  979. }
  980. static int tegra_dma_alloc_chan_resources(struct dma_chan *dc)
  981. {
  982. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  983. struct tegra_dma *tdma = tdc->tdma;
  984. int ret;
  985. dma_cookie_init(&tdc->dma_chan);
  986. tdc->config_init = false;
  987. ret = clk_prepare_enable(tdma->dma_clk);
  988. if (ret < 0)
  989. dev_err(tdc2dev(tdc), "clk_prepare_enable failed: %d\n", ret);
  990. return ret;
  991. }
  992. static void tegra_dma_free_chan_resources(struct dma_chan *dc)
  993. {
  994. struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
  995. struct tegra_dma *tdma = tdc->tdma;
  996. struct tegra_dma_desc *dma_desc;
  997. struct tegra_dma_sg_req *sg_req;
  998. struct list_head dma_desc_list;
  999. struct list_head sg_req_list;
  1000. unsigned long flags;
  1001. INIT_LIST_HEAD(&dma_desc_list);
  1002. INIT_LIST_HEAD(&sg_req_list);
  1003. dev_dbg(tdc2dev(tdc), "Freeing channel %d\n", tdc->id);
  1004. if (tdc->busy)
  1005. tegra_dma_terminate_all(dc);
  1006. spin_lock_irqsave(&tdc->lock, flags);
  1007. list_splice_init(&tdc->pending_sg_req, &sg_req_list);
  1008. list_splice_init(&tdc->free_sg_req, &sg_req_list);
  1009. list_splice_init(&tdc->free_dma_desc, &dma_desc_list);
  1010. INIT_LIST_HEAD(&tdc->cb_desc);
  1011. tdc->config_init = false;
  1012. spin_unlock_irqrestore(&tdc->lock, flags);
  1013. while (!list_empty(&dma_desc_list)) {
  1014. dma_desc = list_first_entry(&dma_desc_list,
  1015. typeof(*dma_desc), node);
  1016. list_del(&dma_desc->node);
  1017. kfree(dma_desc);
  1018. }
  1019. while (!list_empty(&sg_req_list)) {
  1020. sg_req = list_first_entry(&sg_req_list, typeof(*sg_req), node);
  1021. list_del(&sg_req->node);
  1022. kfree(sg_req);
  1023. }
  1024. clk_disable_unprepare(tdma->dma_clk);
  1025. }
  1026. /* Tegra20 specific DMA controller information */
  1027. static const struct tegra_dma_chip_data tegra20_dma_chip_data = {
  1028. .nr_channels = 16,
  1029. .max_dma_count = 1024UL * 64,
  1030. .support_channel_pause = false,
  1031. };
  1032. #if defined(CONFIG_OF)
  1033. /* Tegra30 specific DMA controller information */
  1034. static const struct tegra_dma_chip_data tegra30_dma_chip_data = {
  1035. .nr_channels = 32,
  1036. .max_dma_count = 1024UL * 64,
  1037. .support_channel_pause = false,
  1038. };
  1039. /* Tegra114 specific DMA controller information */
  1040. static const struct tegra_dma_chip_data tegra114_dma_chip_data = {
  1041. .nr_channels = 32,
  1042. .max_dma_count = 1024UL * 64,
  1043. .support_channel_pause = true,
  1044. };
  1045. static const struct of_device_id tegra_dma_of_match[] = {
  1046. {
  1047. .compatible = "nvidia,tegra114-apbdma",
  1048. .data = &tegra114_dma_chip_data,
  1049. }, {
  1050. .compatible = "nvidia,tegra30-apbdma",
  1051. .data = &tegra30_dma_chip_data,
  1052. }, {
  1053. .compatible = "nvidia,tegra20-apbdma",
  1054. .data = &tegra20_dma_chip_data,
  1055. }, {
  1056. },
  1057. };
  1058. MODULE_DEVICE_TABLE(of, tegra_dma_of_match);
  1059. #endif
  1060. static int tegra_dma_probe(struct platform_device *pdev)
  1061. {
  1062. struct resource *res;
  1063. struct tegra_dma *tdma;
  1064. int ret;
  1065. int i;
  1066. const struct tegra_dma_chip_data *cdata = NULL;
  1067. if (pdev->dev.of_node) {
  1068. const struct of_device_id *match;
  1069. match = of_match_device(of_match_ptr(tegra_dma_of_match),
  1070. &pdev->dev);
  1071. if (!match) {
  1072. dev_err(&pdev->dev, "Error: No device match found\n");
  1073. return -ENODEV;
  1074. }
  1075. cdata = match->data;
  1076. } else {
  1077. /* If no device tree then fallback to tegra20 */
  1078. cdata = &tegra20_dma_chip_data;
  1079. }
  1080. tdma = devm_kzalloc(&pdev->dev, sizeof(*tdma) + cdata->nr_channels *
  1081. sizeof(struct tegra_dma_channel), GFP_KERNEL);
  1082. if (!tdma) {
  1083. dev_err(&pdev->dev, "Error: memory allocation failed\n");
  1084. return -ENOMEM;
  1085. }
  1086. tdma->dev = &pdev->dev;
  1087. tdma->chip_data = cdata;
  1088. platform_set_drvdata(pdev, tdma);
  1089. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1090. if (!res) {
  1091. dev_err(&pdev->dev, "No mem resource for DMA\n");
  1092. return -EINVAL;
  1093. }
  1094. tdma->base_addr = devm_request_and_ioremap(&pdev->dev, res);
  1095. if (!tdma->base_addr) {
  1096. dev_err(&pdev->dev,
  1097. "Cannot request memregion/iomap dma address\n");
  1098. return -EADDRNOTAVAIL;
  1099. }
  1100. tdma->dma_clk = devm_clk_get(&pdev->dev, NULL);
  1101. if (IS_ERR(tdma->dma_clk)) {
  1102. dev_err(&pdev->dev, "Error: Missing controller clock\n");
  1103. return PTR_ERR(tdma->dma_clk);
  1104. }
  1105. spin_lock_init(&tdma->global_lock);
  1106. pm_runtime_enable(&pdev->dev);
  1107. if (!pm_runtime_enabled(&pdev->dev)) {
  1108. ret = tegra_dma_runtime_resume(&pdev->dev);
  1109. if (ret) {
  1110. dev_err(&pdev->dev, "dma_runtime_resume failed %d\n",
  1111. ret);
  1112. goto err_pm_disable;
  1113. }
  1114. }
  1115. /* Enable clock before accessing registers */
  1116. ret = clk_prepare_enable(tdma->dma_clk);
  1117. if (ret < 0) {
  1118. dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
  1119. goto err_pm_disable;
  1120. }
  1121. /* Reset DMA controller */
  1122. tegra_periph_reset_assert(tdma->dma_clk);
  1123. udelay(2);
  1124. tegra_periph_reset_deassert(tdma->dma_clk);
  1125. /* Enable global DMA registers */
  1126. tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE);
  1127. tdma_write(tdma, TEGRA_APBDMA_CONTROL, 0);
  1128. tdma_write(tdma, TEGRA_APBDMA_IRQ_MASK_SET, 0xFFFFFFFFul);
  1129. clk_disable_unprepare(tdma->dma_clk);
  1130. INIT_LIST_HEAD(&tdma->dma_dev.channels);
  1131. for (i = 0; i < cdata->nr_channels; i++) {
  1132. struct tegra_dma_channel *tdc = &tdma->channels[i];
  1133. tdc->chan_base_offset = TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET +
  1134. i * TEGRA_APBDMA_CHANNEL_REGISTER_SIZE;
  1135. res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
  1136. if (!res) {
  1137. ret = -EINVAL;
  1138. dev_err(&pdev->dev, "No irq resource for chan %d\n", i);
  1139. goto err_irq;
  1140. }
  1141. tdc->irq = res->start;
  1142. snprintf(tdc->name, sizeof(tdc->name), "apbdma.%d", i);
  1143. ret = devm_request_irq(&pdev->dev, tdc->irq,
  1144. tegra_dma_isr, 0, tdc->name, tdc);
  1145. if (ret) {
  1146. dev_err(&pdev->dev,
  1147. "request_irq failed with err %d channel %d\n",
  1148. i, ret);
  1149. goto err_irq;
  1150. }
  1151. tdc->dma_chan.device = &tdma->dma_dev;
  1152. dma_cookie_init(&tdc->dma_chan);
  1153. list_add_tail(&tdc->dma_chan.device_node,
  1154. &tdma->dma_dev.channels);
  1155. tdc->tdma = tdma;
  1156. tdc->id = i;
  1157. tasklet_init(&tdc->tasklet, tegra_dma_tasklet,
  1158. (unsigned long)tdc);
  1159. spin_lock_init(&tdc->lock);
  1160. INIT_LIST_HEAD(&tdc->pending_sg_req);
  1161. INIT_LIST_HEAD(&tdc->free_sg_req);
  1162. INIT_LIST_HEAD(&tdc->free_dma_desc);
  1163. INIT_LIST_HEAD(&tdc->cb_desc);
  1164. }
  1165. dma_cap_set(DMA_SLAVE, tdma->dma_dev.cap_mask);
  1166. dma_cap_set(DMA_PRIVATE, tdma->dma_dev.cap_mask);
  1167. dma_cap_set(DMA_CYCLIC, tdma->dma_dev.cap_mask);
  1168. tdma->dma_dev.dev = &pdev->dev;
  1169. tdma->dma_dev.device_alloc_chan_resources =
  1170. tegra_dma_alloc_chan_resources;
  1171. tdma->dma_dev.device_free_chan_resources =
  1172. tegra_dma_free_chan_resources;
  1173. tdma->dma_dev.device_prep_slave_sg = tegra_dma_prep_slave_sg;
  1174. tdma->dma_dev.device_prep_dma_cyclic = tegra_dma_prep_dma_cyclic;
  1175. tdma->dma_dev.device_control = tegra_dma_device_control;
  1176. tdma->dma_dev.device_tx_status = tegra_dma_tx_status;
  1177. tdma->dma_dev.device_issue_pending = tegra_dma_issue_pending;
  1178. ret = dma_async_device_register(&tdma->dma_dev);
  1179. if (ret < 0) {
  1180. dev_err(&pdev->dev,
  1181. "Tegra20 APB DMA driver registration failed %d\n", ret);
  1182. goto err_irq;
  1183. }
  1184. dev_info(&pdev->dev, "Tegra20 APB DMA driver register %d channels\n",
  1185. cdata->nr_channels);
  1186. return 0;
  1187. err_irq:
  1188. while (--i >= 0) {
  1189. struct tegra_dma_channel *tdc = &tdma->channels[i];
  1190. tasklet_kill(&tdc->tasklet);
  1191. }
  1192. err_pm_disable:
  1193. pm_runtime_disable(&pdev->dev);
  1194. if (!pm_runtime_status_suspended(&pdev->dev))
  1195. tegra_dma_runtime_suspend(&pdev->dev);
  1196. return ret;
  1197. }
  1198. static int __devexit tegra_dma_remove(struct platform_device *pdev)
  1199. {
  1200. struct tegra_dma *tdma = platform_get_drvdata(pdev);
  1201. int i;
  1202. struct tegra_dma_channel *tdc;
  1203. dma_async_device_unregister(&tdma->dma_dev);
  1204. for (i = 0; i < tdma->chip_data->nr_channels; ++i) {
  1205. tdc = &tdma->channels[i];
  1206. tasklet_kill(&tdc->tasklet);
  1207. }
  1208. pm_runtime_disable(&pdev->dev);
  1209. if (!pm_runtime_status_suspended(&pdev->dev))
  1210. tegra_dma_runtime_suspend(&pdev->dev);
  1211. return 0;
  1212. }
  1213. static int tegra_dma_runtime_suspend(struct device *dev)
  1214. {
  1215. struct platform_device *pdev = to_platform_device(dev);
  1216. struct tegra_dma *tdma = platform_get_drvdata(pdev);
  1217. clk_disable_unprepare(tdma->dma_clk);
  1218. return 0;
  1219. }
  1220. static int tegra_dma_runtime_resume(struct device *dev)
  1221. {
  1222. struct platform_device *pdev = to_platform_device(dev);
  1223. struct tegra_dma *tdma = platform_get_drvdata(pdev);
  1224. int ret;
  1225. ret = clk_prepare_enable(tdma->dma_clk);
  1226. if (ret < 0) {
  1227. dev_err(dev, "clk_enable failed: %d\n", ret);
  1228. return ret;
  1229. }
  1230. return 0;
  1231. }
  1232. static const struct dev_pm_ops tegra_dma_dev_pm_ops __devinitconst = {
  1233. #ifdef CONFIG_PM_RUNTIME
  1234. .runtime_suspend = tegra_dma_runtime_suspend,
  1235. .runtime_resume = tegra_dma_runtime_resume,
  1236. #endif
  1237. };
  1238. static struct platform_driver tegra_dmac_driver = {
  1239. .driver = {
  1240. .name = "tegra-apbdma",
  1241. .owner = THIS_MODULE,
  1242. .pm = &tegra_dma_dev_pm_ops,
  1243. .of_match_table = of_match_ptr(tegra_dma_of_match),
  1244. },
  1245. .probe = tegra_dma_probe,
  1246. .remove = tegra_dma_remove,
  1247. };
  1248. module_platform_driver(tegra_dmac_driver);
  1249. MODULE_ALIAS("platform:tegra20-apbdma");
  1250. MODULE_DESCRIPTION("NVIDIA Tegra APB DMA Controller driver");
  1251. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1252. MODULE_LICENSE("GPL v2");