intel_mid_dma.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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/intel_mid_dma.h>
  29. #define MAX_CHAN 4 /*max ch across controllers*/
  30. #include "intel_mid_dma_regs.h"
  31. #define INTEL_MID_DMAC1_ID 0x0814
  32. #define INTEL_MID_DMAC2_ID 0x0813
  33. #define INTEL_MID_GP_DMAC2_ID 0x0827
  34. #define INTEL_MFLD_DMAC1_ID 0x0830
  35. #define LNW_PERIPHRAL_MASK_BASE 0xFFAE8008
  36. #define LNW_PERIPHRAL_MASK_SIZE 0x10
  37. #define LNW_PERIPHRAL_STATUS 0x0
  38. #define LNW_PERIPHRAL_MASK 0x8
  39. struct intel_mid_dma_probe_info {
  40. u8 max_chan;
  41. u8 ch_base;
  42. u16 block_size;
  43. u32 pimr_mask;
  44. };
  45. #define INFO(_max_chan, _ch_base, _block_size, _pimr_mask) \
  46. ((kernel_ulong_t)&(struct intel_mid_dma_probe_info) { \
  47. .max_chan = (_max_chan), \
  48. .ch_base = (_ch_base), \
  49. .block_size = (_block_size), \
  50. .pimr_mask = (_pimr_mask), \
  51. })
  52. /*****************************************************************************
  53. Utility Functions*/
  54. /**
  55. * get_ch_index - convert status to channel
  56. * @status: status mask
  57. * @base: dma ch base value
  58. *
  59. * Modify the status mask and return the channel index needing
  60. * attention (or -1 if neither)
  61. */
  62. static int get_ch_index(int *status, unsigned int base)
  63. {
  64. int i;
  65. for (i = 0; i < MAX_CHAN; i++) {
  66. if (*status & (1 << (i + base))) {
  67. *status = *status & ~(1 << (i + base));
  68. pr_debug("MDMA: index %d New status %x\n", i, *status);
  69. return i;
  70. }
  71. }
  72. return -1;
  73. }
  74. /**
  75. * get_block_ts - calculates dma transaction length
  76. * @len: dma transfer length
  77. * @tx_width: dma transfer src width
  78. * @block_size: dma controller max block size
  79. *
  80. * Based on src width calculate the DMA trsaction length in data items
  81. * return data items or FFFF if exceeds max length for block
  82. */
  83. static int get_block_ts(int len, int tx_width, int block_size)
  84. {
  85. int byte_width = 0, block_ts = 0;
  86. switch (tx_width) {
  87. case LNW_DMA_WIDTH_8BIT:
  88. byte_width = 1;
  89. break;
  90. case LNW_DMA_WIDTH_16BIT:
  91. byte_width = 2;
  92. break;
  93. case LNW_DMA_WIDTH_32BIT:
  94. default:
  95. byte_width = 4;
  96. break;
  97. }
  98. block_ts = len/byte_width;
  99. if (block_ts > block_size)
  100. block_ts = 0xFFFF;
  101. return block_ts;
  102. }
  103. /*****************************************************************************
  104. DMAC1 interrupt Functions*/
  105. /**
  106. * dmac1_mask_periphral_intr - mask the periphral interrupt
  107. * @midc: dma channel for which masking is required
  108. *
  109. * Masks the DMA periphral interrupt
  110. * this is valid for DMAC1 family controllers only
  111. * This controller should have periphral mask registers already mapped
  112. */
  113. static void dmac1_mask_periphral_intr(struct intel_mid_dma_chan *midc)
  114. {
  115. u32 pimr;
  116. struct middma_device *mid = to_middma_device(midc->chan.device);
  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. dmac1_mask_periphral_intr(midc);
  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->in_use && 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. /*write registers and en*/
  237. iowrite32(first->sar, midc->ch_regs + SAR);
  238. iowrite32(first->dar, midc->ch_regs + DAR);
  239. iowrite32(first->cfg_hi, midc->ch_regs + CFG_HIGH);
  240. iowrite32(first->cfg_lo, midc->ch_regs + CFG_LOW);
  241. iowrite32(first->ctl_lo, midc->ch_regs + CTL_LOW);
  242. iowrite32(first->ctl_hi, midc->ch_regs + CTL_HIGH);
  243. pr_debug("MDMA:TX SAR %x,DAR %x,CFGL %x,CFGH %x,CTLH %x, CTLL %x\n",
  244. (int)first->sar, (int)first->dar, first->cfg_hi,
  245. first->cfg_lo, first->ctl_hi, first->ctl_lo);
  246. iowrite32(ENABLE_CHANNEL(midc->ch_id), mid->dma_base + DMA_CHAN_EN);
  247. first->status = DMA_IN_PROGRESS;
  248. }
  249. /**
  250. * midc_descriptor_complete - process completed descriptor
  251. * @midc: channel owning the descriptor
  252. * @desc: the descriptor itself
  253. *
  254. * Process a completed descriptor and perform any callbacks upon
  255. * the completion. The completion handling drops the lock during the
  256. * callbacks but must be called with the lock held.
  257. */
  258. static void midc_descriptor_complete(struct intel_mid_dma_chan *midc,
  259. struct intel_mid_dma_desc *desc)
  260. {
  261. struct dma_async_tx_descriptor *txd = &desc->txd;
  262. dma_async_tx_callback callback_txd = NULL;
  263. void *param_txd = NULL;
  264. midc->completed = txd->cookie;
  265. callback_txd = txd->callback;
  266. param_txd = txd->callback_param;
  267. list_move(&desc->desc_node, &midc->free_list);
  268. spin_unlock_bh(&midc->lock);
  269. if (callback_txd) {
  270. pr_debug("MDMA: TXD callback set ... calling\n");
  271. callback_txd(param_txd);
  272. spin_lock_bh(&midc->lock);
  273. return;
  274. }
  275. spin_lock_bh(&midc->lock);
  276. }
  277. /**
  278. * midc_scan_descriptors - check the descriptors in channel
  279. * mark completed when tx is completete
  280. * @mid: device
  281. * @midc: channel to scan
  282. *
  283. * Walk the descriptor chain for the device and process any entries
  284. * that are complete.
  285. */
  286. static void midc_scan_descriptors(struct middma_device *mid,
  287. struct intel_mid_dma_chan *midc)
  288. {
  289. struct intel_mid_dma_desc *desc = NULL, *_desc = NULL;
  290. /*tx is complete*/
  291. list_for_each_entry_safe(desc, _desc, &midc->active_list, desc_node) {
  292. if (desc->status == DMA_IN_PROGRESS) {
  293. desc->status = DMA_SUCCESS;
  294. midc_descriptor_complete(midc, desc);
  295. }
  296. }
  297. return;
  298. }
  299. /*****************************************************************************
  300. DMA engine callback Functions*/
  301. /**
  302. * intel_mid_dma_tx_submit - callback to submit DMA transaction
  303. * @tx: dma engine descriptor
  304. *
  305. * Submit the DMA trasaction for this descriptor, start if ch idle
  306. */
  307. static dma_cookie_t intel_mid_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  308. {
  309. struct intel_mid_dma_desc *desc = to_intel_mid_dma_desc(tx);
  310. struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(tx->chan);
  311. dma_cookie_t cookie;
  312. spin_lock_bh(&midc->lock);
  313. cookie = midc->chan.cookie;
  314. if (++cookie < 0)
  315. cookie = 1;
  316. midc->chan.cookie = cookie;
  317. desc->txd.cookie = cookie;
  318. if (list_empty(&midc->active_list)) {
  319. midc_dostart(midc, desc);
  320. list_add_tail(&desc->desc_node, &midc->active_list);
  321. } else {
  322. list_add_tail(&desc->desc_node, &midc->queue);
  323. }
  324. spin_unlock_bh(&midc->lock);
  325. return cookie;
  326. }
  327. /**
  328. * intel_mid_dma_issue_pending - callback to issue pending txn
  329. * @chan: chan where pending trascation needs to be checked and submitted
  330. *
  331. * Call for scan to issue pending descriptors
  332. */
  333. static void intel_mid_dma_issue_pending(struct dma_chan *chan)
  334. {
  335. struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
  336. spin_lock_bh(&midc->lock);
  337. if (!list_empty(&midc->queue))
  338. midc_scan_descriptors(to_middma_device(chan->device), midc);
  339. spin_unlock_bh(&midc->lock);
  340. }
  341. /**
  342. * intel_mid_dma_tx_status - Return status of txn
  343. * @chan: chan for where status needs to be checked
  344. * @cookie: cookie for txn
  345. * @txstate: DMA txn state
  346. *
  347. * Return status of DMA txn
  348. */
  349. static enum dma_status intel_mid_dma_tx_status(struct dma_chan *chan,
  350. dma_cookie_t cookie,
  351. struct dma_tx_state *txstate)
  352. {
  353. struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
  354. dma_cookie_t last_used;
  355. dma_cookie_t last_complete;
  356. int ret;
  357. last_complete = midc->completed;
  358. last_used = chan->cookie;
  359. ret = dma_async_is_complete(cookie, last_complete, last_used);
  360. if (ret != DMA_SUCCESS) {
  361. midc_scan_descriptors(to_middma_device(chan->device), midc);
  362. last_complete = midc->completed;
  363. last_used = chan->cookie;
  364. ret = dma_async_is_complete(cookie, last_complete, last_used);
  365. }
  366. if (txstate) {
  367. txstate->last = last_complete;
  368. txstate->used = last_used;
  369. txstate->residue = 0;
  370. }
  371. return ret;
  372. }
  373. /**
  374. * intel_mid_dma_device_control - DMA device control
  375. * @chan: chan for DMA control
  376. * @cmd: control cmd
  377. * @arg: cmd arg value
  378. *
  379. * Perform DMA control command
  380. */
  381. static int intel_mid_dma_device_control(struct dma_chan *chan,
  382. enum dma_ctrl_cmd cmd, unsigned long arg)
  383. {
  384. struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
  385. struct middma_device *mid = to_middma_device(chan->device);
  386. struct intel_mid_dma_desc *desc, *_desc;
  387. LIST_HEAD(list);
  388. if (cmd != DMA_TERMINATE_ALL)
  389. return -ENXIO;
  390. spin_lock_bh(&midc->lock);
  391. if (midc->in_use == false) {
  392. spin_unlock_bh(&midc->lock);
  393. return 0;
  394. }
  395. list_splice_init(&midc->free_list, &list);
  396. midc->descs_allocated = 0;
  397. midc->slave = NULL;
  398. /* Disable interrupts */
  399. disable_dma_interrupt(midc);
  400. spin_unlock_bh(&midc->lock);
  401. list_for_each_entry_safe(desc, _desc, &list, desc_node) {
  402. pr_debug("MDMA: freeing descriptor %p\n", desc);
  403. pci_pool_free(mid->dma_pool, desc, desc->txd.phys);
  404. }
  405. return 0;
  406. }
  407. /**
  408. * intel_mid_dma_prep_slave_sg - Prep slave sg txn
  409. * @chan: chan for DMA transfer
  410. * @sgl: scatter gather list
  411. * @sg_len: length of sg txn
  412. * @direction: DMA transfer dirtn
  413. * @flags: DMA flags
  414. *
  415. * Do DMA sg txn: NOT supported now
  416. */
  417. static struct dma_async_tx_descriptor *intel_mid_dma_prep_slave_sg(
  418. struct dma_chan *chan, struct scatterlist *sgl,
  419. unsigned int sg_len, enum dma_data_direction direction,
  420. unsigned long flags)
  421. {
  422. /*not supported now*/
  423. return NULL;
  424. }
  425. /**
  426. * intel_mid_dma_prep_memcpy - Prep memcpy txn
  427. * @chan: chan for DMA transfer
  428. * @dest: destn address
  429. * @src: src address
  430. * @len: DMA transfer len
  431. * @flags: DMA flags
  432. *
  433. * Perform a DMA memcpy. Note we support slave periphral DMA transfers only
  434. * The periphral txn details should be filled in slave structure properly
  435. * Returns the descriptor for this txn
  436. */
  437. static struct dma_async_tx_descriptor *intel_mid_dma_prep_memcpy(
  438. struct dma_chan *chan, dma_addr_t dest,
  439. dma_addr_t src, size_t len, unsigned long flags)
  440. {
  441. struct intel_mid_dma_chan *midc;
  442. struct intel_mid_dma_desc *desc = NULL;
  443. struct intel_mid_dma_slave *mids;
  444. union intel_mid_dma_ctl_lo ctl_lo;
  445. union intel_mid_dma_ctl_hi ctl_hi;
  446. union intel_mid_dma_cfg_lo cfg_lo;
  447. union intel_mid_dma_cfg_hi cfg_hi;
  448. enum intel_mid_dma_width width = 0;
  449. pr_debug("MDMA: Prep for memcpy\n");
  450. WARN_ON(!chan);
  451. if (!len)
  452. return NULL;
  453. mids = chan->private;
  454. WARN_ON(!mids);
  455. midc = to_intel_mid_dma_chan(chan);
  456. WARN_ON(!midc);
  457. pr_debug("MDMA:called for DMA %x CH %d Length %zu\n",
  458. midc->dma->pci_id, midc->ch_id, len);
  459. pr_debug("MDMA:Cfg passed Mode %x, Dirn %x, HS %x, Width %x\n",
  460. mids->cfg_mode, mids->dirn, mids->hs_mode, mids->src_width);
  461. /*calculate CFG_LO*/
  462. if (mids->hs_mode == LNW_DMA_SW_HS) {
  463. cfg_lo.cfg_lo = 0;
  464. cfg_lo.cfgx.hs_sel_dst = 1;
  465. cfg_lo.cfgx.hs_sel_src = 1;
  466. } else if (mids->hs_mode == LNW_DMA_HW_HS)
  467. cfg_lo.cfg_lo = 0x00000;
  468. /*calculate CFG_HI*/
  469. if (mids->cfg_mode == LNW_DMA_MEM_TO_MEM) {
  470. /*SW HS only*/
  471. cfg_hi.cfg_hi = 0;
  472. } else {
  473. cfg_hi.cfg_hi = 0;
  474. if (midc->dma->pimr_mask) {
  475. cfg_hi.cfgx.protctl = 0x0; /*default value*/
  476. cfg_hi.cfgx.fifo_mode = 1;
  477. if (mids->dirn == DMA_TO_DEVICE) {
  478. cfg_hi.cfgx.src_per = 0;
  479. if (mids->device_instance == 0)
  480. cfg_hi.cfgx.dst_per = 3;
  481. if (mids->device_instance == 1)
  482. cfg_hi.cfgx.dst_per = 1;
  483. } else if (mids->dirn == DMA_FROM_DEVICE) {
  484. if (mids->device_instance == 0)
  485. cfg_hi.cfgx.src_per = 2;
  486. if (mids->device_instance == 1)
  487. cfg_hi.cfgx.src_per = 0;
  488. cfg_hi.cfgx.dst_per = 0;
  489. }
  490. } else {
  491. cfg_hi.cfgx.protctl = 0x1; /*default value*/
  492. cfg_hi.cfgx.src_per = cfg_hi.cfgx.dst_per =
  493. midc->ch_id - midc->dma->chan_base;
  494. }
  495. }
  496. /*calculate CTL_HI*/
  497. ctl_hi.ctlx.reser = 0;
  498. width = mids->src_width;
  499. ctl_hi.ctlx.block_ts = get_block_ts(len, width, midc->dma->block_size);
  500. pr_debug("MDMA:calc len %d for block size %d\n",
  501. ctl_hi.ctlx.block_ts, midc->dma->block_size);
  502. /*calculate CTL_LO*/
  503. ctl_lo.ctl_lo = 0;
  504. ctl_lo.ctlx.int_en = 1;
  505. ctl_lo.ctlx.dst_tr_width = mids->dst_width;
  506. ctl_lo.ctlx.src_tr_width = mids->src_width;
  507. ctl_lo.ctlx.dst_msize = mids->src_msize;
  508. ctl_lo.ctlx.src_msize = mids->dst_msize;
  509. if (mids->cfg_mode == LNW_DMA_MEM_TO_MEM) {
  510. ctl_lo.ctlx.tt_fc = 0;
  511. ctl_lo.ctlx.sinc = 0;
  512. ctl_lo.ctlx.dinc = 0;
  513. } else {
  514. if (mids->dirn == DMA_TO_DEVICE) {
  515. ctl_lo.ctlx.sinc = 0;
  516. ctl_lo.ctlx.dinc = 2;
  517. ctl_lo.ctlx.tt_fc = 1;
  518. } else if (mids->dirn == DMA_FROM_DEVICE) {
  519. ctl_lo.ctlx.sinc = 2;
  520. ctl_lo.ctlx.dinc = 0;
  521. ctl_lo.ctlx.tt_fc = 2;
  522. }
  523. }
  524. pr_debug("MDMA:Calc CTL LO %x, CTL HI %x, CFG LO %x, CFG HI %x\n",
  525. ctl_lo.ctl_lo, ctl_hi.ctl_hi, cfg_lo.cfg_lo, cfg_hi.cfg_hi);
  526. enable_dma_interrupt(midc);
  527. desc = midc_desc_get(midc);
  528. if (desc == NULL)
  529. goto err_desc_get;
  530. desc->sar = src;
  531. desc->dar = dest ;
  532. desc->len = len;
  533. desc->cfg_hi = cfg_hi.cfg_hi;
  534. desc->cfg_lo = cfg_lo.cfg_lo;
  535. desc->ctl_lo = ctl_lo.ctl_lo;
  536. desc->ctl_hi = ctl_hi.ctl_hi;
  537. desc->width = width;
  538. desc->dirn = mids->dirn;
  539. return &desc->txd;
  540. err_desc_get:
  541. pr_err("ERR_MDMA: Failed to get desc\n");
  542. midc_desc_put(midc, desc);
  543. return NULL;
  544. }
  545. /**
  546. * intel_mid_dma_free_chan_resources - Frees dma resources
  547. * @chan: chan requiring attention
  548. *
  549. * Frees the allocated resources on this DMA chan
  550. */
  551. static void intel_mid_dma_free_chan_resources(struct dma_chan *chan)
  552. {
  553. struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
  554. struct middma_device *mid = to_middma_device(chan->device);
  555. struct intel_mid_dma_desc *desc, *_desc;
  556. if (true == midc->in_use) {
  557. /*trying to free ch in use!!!!!*/
  558. pr_err("ERR_MDMA: trying to free ch in use\n");
  559. }
  560. spin_lock_bh(&midc->lock);
  561. midc->descs_allocated = 0;
  562. list_for_each_entry_safe(desc, _desc, &midc->active_list, desc_node) {
  563. list_del(&desc->desc_node);
  564. pci_pool_free(mid->dma_pool, desc, desc->txd.phys);
  565. }
  566. list_for_each_entry_safe(desc, _desc, &midc->free_list, desc_node) {
  567. list_del(&desc->desc_node);
  568. pci_pool_free(mid->dma_pool, desc, desc->txd.phys);
  569. }
  570. list_for_each_entry_safe(desc, _desc, &midc->queue, desc_node) {
  571. list_del(&desc->desc_node);
  572. pci_pool_free(mid->dma_pool, desc, desc->txd.phys);
  573. }
  574. spin_unlock_bh(&midc->lock);
  575. midc->in_use = false;
  576. /* Disable CH interrupts */
  577. iowrite32(MASK_INTR_REG(midc->ch_id), mid->dma_base + MASK_BLOCK);
  578. iowrite32(MASK_INTR_REG(midc->ch_id), mid->dma_base + MASK_ERR);
  579. }
  580. /**
  581. * intel_mid_dma_alloc_chan_resources - Allocate dma resources
  582. * @chan: chan requiring attention
  583. *
  584. * Allocates DMA resources on this chan
  585. * Return the descriptors allocated
  586. */
  587. static int intel_mid_dma_alloc_chan_resources(struct dma_chan *chan)
  588. {
  589. struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
  590. struct middma_device *mid = to_middma_device(chan->device);
  591. struct intel_mid_dma_desc *desc;
  592. dma_addr_t phys;
  593. int i = 0;
  594. /* ASSERT: channel is idle */
  595. if (test_ch_en(mid->dma_base, midc->ch_id)) {
  596. /*ch is not idle*/
  597. pr_err("ERR_MDMA: ch not idle\n");
  598. return -EIO;
  599. }
  600. midc->completed = chan->cookie = 1;
  601. spin_lock_bh(&midc->lock);
  602. while (midc->descs_allocated < DESCS_PER_CHANNEL) {
  603. spin_unlock_bh(&midc->lock);
  604. desc = pci_pool_alloc(mid->dma_pool, GFP_KERNEL, &phys);
  605. if (!desc) {
  606. pr_err("ERR_MDMA: desc failed\n");
  607. return -ENOMEM;
  608. /*check*/
  609. }
  610. dma_async_tx_descriptor_init(&desc->txd, chan);
  611. desc->txd.tx_submit = intel_mid_dma_tx_submit;
  612. desc->txd.flags = DMA_CTRL_ACK;
  613. desc->txd.phys = phys;
  614. spin_lock_bh(&midc->lock);
  615. i = ++midc->descs_allocated;
  616. list_add_tail(&desc->desc_node, &midc->free_list);
  617. }
  618. spin_unlock_bh(&midc->lock);
  619. midc->in_use = false;
  620. pr_debug("MID_DMA: Desc alloc done ret: %d desc\n", i);
  621. return i;
  622. }
  623. /**
  624. * midc_handle_error - Handle DMA txn error
  625. * @mid: controller where error occured
  626. * @midc: chan where error occured
  627. *
  628. * Scan the descriptor for error
  629. */
  630. static void midc_handle_error(struct middma_device *mid,
  631. struct intel_mid_dma_chan *midc)
  632. {
  633. midc_scan_descriptors(mid, midc);
  634. }
  635. /**
  636. * dma_tasklet - DMA interrupt tasklet
  637. * @data: tasklet arg (the controller structure)
  638. *
  639. * Scan the controller for interrupts for completion/error
  640. * Clear the interrupt and call for handling completion/error
  641. */
  642. static void dma_tasklet(unsigned long data)
  643. {
  644. struct middma_device *mid = NULL;
  645. struct intel_mid_dma_chan *midc = NULL;
  646. u32 status;
  647. int i;
  648. mid = (struct middma_device *)data;
  649. if (mid == NULL) {
  650. pr_err("ERR_MDMA: tasklet Null param\n");
  651. return;
  652. }
  653. pr_debug("MDMA: in tasklet for device %x\n", mid->pci_id);
  654. status = ioread32(mid->dma_base + RAW_TFR);
  655. pr_debug("MDMA:RAW_TFR %x\n", status);
  656. status &= mid->intr_mask;
  657. while (status) {
  658. /*txn interrupt*/
  659. i = get_ch_index(&status, mid->chan_base);
  660. if (i < 0) {
  661. pr_err("ERR_MDMA:Invalid ch index %x\n", i);
  662. return;
  663. }
  664. midc = &mid->ch[i];
  665. if (midc == NULL) {
  666. pr_err("ERR_MDMA:Null param midc\n");
  667. return;
  668. }
  669. pr_debug("MDMA:Tx complete interrupt %x, Ch No %d Index %d\n",
  670. status, midc->ch_id, i);
  671. /*clearing this interrupts first*/
  672. iowrite32((1 << midc->ch_id), mid->dma_base + CLEAR_TFR);
  673. iowrite32((1 << midc->ch_id), mid->dma_base + CLEAR_BLOCK);
  674. spin_lock_bh(&midc->lock);
  675. midc_scan_descriptors(mid, midc);
  676. pr_debug("MDMA:Scan of desc... complete, unmasking\n");
  677. iowrite32(UNMASK_INTR_REG(midc->ch_id),
  678. mid->dma_base + MASK_TFR);
  679. spin_unlock_bh(&midc->lock);
  680. }
  681. status = ioread32(mid->dma_base + RAW_ERR);
  682. status &= mid->intr_mask;
  683. while (status) {
  684. /*err interrupt*/
  685. i = get_ch_index(&status, mid->chan_base);
  686. if (i < 0) {
  687. pr_err("ERR_MDMA:Invalid ch index %x\n", i);
  688. return;
  689. }
  690. midc = &mid->ch[i];
  691. if (midc == NULL) {
  692. pr_err("ERR_MDMA:Null param midc\n");
  693. return;
  694. }
  695. pr_debug("MDMA:Tx complete interrupt %x, Ch No %d Index %d\n",
  696. status, midc->ch_id, i);
  697. iowrite32((1 << midc->ch_id), mid->dma_base + CLEAR_ERR);
  698. spin_lock_bh(&midc->lock);
  699. midc_handle_error(mid, midc);
  700. iowrite32(UNMASK_INTR_REG(midc->ch_id),
  701. mid->dma_base + MASK_ERR);
  702. spin_unlock_bh(&midc->lock);
  703. }
  704. pr_debug("MDMA:Exiting takslet...\n");
  705. return;
  706. }
  707. static void dma_tasklet1(unsigned long data)
  708. {
  709. pr_debug("MDMA:in takslet1...\n");
  710. return dma_tasklet(data);
  711. }
  712. static void dma_tasklet2(unsigned long data)
  713. {
  714. pr_debug("MDMA:in takslet2...\n");
  715. return dma_tasklet(data);
  716. }
  717. /**
  718. * intel_mid_dma_interrupt - DMA ISR
  719. * @irq: IRQ where interrupt occurred
  720. * @data: ISR cllback data (the controller structure)
  721. *
  722. * See if this is our interrupt if so then schedule the tasklet
  723. * otherwise ignore
  724. */
  725. static irqreturn_t intel_mid_dma_interrupt(int irq, void *data)
  726. {
  727. struct middma_device *mid = data;
  728. u32 status;
  729. int call_tasklet = 0;
  730. /*DMA Interrupt*/
  731. pr_debug("MDMA:Got an interrupt on irq %d\n", irq);
  732. if (!mid) {
  733. pr_err("ERR_MDMA:null pointer mid\n");
  734. return -EINVAL;
  735. }
  736. status = ioread32(mid->dma_base + RAW_TFR);
  737. pr_debug("MDMA: Status %x, Mask %x\n", status, mid->intr_mask);
  738. status &= mid->intr_mask;
  739. if (status) {
  740. /*need to disable intr*/
  741. iowrite32((status << 8), mid->dma_base + MASK_TFR);
  742. pr_debug("MDMA: Calling tasklet %x\n", status);
  743. call_tasklet = 1;
  744. }
  745. status = ioread32(mid->dma_base + RAW_ERR);
  746. status &= mid->intr_mask;
  747. if (status) {
  748. iowrite32(MASK_INTR_REG(status), mid->dma_base + MASK_ERR);
  749. call_tasklet = 1;
  750. }
  751. if (call_tasklet)
  752. tasklet_schedule(&mid->tasklet);
  753. return IRQ_HANDLED;
  754. }
  755. static irqreturn_t intel_mid_dma_interrupt1(int irq, void *data)
  756. {
  757. return intel_mid_dma_interrupt(irq, data);
  758. }
  759. static irqreturn_t intel_mid_dma_interrupt2(int irq, void *data)
  760. {
  761. return intel_mid_dma_interrupt(irq, data);
  762. }
  763. /**
  764. * mid_setup_dma - Setup the DMA controller
  765. * @pdev: Controller PCI device structure
  766. *
  767. * Initilize the DMA controller, channels, registers with DMA engine,
  768. * ISR. Initilize DMA controller channels.
  769. */
  770. static int mid_setup_dma(struct pci_dev *pdev)
  771. {
  772. struct middma_device *dma = pci_get_drvdata(pdev);
  773. int err, i;
  774. unsigned int irq_level;
  775. /* DMA coherent memory pool for DMA descriptor allocations */
  776. dma->dma_pool = pci_pool_create("intel_mid_dma_desc_pool", pdev,
  777. sizeof(struct intel_mid_dma_desc),
  778. 32, 0);
  779. if (NULL == dma->dma_pool) {
  780. pr_err("ERR_MDMA:pci_pool_create failed\n");
  781. err = -ENOMEM;
  782. kfree(dma);
  783. goto err_dma_pool;
  784. }
  785. INIT_LIST_HEAD(&dma->common.channels);
  786. dma->pci_id = pdev->device;
  787. if (dma->pimr_mask) {
  788. dma->mask_reg = ioremap(LNW_PERIPHRAL_MASK_BASE,
  789. LNW_PERIPHRAL_MASK_SIZE);
  790. if (dma->mask_reg == NULL) {
  791. pr_err("ERR_MDMA:Cant map periphral intr space !!\n");
  792. return -ENOMEM;
  793. }
  794. } else
  795. dma->mask_reg = NULL;
  796. pr_debug("MDMA:Adding %d channel for this controller\n", dma->max_chan);
  797. /*init CH structures*/
  798. dma->intr_mask = 0;
  799. for (i = 0; i < dma->max_chan; i++) {
  800. struct intel_mid_dma_chan *midch = &dma->ch[i];
  801. midch->chan.device = &dma->common;
  802. midch->chan.cookie = 1;
  803. midch->chan.chan_id = i;
  804. midch->ch_id = dma->chan_base + i;
  805. pr_debug("MDMA:Init CH %d, ID %d\n", i, midch->ch_id);
  806. midch->dma_base = dma->dma_base;
  807. midch->ch_regs = dma->dma_base + DMA_CH_SIZE * midch->ch_id;
  808. midch->dma = dma;
  809. dma->intr_mask |= 1 << (dma->chan_base + i);
  810. spin_lock_init(&midch->lock);
  811. INIT_LIST_HEAD(&midch->active_list);
  812. INIT_LIST_HEAD(&midch->queue);
  813. INIT_LIST_HEAD(&midch->free_list);
  814. /*mask interrupts*/
  815. iowrite32(MASK_INTR_REG(midch->ch_id),
  816. dma->dma_base + MASK_BLOCK);
  817. iowrite32(MASK_INTR_REG(midch->ch_id),
  818. dma->dma_base + MASK_SRC_TRAN);
  819. iowrite32(MASK_INTR_REG(midch->ch_id),
  820. dma->dma_base + MASK_DST_TRAN);
  821. iowrite32(MASK_INTR_REG(midch->ch_id),
  822. dma->dma_base + MASK_ERR);
  823. iowrite32(MASK_INTR_REG(midch->ch_id),
  824. dma->dma_base + MASK_TFR);
  825. disable_dma_interrupt(midch);
  826. list_add_tail(&midch->chan.device_node, &dma->common.channels);
  827. }
  828. pr_debug("MDMA: Calc Mask as %x for this controller\n", dma->intr_mask);
  829. /*init dma structure*/
  830. dma_cap_zero(dma->common.cap_mask);
  831. dma_cap_set(DMA_MEMCPY, dma->common.cap_mask);
  832. dma_cap_set(DMA_SLAVE, dma->common.cap_mask);
  833. dma_cap_set(DMA_PRIVATE, dma->common.cap_mask);
  834. dma->common.dev = &pdev->dev;
  835. dma->common.chancnt = dma->max_chan;
  836. dma->common.device_alloc_chan_resources =
  837. intel_mid_dma_alloc_chan_resources;
  838. dma->common.device_free_chan_resources =
  839. intel_mid_dma_free_chan_resources;
  840. dma->common.device_tx_status = intel_mid_dma_tx_status;
  841. dma->common.device_prep_dma_memcpy = intel_mid_dma_prep_memcpy;
  842. dma->common.device_issue_pending = intel_mid_dma_issue_pending;
  843. dma->common.device_prep_slave_sg = intel_mid_dma_prep_slave_sg;
  844. dma->common.device_control = intel_mid_dma_device_control;
  845. /*enable dma cntrl*/
  846. iowrite32(REG_BIT0, dma->dma_base + DMA_CFG);
  847. /*register irq */
  848. if (dma->pimr_mask) {
  849. irq_level = IRQF_SHARED;
  850. pr_debug("MDMA:Requesting irq shared for DMAC1\n");
  851. err = request_irq(pdev->irq, intel_mid_dma_interrupt1,
  852. IRQF_SHARED, "INTEL_MID_DMAC1", dma);
  853. if (0 != err)
  854. goto err_irq;
  855. } else {
  856. dma->intr_mask = 0x03;
  857. irq_level = 0;
  858. pr_debug("MDMA:Requesting irq for DMAC2\n");
  859. err = request_irq(pdev->irq, intel_mid_dma_interrupt2,
  860. 0, "INTEL_MID_DMAC2", dma);
  861. if (0 != err)
  862. goto err_irq;
  863. }
  864. /*register device w/ engine*/
  865. err = dma_async_device_register(&dma->common);
  866. if (0 != err) {
  867. pr_err("ERR_MDMA:device_register failed: %d\n", err);
  868. goto err_engine;
  869. }
  870. if (dma->pimr_mask) {
  871. pr_debug("setting up tasklet1 for DMAC1\n");
  872. tasklet_init(&dma->tasklet, dma_tasklet1, (unsigned long)dma);
  873. } else {
  874. pr_debug("setting up tasklet2 for DMAC2\n");
  875. tasklet_init(&dma->tasklet, dma_tasklet2, (unsigned long)dma);
  876. }
  877. return 0;
  878. err_engine:
  879. free_irq(pdev->irq, dma);
  880. err_irq:
  881. pci_pool_destroy(dma->dma_pool);
  882. kfree(dma);
  883. err_dma_pool:
  884. pr_err("ERR_MDMA:setup_dma failed: %d\n", err);
  885. return err;
  886. }
  887. /**
  888. * middma_shutdown - Shutdown the DMA controller
  889. * @pdev: Controller PCI device structure
  890. *
  891. * Called by remove
  892. * Unregister DMa controller, clear all structures and free interrupt
  893. */
  894. static void middma_shutdown(struct pci_dev *pdev)
  895. {
  896. struct middma_device *device = pci_get_drvdata(pdev);
  897. dma_async_device_unregister(&device->common);
  898. pci_pool_destroy(device->dma_pool);
  899. if (device->mask_reg)
  900. iounmap(device->mask_reg);
  901. if (device->dma_base)
  902. iounmap(device->dma_base);
  903. free_irq(pdev->irq, device);
  904. return;
  905. }
  906. /**
  907. * intel_mid_dma_probe - PCI Probe
  908. * @pdev: Controller PCI device structure
  909. * @id: pci device id structure
  910. *
  911. * Initilize the PCI device, map BARs, query driver data.
  912. * Call setup_dma to complete contoller and chan initilzation
  913. */
  914. static int __devinit intel_mid_dma_probe(struct pci_dev *pdev,
  915. const struct pci_device_id *id)
  916. {
  917. struct middma_device *device;
  918. u32 base_addr, bar_size;
  919. struct intel_mid_dma_probe_info *info;
  920. int err;
  921. pr_debug("MDMA: probe for %x\n", pdev->device);
  922. info = (void *)id->driver_data;
  923. pr_debug("MDMA: CH %d, base %d, block len %d, Periphral mask %x\n",
  924. info->max_chan, info->ch_base,
  925. info->block_size, info->pimr_mask);
  926. err = pci_enable_device(pdev);
  927. if (err)
  928. goto err_enable_device;
  929. err = pci_request_regions(pdev, "intel_mid_dmac");
  930. if (err)
  931. goto err_request_regions;
  932. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  933. if (err)
  934. goto err_set_dma_mask;
  935. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  936. if (err)
  937. goto err_set_dma_mask;
  938. device = kzalloc(sizeof(*device), GFP_KERNEL);
  939. if (!device) {
  940. pr_err("ERR_MDMA:kzalloc failed probe\n");
  941. err = -ENOMEM;
  942. goto err_kzalloc;
  943. }
  944. device->pdev = pci_dev_get(pdev);
  945. base_addr = pci_resource_start(pdev, 0);
  946. bar_size = pci_resource_len(pdev, 0);
  947. device->dma_base = ioremap_nocache(base_addr, DMA_REG_SIZE);
  948. if (!device->dma_base) {
  949. pr_err("ERR_MDMA:ioremap failed\n");
  950. err = -ENOMEM;
  951. goto err_ioremap;
  952. }
  953. pci_set_drvdata(pdev, device);
  954. pci_set_master(pdev);
  955. device->max_chan = info->max_chan;
  956. device->chan_base = info->ch_base;
  957. device->block_size = info->block_size;
  958. device->pimr_mask = info->pimr_mask;
  959. err = mid_setup_dma(pdev);
  960. if (err)
  961. goto err_dma;
  962. return 0;
  963. err_dma:
  964. iounmap(device->dma_base);
  965. err_ioremap:
  966. pci_dev_put(pdev);
  967. kfree(device);
  968. err_kzalloc:
  969. err_set_dma_mask:
  970. pci_release_regions(pdev);
  971. pci_disable_device(pdev);
  972. err_request_regions:
  973. err_enable_device:
  974. pr_err("ERR_MDMA:Probe failed %d\n", err);
  975. return err;
  976. }
  977. /**
  978. * intel_mid_dma_remove - PCI remove
  979. * @pdev: Controller PCI device structure
  980. *
  981. * Free up all resources and data
  982. * Call shutdown_dma to complete contoller and chan cleanup
  983. */
  984. static void __devexit intel_mid_dma_remove(struct pci_dev *pdev)
  985. {
  986. struct middma_device *device = pci_get_drvdata(pdev);
  987. middma_shutdown(pdev);
  988. pci_dev_put(pdev);
  989. kfree(device);
  990. pci_release_regions(pdev);
  991. pci_disable_device(pdev);
  992. }
  993. /******************************************************************************
  994. * PCI stuff
  995. */
  996. static struct pci_device_id intel_mid_dma_ids[] = {
  997. { PCI_VDEVICE(INTEL, INTEL_MID_DMAC1_ID), INFO(2, 6, 4095, 0x200020)},
  998. { PCI_VDEVICE(INTEL, INTEL_MID_DMAC2_ID), INFO(2, 0, 2047, 0)},
  999. { PCI_VDEVICE(INTEL, INTEL_MID_GP_DMAC2_ID), INFO(2, 0, 2047, 0)},
  1000. { PCI_VDEVICE(INTEL, INTEL_MFLD_DMAC1_ID), INFO(4, 0, 4095, 0x400040)},
  1001. { 0, }
  1002. };
  1003. MODULE_DEVICE_TABLE(pci, intel_mid_dma_ids);
  1004. static struct pci_driver intel_mid_dma_pci = {
  1005. .name = "Intel MID DMA",
  1006. .id_table = intel_mid_dma_ids,
  1007. .probe = intel_mid_dma_probe,
  1008. .remove = __devexit_p(intel_mid_dma_remove),
  1009. };
  1010. static int __init intel_mid_dma_init(void)
  1011. {
  1012. pr_debug("INFO_MDMA: LNW DMA Driver Version %s\n",
  1013. INTEL_MID_DMA_DRIVER_VERSION);
  1014. return pci_register_driver(&intel_mid_dma_pci);
  1015. }
  1016. fs_initcall(intel_mid_dma_init);
  1017. static void __exit intel_mid_dma_exit(void)
  1018. {
  1019. pci_unregister_driver(&intel_mid_dma_pci);
  1020. }
  1021. module_exit(intel_mid_dma_exit);
  1022. MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
  1023. MODULE_DESCRIPTION("Intel (R) MID DMAC Driver");
  1024. MODULE_LICENSE("GPL v2");
  1025. MODULE_VERSION(INTEL_MID_DMA_DRIVER_VERSION);