intel_mid_dma.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445
  1. /*
  2. * intel_mid_dma.c - Intel Langwell DMA Drivers
  3. *
  4. * Copyright (C) 2008-10 Intel Corp
  5. * Author: Vinod Koul <vinod.koul@intel.com>
  6. * The driver design is based on dw_dmac driver
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. *
  24. *
  25. */
  26. #include <linux/pci.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/intel_mid_dma.h>
  30. #define MAX_CHAN 4 /*max ch across controllers*/
  31. #include "intel_mid_dma_regs.h"
  32. #define INTEL_MID_DMAC1_ID 0x0814
  33. #define INTEL_MID_DMAC2_ID 0x0813
  34. #define INTEL_MID_GP_DMAC2_ID 0x0827
  35. #define INTEL_MFLD_DMAC1_ID 0x0830
  36. #define LNW_PERIPHRAL_MASK_BASE 0xFFAE8008
  37. #define LNW_PERIPHRAL_MASK_SIZE 0x10
  38. #define LNW_PERIPHRAL_STATUS 0x0
  39. #define LNW_PERIPHRAL_MASK 0x8
  40. struct intel_mid_dma_probe_info {
  41. u8 max_chan;
  42. u8 ch_base;
  43. u16 block_size;
  44. u32 pimr_mask;
  45. };
  46. #define INFO(_max_chan, _ch_base, _block_size, _pimr_mask) \
  47. ((kernel_ulong_t)&(struct intel_mid_dma_probe_info) { \
  48. .max_chan = (_max_chan), \
  49. .ch_base = (_ch_base), \
  50. .block_size = (_block_size), \
  51. .pimr_mask = (_pimr_mask), \
  52. })
  53. /*****************************************************************************
  54. Utility Functions*/
  55. /**
  56. * get_ch_index - convert status to channel
  57. * @status: status mask
  58. * @base: dma ch base value
  59. *
  60. * Modify the status mask and return the channel index needing
  61. * attention (or -1 if neither)
  62. */
  63. static int get_ch_index(int *status, unsigned int base)
  64. {
  65. int i;
  66. for (i = 0; i < MAX_CHAN; i++) {
  67. if (*status & (1 << (i + base))) {
  68. *status = *status & ~(1 << (i + base));
  69. pr_debug("MDMA: index %d New status %x\n", i, *status);
  70. return i;
  71. }
  72. }
  73. return -1;
  74. }
  75. /**
  76. * get_block_ts - calculates dma transaction length
  77. * @len: dma transfer length
  78. * @tx_width: dma transfer src width
  79. * @block_size: dma controller max block size
  80. *
  81. * Based on src width calculate the DMA trsaction length in data items
  82. * return data items or FFFF if exceeds max length for block
  83. */
  84. static int get_block_ts(int len, int tx_width, int block_size)
  85. {
  86. int byte_width = 0, block_ts = 0;
  87. switch (tx_width) {
  88. case DMA_SLAVE_BUSWIDTH_1_BYTE:
  89. byte_width = 1;
  90. break;
  91. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  92. byte_width = 2;
  93. break;
  94. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  95. default:
  96. byte_width = 4;
  97. break;
  98. }
  99. block_ts = len/byte_width;
  100. if (block_ts > block_size)
  101. block_ts = 0xFFFF;
  102. return block_ts;
  103. }
  104. /*****************************************************************************
  105. DMAC1 interrupt Functions*/
  106. /**
  107. * dmac1_mask_periphral_intr - mask the periphral interrupt
  108. * @midc: dma channel for which masking is required
  109. *
  110. * Masks the DMA periphral interrupt
  111. * this is valid for DMAC1 family controllers only
  112. * This controller should have periphral mask registers already mapped
  113. */
  114. static void dmac1_mask_periphral_intr(struct intel_mid_dma_chan *midc)
  115. {
  116. u32 pimr;
  117. struct middma_device *mid = to_middma_device(midc->chan.device);
  118. if (mid->pimr_mask) {
  119. pimr = readl(mid->mask_reg + LNW_PERIPHRAL_MASK);
  120. pimr |= mid->pimr_mask;
  121. writel(pimr, mid->mask_reg + LNW_PERIPHRAL_MASK);
  122. }
  123. return;
  124. }
  125. /**
  126. * dmac1_unmask_periphral_intr - unmask the periphral interrupt
  127. * @midc: dma channel for which masking is required
  128. *
  129. * UnMasks the DMA periphral interrupt,
  130. * this is valid for DMAC1 family controllers only
  131. * This controller should have periphral mask registers already mapped
  132. */
  133. static void dmac1_unmask_periphral_intr(struct intel_mid_dma_chan *midc)
  134. {
  135. u32 pimr;
  136. struct middma_device *mid = to_middma_device(midc->chan.device);
  137. if (mid->pimr_mask) {
  138. pimr = readl(mid->mask_reg + LNW_PERIPHRAL_MASK);
  139. pimr &= ~mid->pimr_mask;
  140. writel(pimr, mid->mask_reg + LNW_PERIPHRAL_MASK);
  141. }
  142. return;
  143. }
  144. /**
  145. * enable_dma_interrupt - enable the periphral interrupt
  146. * @midc: dma channel for which enable interrupt is required
  147. *
  148. * Enable the DMA periphral interrupt,
  149. * this is valid for DMAC1 family controllers only
  150. * This controller should have periphral mask registers already mapped
  151. */
  152. static void enable_dma_interrupt(struct intel_mid_dma_chan *midc)
  153. {
  154. dmac1_unmask_periphral_intr(midc);
  155. /*en ch interrupts*/
  156. iowrite32(UNMASK_INTR_REG(midc->ch_id), midc->dma_base + MASK_TFR);
  157. iowrite32(UNMASK_INTR_REG(midc->ch_id), midc->dma_base + MASK_ERR);
  158. return;
  159. }
  160. /**
  161. * disable_dma_interrupt - disable the periphral interrupt
  162. * @midc: dma channel for which disable interrupt is required
  163. *
  164. * Disable the DMA periphral interrupt,
  165. * this is valid for DMAC1 family controllers only
  166. * This controller should have periphral mask registers already mapped
  167. */
  168. static void disable_dma_interrupt(struct intel_mid_dma_chan *midc)
  169. {
  170. /*Check LPE PISR, make sure fwd is disabled*/
  171. dmac1_mask_periphral_intr(midc);
  172. iowrite32(MASK_INTR_REG(midc->ch_id), midc->dma_base + MASK_BLOCK);
  173. iowrite32(MASK_INTR_REG(midc->ch_id), midc->dma_base + MASK_TFR);
  174. iowrite32(MASK_INTR_REG(midc->ch_id), midc->dma_base + MASK_ERR);
  175. return;
  176. }
  177. /*****************************************************************************
  178. DMA channel helper Functions*/
  179. /**
  180. * mid_desc_get - get a descriptor
  181. * @midc: dma channel for which descriptor is required
  182. *
  183. * Obtain a descriptor for the channel. Returns NULL if none are free.
  184. * Once the descriptor is returned it is private until put on another
  185. * list or freed
  186. */
  187. static struct intel_mid_dma_desc *midc_desc_get(struct intel_mid_dma_chan *midc)
  188. {
  189. struct intel_mid_dma_desc *desc, *_desc;
  190. struct intel_mid_dma_desc *ret = NULL;
  191. spin_lock_bh(&midc->lock);
  192. list_for_each_entry_safe(desc, _desc, &midc->free_list, desc_node) {
  193. if (async_tx_test_ack(&desc->txd)) {
  194. list_del(&desc->desc_node);
  195. ret = desc;
  196. break;
  197. }
  198. }
  199. spin_unlock_bh(&midc->lock);
  200. return ret;
  201. }
  202. /**
  203. * mid_desc_put - put a descriptor
  204. * @midc: dma channel for which descriptor is required
  205. * @desc: descriptor to put
  206. *
  207. * Return a descriptor from lwn_desc_get back to the free pool
  208. */
  209. static void midc_desc_put(struct intel_mid_dma_chan *midc,
  210. struct intel_mid_dma_desc *desc)
  211. {
  212. if (desc) {
  213. spin_lock_bh(&midc->lock);
  214. list_add_tail(&desc->desc_node, &midc->free_list);
  215. spin_unlock_bh(&midc->lock);
  216. }
  217. }
  218. /**
  219. * midc_dostart - begin a DMA transaction
  220. * @midc: channel for which txn is to be started
  221. * @first: first descriptor of series
  222. *
  223. * Load a transaction into the engine. This must be called with midc->lock
  224. * held and bh disabled.
  225. */
  226. static void midc_dostart(struct intel_mid_dma_chan *midc,
  227. struct intel_mid_dma_desc *first)
  228. {
  229. struct middma_device *mid = to_middma_device(midc->chan.device);
  230. /* channel is idle */
  231. if (midc->busy && test_ch_en(midc->dma_base, midc->ch_id)) {
  232. /*error*/
  233. pr_err("ERR_MDMA: channel is busy in start\n");
  234. /* The tasklet will hopefully advance the queue... */
  235. return;
  236. }
  237. midc->busy = true;
  238. /*write registers and en*/
  239. iowrite32(first->sar, midc->ch_regs + SAR);
  240. iowrite32(first->dar, midc->ch_regs + DAR);
  241. iowrite32(first->lli_phys, midc->ch_regs + LLP);
  242. iowrite32(first->cfg_hi, midc->ch_regs + CFG_HIGH);
  243. iowrite32(first->cfg_lo, midc->ch_regs + CFG_LOW);
  244. iowrite32(first->ctl_lo, midc->ch_regs + CTL_LOW);
  245. iowrite32(first->ctl_hi, midc->ch_regs + CTL_HIGH);
  246. pr_debug("MDMA:TX SAR %x,DAR %x,CFGL %x,CFGH %x,CTLH %x, CTLL %x\n",
  247. (int)first->sar, (int)first->dar, first->cfg_hi,
  248. first->cfg_lo, first->ctl_hi, first->ctl_lo);
  249. first->status = DMA_IN_PROGRESS;
  250. iowrite32(ENABLE_CHANNEL(midc->ch_id), mid->dma_base + DMA_CHAN_EN);
  251. }
  252. /**
  253. * midc_descriptor_complete - process completed descriptor
  254. * @midc: channel owning the descriptor
  255. * @desc: the descriptor itself
  256. *
  257. * Process a completed descriptor and perform any callbacks upon
  258. * the completion. The completion handling drops the lock during the
  259. * callbacks but must be called with the lock held.
  260. */
  261. static void midc_descriptor_complete(struct intel_mid_dma_chan *midc,
  262. struct intel_mid_dma_desc *desc)
  263. {
  264. struct dma_async_tx_descriptor *txd = &desc->txd;
  265. dma_async_tx_callback callback_txd = NULL;
  266. struct intel_mid_dma_lli *llitem;
  267. void *param_txd = NULL;
  268. midc->completed = txd->cookie;
  269. callback_txd = txd->callback;
  270. param_txd = txd->callback_param;
  271. if (desc->lli != NULL) {
  272. /*clear the DONE bit of completed LLI in memory*/
  273. llitem = desc->lli + desc->current_lli;
  274. llitem->ctl_hi &= CLEAR_DONE;
  275. if (desc->current_lli < desc->lli_length-1)
  276. (desc->current_lli)++;
  277. else
  278. desc->current_lli = 0;
  279. }
  280. spin_unlock_bh(&midc->lock);
  281. if (callback_txd) {
  282. pr_debug("MDMA: TXD callback set ... calling\n");
  283. callback_txd(param_txd);
  284. }
  285. if (midc->raw_tfr) {
  286. desc->status = DMA_SUCCESS;
  287. if (desc->lli != NULL) {
  288. pci_pool_free(desc->lli_pool, desc->lli,
  289. desc->lli_phys);
  290. pci_pool_destroy(desc->lli_pool);
  291. }
  292. list_move(&desc->desc_node, &midc->free_list);
  293. midc->busy = false;
  294. }
  295. spin_lock_bh(&midc->lock);
  296. }
  297. /**
  298. * midc_scan_descriptors - check the descriptors in channel
  299. * mark completed when tx is completete
  300. * @mid: device
  301. * @midc: channel to scan
  302. *
  303. * Walk the descriptor chain for the device and process any entries
  304. * that are complete.
  305. */
  306. static void midc_scan_descriptors(struct middma_device *mid,
  307. struct intel_mid_dma_chan *midc)
  308. {
  309. struct intel_mid_dma_desc *desc = NULL, *_desc = NULL;
  310. /*tx is complete*/
  311. list_for_each_entry_safe(desc, _desc, &midc->active_list, desc_node) {
  312. if (desc->status == DMA_IN_PROGRESS)
  313. midc_descriptor_complete(midc, desc);
  314. }
  315. return;
  316. }
  317. /**
  318. * midc_lli_fill_sg - Helper function to convert
  319. * SG list to Linked List Items.
  320. *@midc: Channel
  321. *@desc: DMA descriptor
  322. *@sglist: Pointer to SG list
  323. *@sglen: SG list length
  324. *@flags: DMA transaction flags
  325. *
  326. * Walk through the SG list and convert the SG list into Linked
  327. * List Items (LLI).
  328. */
  329. static int midc_lli_fill_sg(struct intel_mid_dma_chan *midc,
  330. struct intel_mid_dma_desc *desc,
  331. struct scatterlist *sglist,
  332. unsigned int sglen,
  333. unsigned int flags)
  334. {
  335. struct intel_mid_dma_slave *mids;
  336. struct scatterlist *sg;
  337. dma_addr_t lli_next, sg_phy_addr;
  338. struct intel_mid_dma_lli *lli_bloc_desc;
  339. union intel_mid_dma_ctl_lo ctl_lo;
  340. union intel_mid_dma_ctl_hi ctl_hi;
  341. int i;
  342. pr_debug("MDMA: Entered midc_lli_fill_sg\n");
  343. mids = midc->mid_slave;
  344. lli_bloc_desc = desc->lli;
  345. lli_next = desc->lli_phys;
  346. ctl_lo.ctl_lo = desc->ctl_lo;
  347. ctl_hi.ctl_hi = desc->ctl_hi;
  348. for_each_sg(sglist, sg, sglen, i) {
  349. /*Populate CTL_LOW and LLI values*/
  350. if (i != sglen - 1) {
  351. lli_next = lli_next +
  352. sizeof(struct intel_mid_dma_lli);
  353. } else {
  354. /*Check for circular list, otherwise terminate LLI to ZERO*/
  355. if (flags & DMA_PREP_CIRCULAR_LIST) {
  356. pr_debug("MDMA: LLI is configured in circular mode\n");
  357. lli_next = desc->lli_phys;
  358. } else {
  359. lli_next = 0;
  360. ctl_lo.ctlx.llp_dst_en = 0;
  361. ctl_lo.ctlx.llp_src_en = 0;
  362. }
  363. }
  364. /*Populate CTL_HI values*/
  365. ctl_hi.ctlx.block_ts = get_block_ts(sg->length,
  366. desc->width,
  367. midc->dma->block_size);
  368. /*Populate SAR and DAR values*/
  369. sg_phy_addr = sg_phys(sg);
  370. if (desc->dirn == DMA_TO_DEVICE) {
  371. lli_bloc_desc->sar = sg_phy_addr;
  372. lli_bloc_desc->dar = mids->dma_slave.dst_addr;
  373. } else if (desc->dirn == DMA_FROM_DEVICE) {
  374. lli_bloc_desc->sar = mids->dma_slave.src_addr;
  375. lli_bloc_desc->dar = sg_phy_addr;
  376. }
  377. /*Copy values into block descriptor in system memroy*/
  378. lli_bloc_desc->llp = lli_next;
  379. lli_bloc_desc->ctl_lo = ctl_lo.ctl_lo;
  380. lli_bloc_desc->ctl_hi = ctl_hi.ctl_hi;
  381. lli_bloc_desc++;
  382. }
  383. /*Copy very first LLI values to descriptor*/
  384. desc->ctl_lo = desc->lli->ctl_lo;
  385. desc->ctl_hi = desc->lli->ctl_hi;
  386. desc->sar = desc->lli->sar;
  387. desc->dar = desc->lli->dar;
  388. return 0;
  389. }
  390. /*****************************************************************************
  391. DMA engine callback Functions*/
  392. /**
  393. * intel_mid_dma_tx_submit - callback to submit DMA transaction
  394. * @tx: dma engine descriptor
  395. *
  396. * Submit the DMA trasaction for this descriptor, start if ch idle
  397. */
  398. static dma_cookie_t intel_mid_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  399. {
  400. struct intel_mid_dma_desc *desc = to_intel_mid_dma_desc(tx);
  401. struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(tx->chan);
  402. dma_cookie_t cookie;
  403. spin_lock_bh(&midc->lock);
  404. cookie = midc->chan.cookie;
  405. if (++cookie < 0)
  406. cookie = 1;
  407. midc->chan.cookie = cookie;
  408. desc->txd.cookie = cookie;
  409. if (list_empty(&midc->active_list))
  410. list_add_tail(&desc->desc_node, &midc->active_list);
  411. else
  412. list_add_tail(&desc->desc_node, &midc->queue);
  413. midc_dostart(midc, desc);
  414. spin_unlock_bh(&midc->lock);
  415. return cookie;
  416. }
  417. /**
  418. * intel_mid_dma_issue_pending - callback to issue pending txn
  419. * @chan: chan where pending trascation needs to be checked and submitted
  420. *
  421. * Call for scan to issue pending descriptors
  422. */
  423. static void intel_mid_dma_issue_pending(struct dma_chan *chan)
  424. {
  425. struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
  426. spin_lock_bh(&midc->lock);
  427. if (!list_empty(&midc->queue))
  428. midc_scan_descriptors(to_middma_device(chan->device), midc);
  429. spin_unlock_bh(&midc->lock);
  430. }
  431. /**
  432. * intel_mid_dma_tx_status - Return status of txn
  433. * @chan: chan for where status needs to be checked
  434. * @cookie: cookie for txn
  435. * @txstate: DMA txn state
  436. *
  437. * Return status of DMA txn
  438. */
  439. static enum dma_status intel_mid_dma_tx_status(struct dma_chan *chan,
  440. dma_cookie_t cookie,
  441. struct dma_tx_state *txstate)
  442. {
  443. struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
  444. dma_cookie_t last_used;
  445. dma_cookie_t last_complete;
  446. int ret;
  447. last_complete = midc->completed;
  448. last_used = chan->cookie;
  449. ret = dma_async_is_complete(cookie, last_complete, last_used);
  450. if (ret != DMA_SUCCESS) {
  451. midc_scan_descriptors(to_middma_device(chan->device), midc);
  452. last_complete = midc->completed;
  453. last_used = chan->cookie;
  454. ret = dma_async_is_complete(cookie, last_complete, last_used);
  455. }
  456. if (txstate) {
  457. txstate->last = last_complete;
  458. txstate->used = last_used;
  459. txstate->residue = 0;
  460. }
  461. return ret;
  462. }
  463. static int dma_slave_control(struct dma_chan *chan, unsigned long arg)
  464. {
  465. struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
  466. struct dma_slave_config *slave = (struct dma_slave_config *)arg;
  467. struct intel_mid_dma_slave *mid_slave;
  468. BUG_ON(!midc);
  469. BUG_ON(!slave);
  470. pr_debug("MDMA: slave control called\n");
  471. mid_slave = to_intel_mid_dma_slave(slave);
  472. BUG_ON(!mid_slave);
  473. midc->mid_slave = mid_slave;
  474. return 0;
  475. }
  476. /**
  477. * intel_mid_dma_device_control - DMA device control
  478. * @chan: chan for DMA control
  479. * @cmd: control cmd
  480. * @arg: cmd arg value
  481. *
  482. * Perform DMA control command
  483. */
  484. static int intel_mid_dma_device_control(struct dma_chan *chan,
  485. enum dma_ctrl_cmd cmd, unsigned long arg)
  486. {
  487. struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
  488. struct middma_device *mid = to_middma_device(chan->device);
  489. struct intel_mid_dma_desc *desc, *_desc;
  490. union intel_mid_dma_cfg_lo cfg_lo;
  491. if (cmd == DMA_SLAVE_CONFIG)
  492. return dma_slave_control(chan, arg);
  493. if (cmd != DMA_TERMINATE_ALL)
  494. return -ENXIO;
  495. spin_lock_bh(&midc->lock);
  496. if (midc->busy == false) {
  497. spin_unlock_bh(&midc->lock);
  498. return 0;
  499. }
  500. /*Suspend and disable the channel*/
  501. cfg_lo.cfg_lo = ioread32(midc->ch_regs + CFG_LOW);
  502. cfg_lo.cfgx.ch_susp = 1;
  503. iowrite32(cfg_lo.cfg_lo, midc->ch_regs + CFG_LOW);
  504. iowrite32(DISABLE_CHANNEL(midc->ch_id), mid->dma_base + DMA_CHAN_EN);
  505. midc->busy = false;
  506. /* Disable interrupts */
  507. disable_dma_interrupt(midc);
  508. midc->descs_allocated = 0;
  509. spin_unlock_bh(&midc->lock);
  510. list_for_each_entry_safe(desc, _desc, &midc->active_list, desc_node) {
  511. if (desc->lli != NULL) {
  512. pci_pool_free(desc->lli_pool, desc->lli,
  513. desc->lli_phys);
  514. pci_pool_destroy(desc->lli_pool);
  515. }
  516. list_move(&desc->desc_node, &midc->free_list);
  517. }
  518. return 0;
  519. }
  520. /**
  521. * intel_mid_dma_prep_memcpy - Prep memcpy txn
  522. * @chan: chan for DMA transfer
  523. * @dest: destn address
  524. * @src: src address
  525. * @len: DMA transfer len
  526. * @flags: DMA flags
  527. *
  528. * Perform a DMA memcpy. Note we support slave periphral DMA transfers only
  529. * The periphral txn details should be filled in slave structure properly
  530. * Returns the descriptor for this txn
  531. */
  532. static struct dma_async_tx_descriptor *intel_mid_dma_prep_memcpy(
  533. struct dma_chan *chan, dma_addr_t dest,
  534. dma_addr_t src, size_t len, unsigned long flags)
  535. {
  536. struct intel_mid_dma_chan *midc;
  537. struct intel_mid_dma_desc *desc = NULL;
  538. struct intel_mid_dma_slave *mids;
  539. union intel_mid_dma_ctl_lo ctl_lo;
  540. union intel_mid_dma_ctl_hi ctl_hi;
  541. union intel_mid_dma_cfg_lo cfg_lo;
  542. union intel_mid_dma_cfg_hi cfg_hi;
  543. enum dma_slave_buswidth width;
  544. pr_debug("MDMA: Prep for memcpy\n");
  545. BUG_ON(!chan);
  546. if (!len)
  547. return NULL;
  548. midc = to_intel_mid_dma_chan(chan);
  549. BUG_ON(!midc);
  550. mids = midc->mid_slave;
  551. BUG_ON(!mids);
  552. pr_debug("MDMA:called for DMA %x CH %d Length %zu\n",
  553. midc->dma->pci_id, midc->ch_id, len);
  554. pr_debug("MDMA:Cfg passed Mode %x, Dirn %x, HS %x, Width %x\n",
  555. mids->cfg_mode, mids->dma_slave.direction,
  556. mids->hs_mode, mids->dma_slave.src_addr_width);
  557. /*calculate CFG_LO*/
  558. if (mids->hs_mode == LNW_DMA_SW_HS) {
  559. cfg_lo.cfg_lo = 0;
  560. cfg_lo.cfgx.hs_sel_dst = 1;
  561. cfg_lo.cfgx.hs_sel_src = 1;
  562. } else if (mids->hs_mode == LNW_DMA_HW_HS)
  563. cfg_lo.cfg_lo = 0x00000;
  564. /*calculate CFG_HI*/
  565. if (mids->cfg_mode == LNW_DMA_MEM_TO_MEM) {
  566. /*SW HS only*/
  567. cfg_hi.cfg_hi = 0;
  568. } else {
  569. cfg_hi.cfg_hi = 0;
  570. if (midc->dma->pimr_mask) {
  571. cfg_hi.cfgx.protctl = 0x0; /*default value*/
  572. cfg_hi.cfgx.fifo_mode = 1;
  573. if (mids->dma_slave.direction == DMA_TO_DEVICE) {
  574. cfg_hi.cfgx.src_per = 0;
  575. if (mids->device_instance == 0)
  576. cfg_hi.cfgx.dst_per = 3;
  577. if (mids->device_instance == 1)
  578. cfg_hi.cfgx.dst_per = 1;
  579. } else if (mids->dma_slave.direction == DMA_FROM_DEVICE) {
  580. if (mids->device_instance == 0)
  581. cfg_hi.cfgx.src_per = 2;
  582. if (mids->device_instance == 1)
  583. cfg_hi.cfgx.src_per = 0;
  584. cfg_hi.cfgx.dst_per = 0;
  585. }
  586. } else {
  587. cfg_hi.cfgx.protctl = 0x1; /*default value*/
  588. cfg_hi.cfgx.src_per = cfg_hi.cfgx.dst_per =
  589. midc->ch_id - midc->dma->chan_base;
  590. }
  591. }
  592. /*calculate CTL_HI*/
  593. ctl_hi.ctlx.reser = 0;
  594. ctl_hi.ctlx.done = 0;
  595. width = mids->dma_slave.src_addr_width;
  596. ctl_hi.ctlx.block_ts = get_block_ts(len, width, midc->dma->block_size);
  597. pr_debug("MDMA:calc len %d for block size %d\n",
  598. ctl_hi.ctlx.block_ts, midc->dma->block_size);
  599. /*calculate CTL_LO*/
  600. ctl_lo.ctl_lo = 0;
  601. ctl_lo.ctlx.int_en = 1;
  602. ctl_lo.ctlx.dst_tr_width = mids->dma_slave.dst_addr_width;
  603. ctl_lo.ctlx.src_tr_width = mids->dma_slave.src_addr_width;
  604. ctl_lo.ctlx.dst_msize = mids->dma_slave.src_maxburst;
  605. ctl_lo.ctlx.src_msize = mids->dma_slave.dst_maxburst;
  606. if (mids->cfg_mode == LNW_DMA_MEM_TO_MEM) {
  607. ctl_lo.ctlx.tt_fc = 0;
  608. ctl_lo.ctlx.sinc = 0;
  609. ctl_lo.ctlx.dinc = 0;
  610. } else {
  611. if (mids->dma_slave.direction == DMA_TO_DEVICE) {
  612. ctl_lo.ctlx.sinc = 0;
  613. ctl_lo.ctlx.dinc = 2;
  614. ctl_lo.ctlx.tt_fc = 1;
  615. } else if (mids->dma_slave.direction == DMA_FROM_DEVICE) {
  616. ctl_lo.ctlx.sinc = 2;
  617. ctl_lo.ctlx.dinc = 0;
  618. ctl_lo.ctlx.tt_fc = 2;
  619. }
  620. }
  621. pr_debug("MDMA:Calc CTL LO %x, CTL HI %x, CFG LO %x, CFG HI %x\n",
  622. ctl_lo.ctl_lo, ctl_hi.ctl_hi, cfg_lo.cfg_lo, cfg_hi.cfg_hi);
  623. enable_dma_interrupt(midc);
  624. desc = midc_desc_get(midc);
  625. if (desc == NULL)
  626. goto err_desc_get;
  627. desc->sar = src;
  628. desc->dar = dest ;
  629. desc->len = len;
  630. desc->cfg_hi = cfg_hi.cfg_hi;
  631. desc->cfg_lo = cfg_lo.cfg_lo;
  632. desc->ctl_lo = ctl_lo.ctl_lo;
  633. desc->ctl_hi = ctl_hi.ctl_hi;
  634. desc->width = width;
  635. desc->dirn = mids->dma_slave.direction;
  636. desc->lli_phys = 0;
  637. desc->lli = NULL;
  638. desc->lli_pool = NULL;
  639. return &desc->txd;
  640. err_desc_get:
  641. pr_err("ERR_MDMA: Failed to get desc\n");
  642. midc_desc_put(midc, desc);
  643. return NULL;
  644. }
  645. /**
  646. * intel_mid_dma_prep_slave_sg - Prep slave sg txn
  647. * @chan: chan for DMA transfer
  648. * @sgl: scatter gather list
  649. * @sg_len: length of sg txn
  650. * @direction: DMA transfer dirtn
  651. * @flags: DMA flags
  652. *
  653. * Prepares LLI based periphral transfer
  654. */
  655. static struct dma_async_tx_descriptor *intel_mid_dma_prep_slave_sg(
  656. struct dma_chan *chan, struct scatterlist *sgl,
  657. unsigned int sg_len, enum dma_data_direction direction,
  658. unsigned long flags)
  659. {
  660. struct intel_mid_dma_chan *midc = NULL;
  661. struct intel_mid_dma_slave *mids = NULL;
  662. struct intel_mid_dma_desc *desc = NULL;
  663. struct dma_async_tx_descriptor *txd = NULL;
  664. union intel_mid_dma_ctl_lo ctl_lo;
  665. pr_debug("MDMA: Prep for slave SG\n");
  666. if (!sg_len) {
  667. pr_err("MDMA: Invalid SG length\n");
  668. return NULL;
  669. }
  670. midc = to_intel_mid_dma_chan(chan);
  671. BUG_ON(!midc);
  672. mids = midc->mid_slave;
  673. BUG_ON(!mids);
  674. if (!midc->dma->pimr_mask) {
  675. pr_debug("MDMA: SG list is not supported by this controller\n");
  676. return NULL;
  677. }
  678. pr_debug("MDMA: SG Length = %d, direction = %d, Flags = %#lx\n",
  679. sg_len, direction, flags);
  680. txd = intel_mid_dma_prep_memcpy(chan, 0, 0, sgl->length, flags);
  681. if (NULL == txd) {
  682. pr_err("MDMA: Prep memcpy failed\n");
  683. return NULL;
  684. }
  685. desc = to_intel_mid_dma_desc(txd);
  686. desc->dirn = direction;
  687. ctl_lo.ctl_lo = desc->ctl_lo;
  688. ctl_lo.ctlx.llp_dst_en = 1;
  689. ctl_lo.ctlx.llp_src_en = 1;
  690. desc->ctl_lo = ctl_lo.ctl_lo;
  691. desc->lli_length = sg_len;
  692. desc->current_lli = 0;
  693. /* DMA coherent memory pool for LLI descriptors*/
  694. desc->lli_pool = pci_pool_create("intel_mid_dma_lli_pool",
  695. midc->dma->pdev,
  696. (sizeof(struct intel_mid_dma_lli)*sg_len),
  697. 32, 0);
  698. if (NULL == desc->lli_pool) {
  699. pr_err("MID_DMA:LLI pool create failed\n");
  700. return NULL;
  701. }
  702. desc->lli = pci_pool_alloc(desc->lli_pool, GFP_KERNEL, &desc->lli_phys);
  703. if (!desc->lli) {
  704. pr_err("MID_DMA: LLI alloc failed\n");
  705. pci_pool_destroy(desc->lli_pool);
  706. return NULL;
  707. }
  708. midc_lli_fill_sg(midc, desc, sgl, sg_len, flags);
  709. if (flags & DMA_PREP_INTERRUPT) {
  710. iowrite32(UNMASK_INTR_REG(midc->ch_id),
  711. midc->dma_base + MASK_BLOCK);
  712. pr_debug("MDMA:Enabled Block interrupt\n");
  713. }
  714. return &desc->txd;
  715. }
  716. /**
  717. * intel_mid_dma_free_chan_resources - Frees dma resources
  718. * @chan: chan requiring attention
  719. *
  720. * Frees the allocated resources on this DMA chan
  721. */
  722. static void intel_mid_dma_free_chan_resources(struct dma_chan *chan)
  723. {
  724. struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
  725. struct middma_device *mid = to_middma_device(chan->device);
  726. struct intel_mid_dma_desc *desc, *_desc;
  727. if (true == midc->busy) {
  728. /*trying to free ch in use!!!!!*/
  729. pr_err("ERR_MDMA: trying to free ch in use\n");
  730. }
  731. pm_runtime_put(&mid->pdev->dev);
  732. spin_lock_bh(&midc->lock);
  733. midc->descs_allocated = 0;
  734. list_for_each_entry_safe(desc, _desc, &midc->active_list, desc_node) {
  735. list_del(&desc->desc_node);
  736. pci_pool_free(mid->dma_pool, desc, desc->txd.phys);
  737. }
  738. list_for_each_entry_safe(desc, _desc, &midc->free_list, desc_node) {
  739. list_del(&desc->desc_node);
  740. pci_pool_free(mid->dma_pool, desc, desc->txd.phys);
  741. }
  742. list_for_each_entry_safe(desc, _desc, &midc->queue, desc_node) {
  743. list_del(&desc->desc_node);
  744. pci_pool_free(mid->dma_pool, desc, desc->txd.phys);
  745. }
  746. spin_unlock_bh(&midc->lock);
  747. midc->in_use = false;
  748. midc->busy = false;
  749. /* Disable CH interrupts */
  750. iowrite32(MASK_INTR_REG(midc->ch_id), mid->dma_base + MASK_BLOCK);
  751. iowrite32(MASK_INTR_REG(midc->ch_id), mid->dma_base + MASK_ERR);
  752. }
  753. /**
  754. * intel_mid_dma_alloc_chan_resources - Allocate dma resources
  755. * @chan: chan requiring attention
  756. *
  757. * Allocates DMA resources on this chan
  758. * Return the descriptors allocated
  759. */
  760. static int intel_mid_dma_alloc_chan_resources(struct dma_chan *chan)
  761. {
  762. struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
  763. struct middma_device *mid = to_middma_device(chan->device);
  764. struct intel_mid_dma_desc *desc;
  765. dma_addr_t phys;
  766. int i = 0;
  767. pm_runtime_get_sync(&mid->pdev->dev);
  768. if (mid->state == SUSPENDED) {
  769. if (dma_resume(mid->pdev)) {
  770. pr_err("ERR_MDMA: resume failed");
  771. return -EFAULT;
  772. }
  773. }
  774. /* ASSERT: channel is idle */
  775. if (test_ch_en(mid->dma_base, midc->ch_id)) {
  776. /*ch is not idle*/
  777. pr_err("ERR_MDMA: ch not idle\n");
  778. pm_runtime_put(&mid->pdev->dev);
  779. return -EIO;
  780. }
  781. midc->completed = chan->cookie = 1;
  782. spin_lock_bh(&midc->lock);
  783. while (midc->descs_allocated < DESCS_PER_CHANNEL) {
  784. spin_unlock_bh(&midc->lock);
  785. desc = pci_pool_alloc(mid->dma_pool, GFP_KERNEL, &phys);
  786. if (!desc) {
  787. pr_err("ERR_MDMA: desc failed\n");
  788. pm_runtime_put(&mid->pdev->dev);
  789. return -ENOMEM;
  790. /*check*/
  791. }
  792. dma_async_tx_descriptor_init(&desc->txd, chan);
  793. desc->txd.tx_submit = intel_mid_dma_tx_submit;
  794. desc->txd.flags = DMA_CTRL_ACK;
  795. desc->txd.phys = phys;
  796. spin_lock_bh(&midc->lock);
  797. i = ++midc->descs_allocated;
  798. list_add_tail(&desc->desc_node, &midc->free_list);
  799. }
  800. spin_unlock_bh(&midc->lock);
  801. midc->in_use = true;
  802. midc->busy = false;
  803. pr_debug("MID_DMA: Desc alloc done ret: %d desc\n", i);
  804. return i;
  805. }
  806. /**
  807. * midc_handle_error - Handle DMA txn error
  808. * @mid: controller where error occured
  809. * @midc: chan where error occured
  810. *
  811. * Scan the descriptor for error
  812. */
  813. static void midc_handle_error(struct middma_device *mid,
  814. struct intel_mid_dma_chan *midc)
  815. {
  816. midc_scan_descriptors(mid, midc);
  817. }
  818. /**
  819. * dma_tasklet - DMA interrupt tasklet
  820. * @data: tasklet arg (the controller structure)
  821. *
  822. * Scan the controller for interrupts for completion/error
  823. * Clear the interrupt and call for handling completion/error
  824. */
  825. static void dma_tasklet(unsigned long data)
  826. {
  827. struct middma_device *mid = NULL;
  828. struct intel_mid_dma_chan *midc = NULL;
  829. u32 status, raw_tfr, raw_block;
  830. int i;
  831. mid = (struct middma_device *)data;
  832. if (mid == NULL) {
  833. pr_err("ERR_MDMA: tasklet Null param\n");
  834. return;
  835. }
  836. pr_debug("MDMA: in tasklet for device %x\n", mid->pci_id);
  837. raw_tfr = ioread32(mid->dma_base + RAW_TFR);
  838. raw_block = ioread32(mid->dma_base + RAW_BLOCK);
  839. status = raw_tfr | raw_block;
  840. status &= mid->intr_mask;
  841. while (status) {
  842. /*txn interrupt*/
  843. i = get_ch_index(&status, mid->chan_base);
  844. if (i < 0) {
  845. pr_err("ERR_MDMA:Invalid ch index %x\n", i);
  846. return;
  847. }
  848. midc = &mid->ch[i];
  849. if (midc == NULL) {
  850. pr_err("ERR_MDMA:Null param midc\n");
  851. return;
  852. }
  853. pr_debug("MDMA:Tx complete interrupt %x, Ch No %d Index %d\n",
  854. status, midc->ch_id, i);
  855. midc->raw_tfr = raw_tfr;
  856. midc->raw_block = raw_block;
  857. spin_lock_bh(&midc->lock);
  858. /*clearing this interrupts first*/
  859. iowrite32((1 << midc->ch_id), mid->dma_base + CLEAR_TFR);
  860. if (raw_block) {
  861. iowrite32((1 << midc->ch_id),
  862. mid->dma_base + CLEAR_BLOCK);
  863. }
  864. midc_scan_descriptors(mid, midc);
  865. pr_debug("MDMA:Scan of desc... complete, unmasking\n");
  866. iowrite32(UNMASK_INTR_REG(midc->ch_id),
  867. mid->dma_base + MASK_TFR);
  868. if (raw_block) {
  869. iowrite32(UNMASK_INTR_REG(midc->ch_id),
  870. mid->dma_base + MASK_BLOCK);
  871. }
  872. spin_unlock_bh(&midc->lock);
  873. }
  874. status = ioread32(mid->dma_base + RAW_ERR);
  875. status &= mid->intr_mask;
  876. while (status) {
  877. /*err interrupt*/
  878. i = get_ch_index(&status, mid->chan_base);
  879. if (i < 0) {
  880. pr_err("ERR_MDMA:Invalid ch index %x\n", i);
  881. return;
  882. }
  883. midc = &mid->ch[i];
  884. if (midc == NULL) {
  885. pr_err("ERR_MDMA:Null param midc\n");
  886. return;
  887. }
  888. pr_debug("MDMA:Tx complete interrupt %x, Ch No %d Index %d\n",
  889. status, midc->ch_id, i);
  890. iowrite32((1 << midc->ch_id), mid->dma_base + CLEAR_ERR);
  891. spin_lock_bh(&midc->lock);
  892. midc_handle_error(mid, midc);
  893. iowrite32(UNMASK_INTR_REG(midc->ch_id),
  894. mid->dma_base + MASK_ERR);
  895. spin_unlock_bh(&midc->lock);
  896. }
  897. pr_debug("MDMA:Exiting takslet...\n");
  898. return;
  899. }
  900. static void dma_tasklet1(unsigned long data)
  901. {
  902. pr_debug("MDMA:in takslet1...\n");
  903. return dma_tasklet(data);
  904. }
  905. static void dma_tasklet2(unsigned long data)
  906. {
  907. pr_debug("MDMA:in takslet2...\n");
  908. return dma_tasklet(data);
  909. }
  910. /**
  911. * intel_mid_dma_interrupt - DMA ISR
  912. * @irq: IRQ where interrupt occurred
  913. * @data: ISR cllback data (the controller structure)
  914. *
  915. * See if this is our interrupt if so then schedule the tasklet
  916. * otherwise ignore
  917. */
  918. static irqreturn_t intel_mid_dma_interrupt(int irq, void *data)
  919. {
  920. struct middma_device *mid = data;
  921. u32 tfr_status, err_status;
  922. int call_tasklet = 0;
  923. tfr_status = ioread32(mid->dma_base + RAW_TFR);
  924. err_status = ioread32(mid->dma_base + RAW_ERR);
  925. if (!tfr_status && !err_status)
  926. return IRQ_NONE;
  927. /*DMA Interrupt*/
  928. pr_debug("MDMA:Got an interrupt on irq %d\n", irq);
  929. if (!mid) {
  930. pr_err("ERR_MDMA:null pointer mid\n");
  931. return -EINVAL;
  932. }
  933. pr_debug("MDMA: Status %x, Mask %x\n", tfr_status, mid->intr_mask);
  934. tfr_status &= mid->intr_mask;
  935. if (tfr_status) {
  936. /*need to disable intr*/
  937. iowrite32((tfr_status << INT_MASK_WE), mid->dma_base + MASK_TFR);
  938. iowrite32((tfr_status << INT_MASK_WE), mid->dma_base + MASK_BLOCK);
  939. pr_debug("MDMA: Calling tasklet %x\n", tfr_status);
  940. call_tasklet = 1;
  941. }
  942. err_status &= mid->intr_mask;
  943. if (err_status) {
  944. iowrite32(MASK_INTR_REG(err_status), mid->dma_base + MASK_ERR);
  945. call_tasklet = 1;
  946. }
  947. if (call_tasklet)
  948. tasklet_schedule(&mid->tasklet);
  949. return IRQ_HANDLED;
  950. }
  951. static irqreturn_t intel_mid_dma_interrupt1(int irq, void *data)
  952. {
  953. return intel_mid_dma_interrupt(irq, data);
  954. }
  955. static irqreturn_t intel_mid_dma_interrupt2(int irq, void *data)
  956. {
  957. return intel_mid_dma_interrupt(irq, data);
  958. }
  959. /**
  960. * mid_setup_dma - Setup the DMA controller
  961. * @pdev: Controller PCI device structure
  962. *
  963. * Initilize the DMA controller, channels, registers with DMA engine,
  964. * ISR. Initilize DMA controller channels.
  965. */
  966. static int mid_setup_dma(struct pci_dev *pdev)
  967. {
  968. struct middma_device *dma = pci_get_drvdata(pdev);
  969. int err, i;
  970. /* DMA coherent memory pool for DMA descriptor allocations */
  971. dma->dma_pool = pci_pool_create("intel_mid_dma_desc_pool", pdev,
  972. sizeof(struct intel_mid_dma_desc),
  973. 32, 0);
  974. if (NULL == dma->dma_pool) {
  975. pr_err("ERR_MDMA:pci_pool_create failed\n");
  976. err = -ENOMEM;
  977. goto err_dma_pool;
  978. }
  979. INIT_LIST_HEAD(&dma->common.channels);
  980. dma->pci_id = pdev->device;
  981. if (dma->pimr_mask) {
  982. dma->mask_reg = ioremap(LNW_PERIPHRAL_MASK_BASE,
  983. LNW_PERIPHRAL_MASK_SIZE);
  984. if (dma->mask_reg == NULL) {
  985. pr_err("ERR_MDMA:Cant map periphral intr space !!\n");
  986. return -ENOMEM;
  987. }
  988. } else
  989. dma->mask_reg = NULL;
  990. pr_debug("MDMA:Adding %d channel for this controller\n", dma->max_chan);
  991. /*init CH structures*/
  992. dma->intr_mask = 0;
  993. dma->state = RUNNING;
  994. for (i = 0; i < dma->max_chan; i++) {
  995. struct intel_mid_dma_chan *midch = &dma->ch[i];
  996. midch->chan.device = &dma->common;
  997. midch->chan.cookie = 1;
  998. midch->chan.chan_id = i;
  999. midch->ch_id = dma->chan_base + i;
  1000. pr_debug("MDMA:Init CH %d, ID %d\n", i, midch->ch_id);
  1001. midch->dma_base = dma->dma_base;
  1002. midch->ch_regs = dma->dma_base + DMA_CH_SIZE * midch->ch_id;
  1003. midch->dma = dma;
  1004. dma->intr_mask |= 1 << (dma->chan_base + i);
  1005. spin_lock_init(&midch->lock);
  1006. INIT_LIST_HEAD(&midch->active_list);
  1007. INIT_LIST_HEAD(&midch->queue);
  1008. INIT_LIST_HEAD(&midch->free_list);
  1009. /*mask interrupts*/
  1010. iowrite32(MASK_INTR_REG(midch->ch_id),
  1011. dma->dma_base + MASK_BLOCK);
  1012. iowrite32(MASK_INTR_REG(midch->ch_id),
  1013. dma->dma_base + MASK_SRC_TRAN);
  1014. iowrite32(MASK_INTR_REG(midch->ch_id),
  1015. dma->dma_base + MASK_DST_TRAN);
  1016. iowrite32(MASK_INTR_REG(midch->ch_id),
  1017. dma->dma_base + MASK_ERR);
  1018. iowrite32(MASK_INTR_REG(midch->ch_id),
  1019. dma->dma_base + MASK_TFR);
  1020. disable_dma_interrupt(midch);
  1021. list_add_tail(&midch->chan.device_node, &dma->common.channels);
  1022. }
  1023. pr_debug("MDMA: Calc Mask as %x for this controller\n", dma->intr_mask);
  1024. /*init dma structure*/
  1025. dma_cap_zero(dma->common.cap_mask);
  1026. dma_cap_set(DMA_MEMCPY, dma->common.cap_mask);
  1027. dma_cap_set(DMA_SLAVE, dma->common.cap_mask);
  1028. dma_cap_set(DMA_PRIVATE, dma->common.cap_mask);
  1029. dma->common.dev = &pdev->dev;
  1030. dma->common.chancnt = dma->max_chan;
  1031. dma->common.device_alloc_chan_resources =
  1032. intel_mid_dma_alloc_chan_resources;
  1033. dma->common.device_free_chan_resources =
  1034. intel_mid_dma_free_chan_resources;
  1035. dma->common.device_tx_status = intel_mid_dma_tx_status;
  1036. dma->common.device_prep_dma_memcpy = intel_mid_dma_prep_memcpy;
  1037. dma->common.device_issue_pending = intel_mid_dma_issue_pending;
  1038. dma->common.device_prep_slave_sg = intel_mid_dma_prep_slave_sg;
  1039. dma->common.device_control = intel_mid_dma_device_control;
  1040. /*enable dma cntrl*/
  1041. iowrite32(REG_BIT0, dma->dma_base + DMA_CFG);
  1042. /*register irq */
  1043. if (dma->pimr_mask) {
  1044. pr_debug("MDMA:Requesting irq shared for DMAC1\n");
  1045. err = request_irq(pdev->irq, intel_mid_dma_interrupt1,
  1046. IRQF_SHARED, "INTEL_MID_DMAC1", dma);
  1047. if (0 != err)
  1048. goto err_irq;
  1049. } else {
  1050. dma->intr_mask = 0x03;
  1051. pr_debug("MDMA:Requesting irq for DMAC2\n");
  1052. err = request_irq(pdev->irq, intel_mid_dma_interrupt2,
  1053. IRQF_SHARED, "INTEL_MID_DMAC2", dma);
  1054. if (0 != err)
  1055. goto err_irq;
  1056. }
  1057. /*register device w/ engine*/
  1058. err = dma_async_device_register(&dma->common);
  1059. if (0 != err) {
  1060. pr_err("ERR_MDMA:device_register failed: %d\n", err);
  1061. goto err_engine;
  1062. }
  1063. if (dma->pimr_mask) {
  1064. pr_debug("setting up tasklet1 for DMAC1\n");
  1065. tasklet_init(&dma->tasklet, dma_tasklet1, (unsigned long)dma);
  1066. } else {
  1067. pr_debug("setting up tasklet2 for DMAC2\n");
  1068. tasklet_init(&dma->tasklet, dma_tasklet2, (unsigned long)dma);
  1069. }
  1070. return 0;
  1071. err_engine:
  1072. free_irq(pdev->irq, dma);
  1073. err_irq:
  1074. pci_pool_destroy(dma->dma_pool);
  1075. err_dma_pool:
  1076. pr_err("ERR_MDMA:setup_dma failed: %d\n", err);
  1077. return err;
  1078. }
  1079. /**
  1080. * middma_shutdown - Shutdown the DMA controller
  1081. * @pdev: Controller PCI device structure
  1082. *
  1083. * Called by remove
  1084. * Unregister DMa controller, clear all structures and free interrupt
  1085. */
  1086. static void middma_shutdown(struct pci_dev *pdev)
  1087. {
  1088. struct middma_device *device = pci_get_drvdata(pdev);
  1089. dma_async_device_unregister(&device->common);
  1090. pci_pool_destroy(device->dma_pool);
  1091. if (device->mask_reg)
  1092. iounmap(device->mask_reg);
  1093. if (device->dma_base)
  1094. iounmap(device->dma_base);
  1095. free_irq(pdev->irq, device);
  1096. return;
  1097. }
  1098. /**
  1099. * intel_mid_dma_probe - PCI Probe
  1100. * @pdev: Controller PCI device structure
  1101. * @id: pci device id structure
  1102. *
  1103. * Initilize the PCI device, map BARs, query driver data.
  1104. * Call setup_dma to complete contoller and chan initilzation
  1105. */
  1106. static int __devinit intel_mid_dma_probe(struct pci_dev *pdev,
  1107. const struct pci_device_id *id)
  1108. {
  1109. struct middma_device *device;
  1110. u32 base_addr, bar_size;
  1111. struct intel_mid_dma_probe_info *info;
  1112. int err;
  1113. pr_debug("MDMA: probe for %x\n", pdev->device);
  1114. info = (void *)id->driver_data;
  1115. pr_debug("MDMA: CH %d, base %d, block len %d, Periphral mask %x\n",
  1116. info->max_chan, info->ch_base,
  1117. info->block_size, info->pimr_mask);
  1118. err = pci_enable_device(pdev);
  1119. if (err)
  1120. goto err_enable_device;
  1121. err = pci_request_regions(pdev, "intel_mid_dmac");
  1122. if (err)
  1123. goto err_request_regions;
  1124. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1125. if (err)
  1126. goto err_set_dma_mask;
  1127. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1128. if (err)
  1129. goto err_set_dma_mask;
  1130. device = kzalloc(sizeof(*device), GFP_KERNEL);
  1131. if (!device) {
  1132. pr_err("ERR_MDMA:kzalloc failed probe\n");
  1133. err = -ENOMEM;
  1134. goto err_kzalloc;
  1135. }
  1136. device->pdev = pci_dev_get(pdev);
  1137. base_addr = pci_resource_start(pdev, 0);
  1138. bar_size = pci_resource_len(pdev, 0);
  1139. device->dma_base = ioremap_nocache(base_addr, DMA_REG_SIZE);
  1140. if (!device->dma_base) {
  1141. pr_err("ERR_MDMA:ioremap failed\n");
  1142. err = -ENOMEM;
  1143. goto err_ioremap;
  1144. }
  1145. pci_set_drvdata(pdev, device);
  1146. pci_set_master(pdev);
  1147. device->max_chan = info->max_chan;
  1148. device->chan_base = info->ch_base;
  1149. device->block_size = info->block_size;
  1150. device->pimr_mask = info->pimr_mask;
  1151. err = mid_setup_dma(pdev);
  1152. if (err)
  1153. goto err_dma;
  1154. pm_runtime_set_active(&pdev->dev);
  1155. pm_runtime_enable(&pdev->dev);
  1156. pm_runtime_allow(&pdev->dev);
  1157. return 0;
  1158. err_dma:
  1159. iounmap(device->dma_base);
  1160. err_ioremap:
  1161. pci_dev_put(pdev);
  1162. kfree(device);
  1163. err_kzalloc:
  1164. err_set_dma_mask:
  1165. pci_release_regions(pdev);
  1166. pci_disable_device(pdev);
  1167. err_request_regions:
  1168. err_enable_device:
  1169. pr_err("ERR_MDMA:Probe failed %d\n", err);
  1170. return err;
  1171. }
  1172. /**
  1173. * intel_mid_dma_remove - PCI remove
  1174. * @pdev: Controller PCI device structure
  1175. *
  1176. * Free up all resources and data
  1177. * Call shutdown_dma to complete contoller and chan cleanup
  1178. */
  1179. static void __devexit intel_mid_dma_remove(struct pci_dev *pdev)
  1180. {
  1181. struct middma_device *device = pci_get_drvdata(pdev);
  1182. middma_shutdown(pdev);
  1183. pci_dev_put(pdev);
  1184. kfree(device);
  1185. pci_release_regions(pdev);
  1186. pci_disable_device(pdev);
  1187. }
  1188. /* Power Management */
  1189. /*
  1190. * dma_suspend - PCI suspend function
  1191. *
  1192. * @pci: PCI device structure
  1193. * @state: PM message
  1194. *
  1195. * This function is called by OS when a power event occurs
  1196. */
  1197. int dma_suspend(struct pci_dev *pci, pm_message_t state)
  1198. {
  1199. int i;
  1200. struct middma_device *device = pci_get_drvdata(pci);
  1201. pr_debug("MDMA: dma_suspend called\n");
  1202. for (i = 0; i < device->max_chan; i++) {
  1203. if (device->ch[i].in_use)
  1204. return -EAGAIN;
  1205. }
  1206. device->state = SUSPENDED;
  1207. pci_set_drvdata(pci, device);
  1208. pci_save_state(pci);
  1209. pci_disable_device(pci);
  1210. pci_set_power_state(pci, PCI_D3hot);
  1211. return 0;
  1212. }
  1213. /**
  1214. * dma_resume - PCI resume function
  1215. *
  1216. * @pci: PCI device structure
  1217. *
  1218. * This function is called by OS when a power event occurs
  1219. */
  1220. int dma_resume(struct pci_dev *pci)
  1221. {
  1222. int ret;
  1223. struct middma_device *device = pci_get_drvdata(pci);
  1224. pr_debug("MDMA: dma_resume called\n");
  1225. pci_set_power_state(pci, PCI_D0);
  1226. pci_restore_state(pci);
  1227. ret = pci_enable_device(pci);
  1228. if (ret) {
  1229. pr_err("MDMA: device cant be enabled for %x\n", pci->device);
  1230. return ret;
  1231. }
  1232. device->state = RUNNING;
  1233. iowrite32(REG_BIT0, device->dma_base + DMA_CFG);
  1234. pci_set_drvdata(pci, device);
  1235. return 0;
  1236. }
  1237. static int dma_runtime_suspend(struct device *dev)
  1238. {
  1239. struct pci_dev *pci_dev = to_pci_dev(dev);
  1240. return dma_suspend(pci_dev, PMSG_SUSPEND);
  1241. }
  1242. static int dma_runtime_resume(struct device *dev)
  1243. {
  1244. struct pci_dev *pci_dev = to_pci_dev(dev);
  1245. return dma_resume(pci_dev);
  1246. }
  1247. static int dma_runtime_idle(struct device *dev)
  1248. {
  1249. struct pci_dev *pdev = to_pci_dev(dev);
  1250. struct middma_device *device = pci_get_drvdata(pdev);
  1251. int i;
  1252. for (i = 0; i < device->max_chan; i++) {
  1253. if (device->ch[i].in_use)
  1254. return -EAGAIN;
  1255. }
  1256. return pm_schedule_suspend(dev, 0);
  1257. }
  1258. /******************************************************************************
  1259. * PCI stuff
  1260. */
  1261. static struct pci_device_id intel_mid_dma_ids[] = {
  1262. { PCI_VDEVICE(INTEL, INTEL_MID_DMAC1_ID), INFO(2, 6, 4095, 0x200020)},
  1263. { PCI_VDEVICE(INTEL, INTEL_MID_DMAC2_ID), INFO(2, 0, 2047, 0)},
  1264. { PCI_VDEVICE(INTEL, INTEL_MID_GP_DMAC2_ID), INFO(2, 0, 2047, 0)},
  1265. { PCI_VDEVICE(INTEL, INTEL_MFLD_DMAC1_ID), INFO(4, 0, 4095, 0x400040)},
  1266. { 0, }
  1267. };
  1268. MODULE_DEVICE_TABLE(pci, intel_mid_dma_ids);
  1269. static const struct dev_pm_ops intel_mid_dma_pm = {
  1270. .runtime_suspend = dma_runtime_suspend,
  1271. .runtime_resume = dma_runtime_resume,
  1272. .runtime_idle = dma_runtime_idle,
  1273. };
  1274. static struct pci_driver intel_mid_dma_pci_driver = {
  1275. .name = "Intel MID DMA",
  1276. .id_table = intel_mid_dma_ids,
  1277. .probe = intel_mid_dma_probe,
  1278. .remove = __devexit_p(intel_mid_dma_remove),
  1279. #ifdef CONFIG_PM
  1280. .suspend = dma_suspend,
  1281. .resume = dma_resume,
  1282. .driver = {
  1283. .pm = &intel_mid_dma_pm,
  1284. },
  1285. #endif
  1286. };
  1287. static int __init intel_mid_dma_init(void)
  1288. {
  1289. pr_debug("INFO_MDMA: LNW DMA Driver Version %s\n",
  1290. INTEL_MID_DMA_DRIVER_VERSION);
  1291. return pci_register_driver(&intel_mid_dma_pci_driver);
  1292. }
  1293. fs_initcall(intel_mid_dma_init);
  1294. static void __exit intel_mid_dma_exit(void)
  1295. {
  1296. pci_unregister_driver(&intel_mid_dma_pci_driver);
  1297. }
  1298. module_exit(intel_mid_dma_exit);
  1299. MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
  1300. MODULE_DESCRIPTION("Intel (R) MID DMAC Driver");
  1301. MODULE_LICENSE("GPL v2");
  1302. MODULE_VERSION(INTEL_MID_DMA_DRIVER_VERSION);