tegra20-apb-dma.c 44 KB

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