intel_mid_dma.c 40 KB

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