intel_mid_dma.c 40 KB

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