intel_mid_dma.c 40 KB

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