intel_mid_dma.c 40 KB

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