mv_xor.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. /*
  2. * offload engine driver for the Marvell XOR engine
  3. * Copyright (C) 2007, 2008, Marvell International Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/memory.h>
  27. #include <linux/clk.h>
  28. #include <linux/of.h>
  29. #include <linux/of_irq.h>
  30. #include <linux/irqdomain.h>
  31. #include <linux/platform_data/dma-mv_xor.h>
  32. #include "dmaengine.h"
  33. #include "mv_xor.h"
  34. static void mv_xor_issue_pending(struct dma_chan *chan);
  35. #define to_mv_xor_chan(chan) \
  36. container_of(chan, struct mv_xor_chan, dmachan)
  37. #define to_mv_xor_slot(tx) \
  38. container_of(tx, struct mv_xor_desc_slot, async_tx)
  39. #define mv_chan_to_devp(chan) \
  40. ((chan)->dmadev.dev)
  41. static void mv_desc_init(struct mv_xor_desc_slot *desc, unsigned long flags)
  42. {
  43. struct mv_xor_desc *hw_desc = desc->hw_desc;
  44. hw_desc->status = (1 << 31);
  45. hw_desc->phy_next_desc = 0;
  46. hw_desc->desc_command = (1 << 31);
  47. }
  48. static u32 mv_desc_get_dest_addr(struct mv_xor_desc_slot *desc)
  49. {
  50. struct mv_xor_desc *hw_desc = desc->hw_desc;
  51. return hw_desc->phy_dest_addr;
  52. }
  53. static u32 mv_desc_get_src_addr(struct mv_xor_desc_slot *desc,
  54. int src_idx)
  55. {
  56. struct mv_xor_desc *hw_desc = desc->hw_desc;
  57. return hw_desc->phy_src_addr[src_idx];
  58. }
  59. static void mv_desc_set_byte_count(struct mv_xor_desc_slot *desc,
  60. u32 byte_count)
  61. {
  62. struct mv_xor_desc *hw_desc = desc->hw_desc;
  63. hw_desc->byte_count = byte_count;
  64. }
  65. static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc,
  66. u32 next_desc_addr)
  67. {
  68. struct mv_xor_desc *hw_desc = desc->hw_desc;
  69. BUG_ON(hw_desc->phy_next_desc);
  70. hw_desc->phy_next_desc = next_desc_addr;
  71. }
  72. static void mv_desc_clear_next_desc(struct mv_xor_desc_slot *desc)
  73. {
  74. struct mv_xor_desc *hw_desc = desc->hw_desc;
  75. hw_desc->phy_next_desc = 0;
  76. }
  77. static void mv_desc_set_dest_addr(struct mv_xor_desc_slot *desc,
  78. dma_addr_t addr)
  79. {
  80. struct mv_xor_desc *hw_desc = desc->hw_desc;
  81. hw_desc->phy_dest_addr = addr;
  82. }
  83. static int mv_chan_memset_slot_count(size_t len)
  84. {
  85. return 1;
  86. }
  87. #define mv_chan_memcpy_slot_count(c) mv_chan_memset_slot_count(c)
  88. static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc,
  89. int index, dma_addr_t addr)
  90. {
  91. struct mv_xor_desc *hw_desc = desc->hw_desc;
  92. hw_desc->phy_src_addr[index] = addr;
  93. if (desc->type == DMA_XOR)
  94. hw_desc->desc_command |= (1 << index);
  95. }
  96. static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan)
  97. {
  98. return __raw_readl(XOR_CURR_DESC(chan));
  99. }
  100. static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan,
  101. u32 next_desc_addr)
  102. {
  103. __raw_writel(next_desc_addr, XOR_NEXT_DESC(chan));
  104. }
  105. static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan)
  106. {
  107. u32 val = __raw_readl(XOR_INTR_MASK(chan));
  108. val |= XOR_INTR_MASK_VALUE << (chan->idx * 16);
  109. __raw_writel(val, XOR_INTR_MASK(chan));
  110. }
  111. static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan)
  112. {
  113. u32 intr_cause = __raw_readl(XOR_INTR_CAUSE(chan));
  114. intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF;
  115. return intr_cause;
  116. }
  117. static int mv_is_err_intr(u32 intr_cause)
  118. {
  119. if (intr_cause & ((1<<4)|(1<<5)|(1<<6)|(1<<7)|(1<<8)|(1<<9)))
  120. return 1;
  121. return 0;
  122. }
  123. static void mv_xor_device_clear_eoc_cause(struct mv_xor_chan *chan)
  124. {
  125. u32 val = ~(1 << (chan->idx * 16));
  126. dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val);
  127. __raw_writel(val, XOR_INTR_CAUSE(chan));
  128. }
  129. static void mv_xor_device_clear_err_status(struct mv_xor_chan *chan)
  130. {
  131. u32 val = 0xFFFF0000 >> (chan->idx * 16);
  132. __raw_writel(val, XOR_INTR_CAUSE(chan));
  133. }
  134. static int mv_can_chain(struct mv_xor_desc_slot *desc)
  135. {
  136. struct mv_xor_desc_slot *chain_old_tail = list_entry(
  137. desc->chain_node.prev, struct mv_xor_desc_slot, chain_node);
  138. if (chain_old_tail->type != desc->type)
  139. return 0;
  140. return 1;
  141. }
  142. static void mv_set_mode(struct mv_xor_chan *chan,
  143. enum dma_transaction_type type)
  144. {
  145. u32 op_mode;
  146. u32 config = __raw_readl(XOR_CONFIG(chan));
  147. switch (type) {
  148. case DMA_XOR:
  149. op_mode = XOR_OPERATION_MODE_XOR;
  150. break;
  151. case DMA_MEMCPY:
  152. op_mode = XOR_OPERATION_MODE_MEMCPY;
  153. break;
  154. default:
  155. dev_err(mv_chan_to_devp(chan),
  156. "error: unsupported operation %d\n",
  157. type);
  158. BUG();
  159. return;
  160. }
  161. config &= ~0x7;
  162. config |= op_mode;
  163. __raw_writel(config, XOR_CONFIG(chan));
  164. chan->current_type = type;
  165. }
  166. static void mv_chan_activate(struct mv_xor_chan *chan)
  167. {
  168. u32 activation;
  169. dev_dbg(mv_chan_to_devp(chan), " activate chan.\n");
  170. activation = __raw_readl(XOR_ACTIVATION(chan));
  171. activation |= 0x1;
  172. __raw_writel(activation, XOR_ACTIVATION(chan));
  173. }
  174. static char mv_chan_is_busy(struct mv_xor_chan *chan)
  175. {
  176. u32 state = __raw_readl(XOR_ACTIVATION(chan));
  177. state = (state >> 4) & 0x3;
  178. return (state == 1) ? 1 : 0;
  179. }
  180. static int mv_chan_xor_slot_count(size_t len, int src_cnt)
  181. {
  182. return 1;
  183. }
  184. /**
  185. * mv_xor_free_slots - flags descriptor slots for reuse
  186. * @slot: Slot to free
  187. * Caller must hold &mv_chan->lock while calling this function
  188. */
  189. static void mv_xor_free_slots(struct mv_xor_chan *mv_chan,
  190. struct mv_xor_desc_slot *slot)
  191. {
  192. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d slot %p\n",
  193. __func__, __LINE__, slot);
  194. slot->slots_per_op = 0;
  195. }
  196. /*
  197. * mv_xor_start_new_chain - program the engine to operate on new chain headed by
  198. * sw_desc
  199. * Caller must hold &mv_chan->lock while calling this function
  200. */
  201. static void mv_xor_start_new_chain(struct mv_xor_chan *mv_chan,
  202. struct mv_xor_desc_slot *sw_desc)
  203. {
  204. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n",
  205. __func__, __LINE__, sw_desc);
  206. if (sw_desc->type != mv_chan->current_type)
  207. mv_set_mode(mv_chan, sw_desc->type);
  208. /* set the hardware chain */
  209. mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys);
  210. mv_chan->pending += sw_desc->slot_cnt;
  211. mv_xor_issue_pending(&mv_chan->dmachan);
  212. }
  213. static dma_cookie_t
  214. mv_xor_run_tx_complete_actions(struct mv_xor_desc_slot *desc,
  215. struct mv_xor_chan *mv_chan, dma_cookie_t cookie)
  216. {
  217. BUG_ON(desc->async_tx.cookie < 0);
  218. if (desc->async_tx.cookie > 0) {
  219. cookie = desc->async_tx.cookie;
  220. /* call the callback (must not sleep or submit new
  221. * operations to this channel)
  222. */
  223. if (desc->async_tx.callback)
  224. desc->async_tx.callback(
  225. desc->async_tx.callback_param);
  226. /* unmap dma addresses
  227. * (unmap_single vs unmap_page?)
  228. */
  229. if (desc->group_head && desc->unmap_len) {
  230. struct mv_xor_desc_slot *unmap = desc->group_head;
  231. struct device *dev = mv_chan_to_devp(mv_chan);
  232. u32 len = unmap->unmap_len;
  233. enum dma_ctrl_flags flags = desc->async_tx.flags;
  234. u32 src_cnt;
  235. dma_addr_t addr;
  236. dma_addr_t dest;
  237. src_cnt = unmap->unmap_src_cnt;
  238. dest = mv_desc_get_dest_addr(unmap);
  239. if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  240. enum dma_data_direction dir;
  241. if (src_cnt > 1) /* is xor ? */
  242. dir = DMA_BIDIRECTIONAL;
  243. else
  244. dir = DMA_FROM_DEVICE;
  245. dma_unmap_page(dev, dest, len, dir);
  246. }
  247. if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  248. while (src_cnt--) {
  249. addr = mv_desc_get_src_addr(unmap,
  250. src_cnt);
  251. if (addr == dest)
  252. continue;
  253. dma_unmap_page(dev, addr, len,
  254. DMA_TO_DEVICE);
  255. }
  256. }
  257. desc->group_head = NULL;
  258. }
  259. }
  260. /* run dependent operations */
  261. dma_run_dependencies(&desc->async_tx);
  262. return cookie;
  263. }
  264. static int
  265. mv_xor_clean_completed_slots(struct mv_xor_chan *mv_chan)
  266. {
  267. struct mv_xor_desc_slot *iter, *_iter;
  268. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
  269. list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
  270. completed_node) {
  271. if (async_tx_test_ack(&iter->async_tx)) {
  272. list_del(&iter->completed_node);
  273. mv_xor_free_slots(mv_chan, iter);
  274. }
  275. }
  276. return 0;
  277. }
  278. static int
  279. mv_xor_clean_slot(struct mv_xor_desc_slot *desc,
  280. struct mv_xor_chan *mv_chan)
  281. {
  282. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n",
  283. __func__, __LINE__, desc, desc->async_tx.flags);
  284. list_del(&desc->chain_node);
  285. /* the client is allowed to attach dependent operations
  286. * until 'ack' is set
  287. */
  288. if (!async_tx_test_ack(&desc->async_tx)) {
  289. /* move this slot to the completed_slots */
  290. list_add_tail(&desc->completed_node, &mv_chan->completed_slots);
  291. return 0;
  292. }
  293. mv_xor_free_slots(mv_chan, desc);
  294. return 0;
  295. }
  296. static void __mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan)
  297. {
  298. struct mv_xor_desc_slot *iter, *_iter;
  299. dma_cookie_t cookie = 0;
  300. int busy = mv_chan_is_busy(mv_chan);
  301. u32 current_desc = mv_chan_get_current_desc(mv_chan);
  302. int seen_current = 0;
  303. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
  304. dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc);
  305. mv_xor_clean_completed_slots(mv_chan);
  306. /* free completed slots from the chain starting with
  307. * the oldest descriptor
  308. */
  309. list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
  310. chain_node) {
  311. prefetch(_iter);
  312. prefetch(&_iter->async_tx);
  313. /* do not advance past the current descriptor loaded into the
  314. * hardware channel, subsequent descriptors are either in
  315. * process or have not been submitted
  316. */
  317. if (seen_current)
  318. break;
  319. /* stop the search if we reach the current descriptor and the
  320. * channel is busy
  321. */
  322. if (iter->async_tx.phys == current_desc) {
  323. seen_current = 1;
  324. if (busy)
  325. break;
  326. }
  327. cookie = mv_xor_run_tx_complete_actions(iter, mv_chan, cookie);
  328. if (mv_xor_clean_slot(iter, mv_chan))
  329. break;
  330. }
  331. if ((busy == 0) && !list_empty(&mv_chan->chain)) {
  332. struct mv_xor_desc_slot *chain_head;
  333. chain_head = list_entry(mv_chan->chain.next,
  334. struct mv_xor_desc_slot,
  335. chain_node);
  336. mv_xor_start_new_chain(mv_chan, chain_head);
  337. }
  338. if (cookie > 0)
  339. mv_chan->dmachan.completed_cookie = cookie;
  340. }
  341. static void
  342. mv_xor_slot_cleanup(struct mv_xor_chan *mv_chan)
  343. {
  344. spin_lock_bh(&mv_chan->lock);
  345. __mv_xor_slot_cleanup(mv_chan);
  346. spin_unlock_bh(&mv_chan->lock);
  347. }
  348. static void mv_xor_tasklet(unsigned long data)
  349. {
  350. struct mv_xor_chan *chan = (struct mv_xor_chan *) data;
  351. mv_xor_slot_cleanup(chan);
  352. }
  353. static struct mv_xor_desc_slot *
  354. mv_xor_alloc_slots(struct mv_xor_chan *mv_chan, int num_slots,
  355. int slots_per_op)
  356. {
  357. struct mv_xor_desc_slot *iter, *_iter, *alloc_start = NULL;
  358. LIST_HEAD(chain);
  359. int slots_found, retry = 0;
  360. /* start search from the last allocated descrtiptor
  361. * if a contiguous allocation can not be found start searching
  362. * from the beginning of the list
  363. */
  364. retry:
  365. slots_found = 0;
  366. if (retry == 0)
  367. iter = mv_chan->last_used;
  368. else
  369. iter = list_entry(&mv_chan->all_slots,
  370. struct mv_xor_desc_slot,
  371. slot_node);
  372. list_for_each_entry_safe_continue(
  373. iter, _iter, &mv_chan->all_slots, slot_node) {
  374. prefetch(_iter);
  375. prefetch(&_iter->async_tx);
  376. if (iter->slots_per_op) {
  377. /* give up after finding the first busy slot
  378. * on the second pass through the list
  379. */
  380. if (retry)
  381. break;
  382. slots_found = 0;
  383. continue;
  384. }
  385. /* start the allocation if the slot is correctly aligned */
  386. if (!slots_found++)
  387. alloc_start = iter;
  388. if (slots_found == num_slots) {
  389. struct mv_xor_desc_slot *alloc_tail = NULL;
  390. struct mv_xor_desc_slot *last_used = NULL;
  391. iter = alloc_start;
  392. while (num_slots) {
  393. int i;
  394. /* pre-ack all but the last descriptor */
  395. async_tx_ack(&iter->async_tx);
  396. list_add_tail(&iter->chain_node, &chain);
  397. alloc_tail = iter;
  398. iter->async_tx.cookie = 0;
  399. iter->slot_cnt = num_slots;
  400. iter->xor_check_result = NULL;
  401. for (i = 0; i < slots_per_op; i++) {
  402. iter->slots_per_op = slots_per_op - i;
  403. last_used = iter;
  404. iter = list_entry(iter->slot_node.next,
  405. struct mv_xor_desc_slot,
  406. slot_node);
  407. }
  408. num_slots -= slots_per_op;
  409. }
  410. alloc_tail->group_head = alloc_start;
  411. alloc_tail->async_tx.cookie = -EBUSY;
  412. list_splice(&chain, &alloc_tail->tx_list);
  413. mv_chan->last_used = last_used;
  414. mv_desc_clear_next_desc(alloc_start);
  415. mv_desc_clear_next_desc(alloc_tail);
  416. return alloc_tail;
  417. }
  418. }
  419. if (!retry++)
  420. goto retry;
  421. /* try to free some slots if the allocation fails */
  422. tasklet_schedule(&mv_chan->irq_tasklet);
  423. return NULL;
  424. }
  425. /************************ DMA engine API functions ****************************/
  426. static dma_cookie_t
  427. mv_xor_tx_submit(struct dma_async_tx_descriptor *tx)
  428. {
  429. struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx);
  430. struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan);
  431. struct mv_xor_desc_slot *grp_start, *old_chain_tail;
  432. dma_cookie_t cookie;
  433. int new_hw_chain = 1;
  434. dev_dbg(mv_chan_to_devp(mv_chan),
  435. "%s sw_desc %p: async_tx %p\n",
  436. __func__, sw_desc, &sw_desc->async_tx);
  437. grp_start = sw_desc->group_head;
  438. spin_lock_bh(&mv_chan->lock);
  439. cookie = dma_cookie_assign(tx);
  440. if (list_empty(&mv_chan->chain))
  441. list_splice_init(&sw_desc->tx_list, &mv_chan->chain);
  442. else {
  443. new_hw_chain = 0;
  444. old_chain_tail = list_entry(mv_chan->chain.prev,
  445. struct mv_xor_desc_slot,
  446. chain_node);
  447. list_splice_init(&grp_start->tx_list,
  448. &old_chain_tail->chain_node);
  449. if (!mv_can_chain(grp_start))
  450. goto submit_done;
  451. dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %x\n",
  452. old_chain_tail->async_tx.phys);
  453. /* fix up the hardware chain */
  454. mv_desc_set_next_desc(old_chain_tail, grp_start->async_tx.phys);
  455. /* if the channel is not busy */
  456. if (!mv_chan_is_busy(mv_chan)) {
  457. u32 current_desc = mv_chan_get_current_desc(mv_chan);
  458. /*
  459. * and the curren desc is the end of the chain before
  460. * the append, then we need to start the channel
  461. */
  462. if (current_desc == old_chain_tail->async_tx.phys)
  463. new_hw_chain = 1;
  464. }
  465. }
  466. if (new_hw_chain)
  467. mv_xor_start_new_chain(mv_chan, grp_start);
  468. submit_done:
  469. spin_unlock_bh(&mv_chan->lock);
  470. return cookie;
  471. }
  472. /* returns the number of allocated descriptors */
  473. static int mv_xor_alloc_chan_resources(struct dma_chan *chan)
  474. {
  475. char *hw_desc;
  476. int idx;
  477. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  478. struct mv_xor_desc_slot *slot = NULL;
  479. int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE;
  480. /* Allocate descriptor slots */
  481. idx = mv_chan->slots_allocated;
  482. while (idx < num_descs_in_pool) {
  483. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  484. if (!slot) {
  485. printk(KERN_INFO "MV XOR Channel only initialized"
  486. " %d descriptor slots", idx);
  487. break;
  488. }
  489. hw_desc = (char *) mv_chan->dma_desc_pool_virt;
  490. slot->hw_desc = (void *) &hw_desc[idx * MV_XOR_SLOT_SIZE];
  491. dma_async_tx_descriptor_init(&slot->async_tx, chan);
  492. slot->async_tx.tx_submit = mv_xor_tx_submit;
  493. INIT_LIST_HEAD(&slot->chain_node);
  494. INIT_LIST_HEAD(&slot->slot_node);
  495. INIT_LIST_HEAD(&slot->tx_list);
  496. hw_desc = (char *) mv_chan->dma_desc_pool;
  497. slot->async_tx.phys =
  498. (dma_addr_t) &hw_desc[idx * MV_XOR_SLOT_SIZE];
  499. slot->idx = idx++;
  500. spin_lock_bh(&mv_chan->lock);
  501. mv_chan->slots_allocated = idx;
  502. list_add_tail(&slot->slot_node, &mv_chan->all_slots);
  503. spin_unlock_bh(&mv_chan->lock);
  504. }
  505. if (mv_chan->slots_allocated && !mv_chan->last_used)
  506. mv_chan->last_used = list_entry(mv_chan->all_slots.next,
  507. struct mv_xor_desc_slot,
  508. slot_node);
  509. dev_dbg(mv_chan_to_devp(mv_chan),
  510. "allocated %d descriptor slots last_used: %p\n",
  511. mv_chan->slots_allocated, mv_chan->last_used);
  512. return mv_chan->slots_allocated ? : -ENOMEM;
  513. }
  514. static struct dma_async_tx_descriptor *
  515. mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  516. size_t len, unsigned long flags)
  517. {
  518. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  519. struct mv_xor_desc_slot *sw_desc, *grp_start;
  520. int slot_cnt;
  521. dev_dbg(mv_chan_to_devp(mv_chan),
  522. "%s dest: %x src %x len: %u flags: %ld\n",
  523. __func__, dest, src, len, flags);
  524. if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
  525. return NULL;
  526. BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
  527. spin_lock_bh(&mv_chan->lock);
  528. slot_cnt = mv_chan_memcpy_slot_count(len);
  529. sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1);
  530. if (sw_desc) {
  531. sw_desc->type = DMA_MEMCPY;
  532. sw_desc->async_tx.flags = flags;
  533. grp_start = sw_desc->group_head;
  534. mv_desc_init(grp_start, flags);
  535. mv_desc_set_byte_count(grp_start, len);
  536. mv_desc_set_dest_addr(sw_desc->group_head, dest);
  537. mv_desc_set_src_addr(grp_start, 0, src);
  538. sw_desc->unmap_src_cnt = 1;
  539. sw_desc->unmap_len = len;
  540. }
  541. spin_unlock_bh(&mv_chan->lock);
  542. dev_dbg(mv_chan_to_devp(mv_chan),
  543. "%s sw_desc %p async_tx %p\n",
  544. __func__, sw_desc, sw_desc ? &sw_desc->async_tx : NULL);
  545. return sw_desc ? &sw_desc->async_tx : NULL;
  546. }
  547. static struct dma_async_tx_descriptor *
  548. mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  549. unsigned int src_cnt, size_t len, unsigned long flags)
  550. {
  551. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  552. struct mv_xor_desc_slot *sw_desc, *grp_start;
  553. int slot_cnt;
  554. if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
  555. return NULL;
  556. BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
  557. dev_dbg(mv_chan_to_devp(mv_chan),
  558. "%s src_cnt: %d len: dest %x %u flags: %ld\n",
  559. __func__, src_cnt, len, dest, flags);
  560. spin_lock_bh(&mv_chan->lock);
  561. slot_cnt = mv_chan_xor_slot_count(len, src_cnt);
  562. sw_desc = mv_xor_alloc_slots(mv_chan, slot_cnt, 1);
  563. if (sw_desc) {
  564. sw_desc->type = DMA_XOR;
  565. sw_desc->async_tx.flags = flags;
  566. grp_start = sw_desc->group_head;
  567. mv_desc_init(grp_start, flags);
  568. /* the byte count field is the same as in memcpy desc*/
  569. mv_desc_set_byte_count(grp_start, len);
  570. mv_desc_set_dest_addr(sw_desc->group_head, dest);
  571. sw_desc->unmap_src_cnt = src_cnt;
  572. sw_desc->unmap_len = len;
  573. while (src_cnt--)
  574. mv_desc_set_src_addr(grp_start, src_cnt, src[src_cnt]);
  575. }
  576. spin_unlock_bh(&mv_chan->lock);
  577. dev_dbg(mv_chan_to_devp(mv_chan),
  578. "%s sw_desc %p async_tx %p \n",
  579. __func__, sw_desc, &sw_desc->async_tx);
  580. return sw_desc ? &sw_desc->async_tx : NULL;
  581. }
  582. static void mv_xor_free_chan_resources(struct dma_chan *chan)
  583. {
  584. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  585. struct mv_xor_desc_slot *iter, *_iter;
  586. int in_use_descs = 0;
  587. mv_xor_slot_cleanup(mv_chan);
  588. spin_lock_bh(&mv_chan->lock);
  589. list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
  590. chain_node) {
  591. in_use_descs++;
  592. list_del(&iter->chain_node);
  593. }
  594. list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
  595. completed_node) {
  596. in_use_descs++;
  597. list_del(&iter->completed_node);
  598. }
  599. list_for_each_entry_safe_reverse(
  600. iter, _iter, &mv_chan->all_slots, slot_node) {
  601. list_del(&iter->slot_node);
  602. kfree(iter);
  603. mv_chan->slots_allocated--;
  604. }
  605. mv_chan->last_used = NULL;
  606. dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n",
  607. __func__, mv_chan->slots_allocated);
  608. spin_unlock_bh(&mv_chan->lock);
  609. if (in_use_descs)
  610. dev_err(mv_chan_to_devp(mv_chan),
  611. "freeing %d in use descriptors!\n", in_use_descs);
  612. }
  613. /**
  614. * mv_xor_status - poll the status of an XOR transaction
  615. * @chan: XOR channel handle
  616. * @cookie: XOR transaction identifier
  617. * @txstate: XOR transactions state holder (or NULL)
  618. */
  619. static enum dma_status mv_xor_status(struct dma_chan *chan,
  620. dma_cookie_t cookie,
  621. struct dma_tx_state *txstate)
  622. {
  623. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  624. enum dma_status ret;
  625. ret = dma_cookie_status(chan, cookie, txstate);
  626. if (ret == DMA_SUCCESS) {
  627. mv_xor_clean_completed_slots(mv_chan);
  628. return ret;
  629. }
  630. mv_xor_slot_cleanup(mv_chan);
  631. return dma_cookie_status(chan, cookie, txstate);
  632. }
  633. static void mv_dump_xor_regs(struct mv_xor_chan *chan)
  634. {
  635. u32 val;
  636. val = __raw_readl(XOR_CONFIG(chan));
  637. dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val);
  638. val = __raw_readl(XOR_ACTIVATION(chan));
  639. dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val);
  640. val = __raw_readl(XOR_INTR_CAUSE(chan));
  641. dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val);
  642. val = __raw_readl(XOR_INTR_MASK(chan));
  643. dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val);
  644. val = __raw_readl(XOR_ERROR_CAUSE(chan));
  645. dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val);
  646. val = __raw_readl(XOR_ERROR_ADDR(chan));
  647. dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val);
  648. }
  649. static void mv_xor_err_interrupt_handler(struct mv_xor_chan *chan,
  650. u32 intr_cause)
  651. {
  652. if (intr_cause & (1 << 4)) {
  653. dev_dbg(mv_chan_to_devp(chan),
  654. "ignore this error\n");
  655. return;
  656. }
  657. dev_err(mv_chan_to_devp(chan),
  658. "error on chan %d. intr cause 0x%08x\n",
  659. chan->idx, intr_cause);
  660. mv_dump_xor_regs(chan);
  661. BUG();
  662. }
  663. static irqreturn_t mv_xor_interrupt_handler(int irq, void *data)
  664. {
  665. struct mv_xor_chan *chan = data;
  666. u32 intr_cause = mv_chan_get_intr_cause(chan);
  667. dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause);
  668. if (mv_is_err_intr(intr_cause))
  669. mv_xor_err_interrupt_handler(chan, intr_cause);
  670. tasklet_schedule(&chan->irq_tasklet);
  671. mv_xor_device_clear_eoc_cause(chan);
  672. return IRQ_HANDLED;
  673. }
  674. static void mv_xor_issue_pending(struct dma_chan *chan)
  675. {
  676. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  677. if (mv_chan->pending >= MV_XOR_THRESHOLD) {
  678. mv_chan->pending = 0;
  679. mv_chan_activate(mv_chan);
  680. }
  681. }
  682. /*
  683. * Perform a transaction to verify the HW works.
  684. */
  685. #define MV_XOR_TEST_SIZE 2000
  686. static int mv_xor_memcpy_self_test(struct mv_xor_chan *mv_chan)
  687. {
  688. int i;
  689. void *src, *dest;
  690. dma_addr_t src_dma, dest_dma;
  691. struct dma_chan *dma_chan;
  692. dma_cookie_t cookie;
  693. struct dma_async_tx_descriptor *tx;
  694. int err = 0;
  695. src = kmalloc(sizeof(u8) * MV_XOR_TEST_SIZE, GFP_KERNEL);
  696. if (!src)
  697. return -ENOMEM;
  698. dest = kzalloc(sizeof(u8) * MV_XOR_TEST_SIZE, GFP_KERNEL);
  699. if (!dest) {
  700. kfree(src);
  701. return -ENOMEM;
  702. }
  703. /* Fill in src buffer */
  704. for (i = 0; i < MV_XOR_TEST_SIZE; i++)
  705. ((u8 *) src)[i] = (u8)i;
  706. dma_chan = &mv_chan->dmachan;
  707. if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
  708. err = -ENODEV;
  709. goto out;
  710. }
  711. dest_dma = dma_map_single(dma_chan->device->dev, dest,
  712. MV_XOR_TEST_SIZE, DMA_FROM_DEVICE);
  713. src_dma = dma_map_single(dma_chan->device->dev, src,
  714. MV_XOR_TEST_SIZE, DMA_TO_DEVICE);
  715. tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
  716. MV_XOR_TEST_SIZE, 0);
  717. cookie = mv_xor_tx_submit(tx);
  718. mv_xor_issue_pending(dma_chan);
  719. async_tx_ack(tx);
  720. msleep(1);
  721. if (mv_xor_status(dma_chan, cookie, NULL) !=
  722. DMA_SUCCESS) {
  723. dev_err(dma_chan->device->dev,
  724. "Self-test copy timed out, disabling\n");
  725. err = -ENODEV;
  726. goto free_resources;
  727. }
  728. dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
  729. MV_XOR_TEST_SIZE, DMA_FROM_DEVICE);
  730. if (memcmp(src, dest, MV_XOR_TEST_SIZE)) {
  731. dev_err(dma_chan->device->dev,
  732. "Self-test copy failed compare, disabling\n");
  733. err = -ENODEV;
  734. goto free_resources;
  735. }
  736. free_resources:
  737. mv_xor_free_chan_resources(dma_chan);
  738. out:
  739. kfree(src);
  740. kfree(dest);
  741. return err;
  742. }
  743. #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
  744. static int
  745. mv_xor_xor_self_test(struct mv_xor_chan *mv_chan)
  746. {
  747. int i, src_idx;
  748. struct page *dest;
  749. struct page *xor_srcs[MV_XOR_NUM_SRC_TEST];
  750. dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST];
  751. dma_addr_t dest_dma;
  752. struct dma_async_tx_descriptor *tx;
  753. struct dma_chan *dma_chan;
  754. dma_cookie_t cookie;
  755. u8 cmp_byte = 0;
  756. u32 cmp_word;
  757. int err = 0;
  758. for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++) {
  759. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  760. if (!xor_srcs[src_idx]) {
  761. while (src_idx--)
  762. __free_page(xor_srcs[src_idx]);
  763. return -ENOMEM;
  764. }
  765. }
  766. dest = alloc_page(GFP_KERNEL);
  767. if (!dest) {
  768. while (src_idx--)
  769. __free_page(xor_srcs[src_idx]);
  770. return -ENOMEM;
  771. }
  772. /* Fill in src buffers */
  773. for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++) {
  774. u8 *ptr = page_address(xor_srcs[src_idx]);
  775. for (i = 0; i < PAGE_SIZE; i++)
  776. ptr[i] = (1 << src_idx);
  777. }
  778. for (src_idx = 0; src_idx < MV_XOR_NUM_SRC_TEST; src_idx++)
  779. cmp_byte ^= (u8) (1 << src_idx);
  780. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  781. (cmp_byte << 8) | cmp_byte;
  782. memset(page_address(dest), 0, PAGE_SIZE);
  783. dma_chan = &mv_chan->dmachan;
  784. if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
  785. err = -ENODEV;
  786. goto out;
  787. }
  788. /* test xor */
  789. dest_dma = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE,
  790. DMA_FROM_DEVICE);
  791. for (i = 0; i < MV_XOR_NUM_SRC_TEST; i++)
  792. dma_srcs[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
  793. 0, PAGE_SIZE, DMA_TO_DEVICE);
  794. tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  795. MV_XOR_NUM_SRC_TEST, PAGE_SIZE, 0);
  796. cookie = mv_xor_tx_submit(tx);
  797. mv_xor_issue_pending(dma_chan);
  798. async_tx_ack(tx);
  799. msleep(8);
  800. if (mv_xor_status(dma_chan, cookie, NULL) !=
  801. DMA_SUCCESS) {
  802. dev_err(dma_chan->device->dev,
  803. "Self-test xor timed out, disabling\n");
  804. err = -ENODEV;
  805. goto free_resources;
  806. }
  807. dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
  808. PAGE_SIZE, DMA_FROM_DEVICE);
  809. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  810. u32 *ptr = page_address(dest);
  811. if (ptr[i] != cmp_word) {
  812. dev_err(dma_chan->device->dev,
  813. "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
  814. i, ptr[i], cmp_word);
  815. err = -ENODEV;
  816. goto free_resources;
  817. }
  818. }
  819. free_resources:
  820. mv_xor_free_chan_resources(dma_chan);
  821. out:
  822. src_idx = MV_XOR_NUM_SRC_TEST;
  823. while (src_idx--)
  824. __free_page(xor_srcs[src_idx]);
  825. __free_page(dest);
  826. return err;
  827. }
  828. /* This driver does not implement any of the optional DMA operations. */
  829. static int
  830. mv_xor_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  831. unsigned long arg)
  832. {
  833. return -ENOSYS;
  834. }
  835. static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan)
  836. {
  837. struct dma_chan *chan, *_chan;
  838. struct device *dev = mv_chan->dmadev.dev;
  839. dma_async_device_unregister(&mv_chan->dmadev);
  840. dma_free_coherent(dev, MV_XOR_POOL_SIZE,
  841. mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
  842. list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels,
  843. device_node) {
  844. list_del(&chan->device_node);
  845. }
  846. free_irq(mv_chan->irq, mv_chan);
  847. return 0;
  848. }
  849. static struct mv_xor_chan *
  850. mv_xor_channel_add(struct mv_xor_device *xordev,
  851. struct platform_device *pdev,
  852. int idx, dma_cap_mask_t cap_mask, int irq)
  853. {
  854. int ret = 0;
  855. struct mv_xor_chan *mv_chan;
  856. struct dma_device *dma_dev;
  857. mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL);
  858. if (!mv_chan) {
  859. ret = -ENOMEM;
  860. goto err_free_dma;
  861. }
  862. mv_chan->idx = idx;
  863. mv_chan->irq = irq;
  864. dma_dev = &mv_chan->dmadev;
  865. /* allocate coherent memory for hardware descriptors
  866. * note: writecombine gives slightly better performance, but
  867. * requires that we explicitly flush the writes
  868. */
  869. mv_chan->dma_desc_pool_virt =
  870. dma_alloc_writecombine(&pdev->dev, MV_XOR_POOL_SIZE,
  871. &mv_chan->dma_desc_pool, GFP_KERNEL);
  872. if (!mv_chan->dma_desc_pool_virt)
  873. return ERR_PTR(-ENOMEM);
  874. /* discover transaction capabilites from the platform data */
  875. dma_dev->cap_mask = cap_mask;
  876. INIT_LIST_HEAD(&dma_dev->channels);
  877. /* set base routines */
  878. dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources;
  879. dma_dev->device_free_chan_resources = mv_xor_free_chan_resources;
  880. dma_dev->device_tx_status = mv_xor_status;
  881. dma_dev->device_issue_pending = mv_xor_issue_pending;
  882. dma_dev->device_control = mv_xor_control;
  883. dma_dev->dev = &pdev->dev;
  884. /* set prep routines based on capability */
  885. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
  886. dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy;
  887. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  888. dma_dev->max_xor = 8;
  889. dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor;
  890. }
  891. mv_chan->mmr_base = xordev->xor_base;
  892. if (!mv_chan->mmr_base) {
  893. ret = -ENOMEM;
  894. goto err_free_dma;
  895. }
  896. tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long)
  897. mv_chan);
  898. /* clear errors before enabling interrupts */
  899. mv_xor_device_clear_err_status(mv_chan);
  900. ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler,
  901. 0, dev_name(&pdev->dev), mv_chan);
  902. if (ret)
  903. goto err_free_dma;
  904. mv_chan_unmask_interrupts(mv_chan);
  905. mv_set_mode(mv_chan, DMA_MEMCPY);
  906. spin_lock_init(&mv_chan->lock);
  907. INIT_LIST_HEAD(&mv_chan->chain);
  908. INIT_LIST_HEAD(&mv_chan->completed_slots);
  909. INIT_LIST_HEAD(&mv_chan->all_slots);
  910. mv_chan->dmachan.device = dma_dev;
  911. dma_cookie_init(&mv_chan->dmachan);
  912. list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels);
  913. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  914. ret = mv_xor_memcpy_self_test(mv_chan);
  915. dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
  916. if (ret)
  917. goto err_free_irq;
  918. }
  919. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  920. ret = mv_xor_xor_self_test(mv_chan);
  921. dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
  922. if (ret)
  923. goto err_free_irq;
  924. }
  925. dev_info(&pdev->dev, "Marvell XOR: ( %s%s%s)\n",
  926. dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
  927. dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
  928. dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
  929. dma_async_device_register(dma_dev);
  930. return mv_chan;
  931. err_free_irq:
  932. free_irq(mv_chan->irq, mv_chan);
  933. err_free_dma:
  934. dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE,
  935. mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
  936. return ERR_PTR(ret);
  937. }
  938. static void
  939. mv_xor_conf_mbus_windows(struct mv_xor_device *xordev,
  940. const struct mbus_dram_target_info *dram)
  941. {
  942. void __iomem *base = xordev->xor_base;
  943. u32 win_enable = 0;
  944. int i;
  945. for (i = 0; i < 8; i++) {
  946. writel(0, base + WINDOW_BASE(i));
  947. writel(0, base + WINDOW_SIZE(i));
  948. if (i < 4)
  949. writel(0, base + WINDOW_REMAP_HIGH(i));
  950. }
  951. for (i = 0; i < dram->num_cs; i++) {
  952. const struct mbus_dram_window *cs = dram->cs + i;
  953. writel((cs->base & 0xffff0000) |
  954. (cs->mbus_attr << 8) |
  955. dram->mbus_dram_target_id, base + WINDOW_BASE(i));
  956. writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
  957. win_enable |= (1 << i);
  958. win_enable |= 3 << (16 + (2 * i));
  959. }
  960. writel(win_enable, base + WINDOW_BAR_ENABLE(0));
  961. writel(win_enable, base + WINDOW_BAR_ENABLE(1));
  962. writel(0, base + WINDOW_OVERRIDE_CTRL(0));
  963. writel(0, base + WINDOW_OVERRIDE_CTRL(1));
  964. }
  965. static int mv_xor_probe(struct platform_device *pdev)
  966. {
  967. const struct mbus_dram_target_info *dram;
  968. struct mv_xor_device *xordev;
  969. struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev);
  970. struct resource *res;
  971. int i, ret;
  972. dev_notice(&pdev->dev, "Marvell shared XOR driver\n");
  973. xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL);
  974. if (!xordev)
  975. return -ENOMEM;
  976. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  977. if (!res)
  978. return -ENODEV;
  979. xordev->xor_base = devm_ioremap(&pdev->dev, res->start,
  980. resource_size(res));
  981. if (!xordev->xor_base)
  982. return -EBUSY;
  983. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  984. if (!res)
  985. return -ENODEV;
  986. xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start,
  987. resource_size(res));
  988. if (!xordev->xor_high_base)
  989. return -EBUSY;
  990. platform_set_drvdata(pdev, xordev);
  991. /*
  992. * (Re-)program MBUS remapping windows if we are asked to.
  993. */
  994. dram = mv_mbus_dram_info();
  995. if (dram)
  996. mv_xor_conf_mbus_windows(xordev, dram);
  997. /* Not all platforms can gate the clock, so it is not
  998. * an error if the clock does not exists.
  999. */
  1000. xordev->clk = clk_get(&pdev->dev, NULL);
  1001. if (!IS_ERR(xordev->clk))
  1002. clk_prepare_enable(xordev->clk);
  1003. if (pdev->dev.of_node) {
  1004. struct device_node *np;
  1005. int i = 0;
  1006. for_each_child_of_node(pdev->dev.of_node, np) {
  1007. dma_cap_mask_t cap_mask;
  1008. int irq;
  1009. dma_cap_zero(cap_mask);
  1010. if (of_property_read_bool(np, "dmacap,memcpy"))
  1011. dma_cap_set(DMA_MEMCPY, cap_mask);
  1012. if (of_property_read_bool(np, "dmacap,xor"))
  1013. dma_cap_set(DMA_XOR, cap_mask);
  1014. if (of_property_read_bool(np, "dmacap,interrupt"))
  1015. dma_cap_set(DMA_INTERRUPT, cap_mask);
  1016. irq = irq_of_parse_and_map(np, 0);
  1017. if (!irq) {
  1018. ret = -ENODEV;
  1019. goto err_channel_add;
  1020. }
  1021. xordev->channels[i] =
  1022. mv_xor_channel_add(xordev, pdev, i,
  1023. cap_mask, irq);
  1024. if (IS_ERR(xordev->channels[i])) {
  1025. ret = PTR_ERR(xordev->channels[i]);
  1026. xordev->channels[i] = NULL;
  1027. irq_dispose_mapping(irq);
  1028. goto err_channel_add;
  1029. }
  1030. i++;
  1031. }
  1032. } else if (pdata && pdata->channels) {
  1033. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
  1034. struct mv_xor_channel_data *cd;
  1035. int irq;
  1036. cd = &pdata->channels[i];
  1037. if (!cd) {
  1038. ret = -ENODEV;
  1039. goto err_channel_add;
  1040. }
  1041. irq = platform_get_irq(pdev, i);
  1042. if (irq < 0) {
  1043. ret = irq;
  1044. goto err_channel_add;
  1045. }
  1046. xordev->channels[i] =
  1047. mv_xor_channel_add(xordev, pdev, i,
  1048. cd->cap_mask, irq);
  1049. if (IS_ERR(xordev->channels[i])) {
  1050. ret = PTR_ERR(xordev->channels[i]);
  1051. goto err_channel_add;
  1052. }
  1053. }
  1054. }
  1055. return 0;
  1056. err_channel_add:
  1057. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++)
  1058. if (xordev->channels[i]) {
  1059. mv_xor_channel_remove(xordev->channels[i]);
  1060. if (pdev->dev.of_node)
  1061. irq_dispose_mapping(xordev->channels[i]->irq);
  1062. }
  1063. if (!IS_ERR(xordev->clk)) {
  1064. clk_disable_unprepare(xordev->clk);
  1065. clk_put(xordev->clk);
  1066. }
  1067. return ret;
  1068. }
  1069. static int mv_xor_remove(struct platform_device *pdev)
  1070. {
  1071. struct mv_xor_device *xordev = platform_get_drvdata(pdev);
  1072. int i;
  1073. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
  1074. if (xordev->channels[i])
  1075. mv_xor_channel_remove(xordev->channels[i]);
  1076. }
  1077. if (!IS_ERR(xordev->clk)) {
  1078. clk_disable_unprepare(xordev->clk);
  1079. clk_put(xordev->clk);
  1080. }
  1081. return 0;
  1082. }
  1083. #ifdef CONFIG_OF
  1084. static struct of_device_id mv_xor_dt_ids[] = {
  1085. { .compatible = "marvell,orion-xor", },
  1086. {},
  1087. };
  1088. MODULE_DEVICE_TABLE(of, mv_xor_dt_ids);
  1089. #endif
  1090. static struct platform_driver mv_xor_driver = {
  1091. .probe = mv_xor_probe,
  1092. .remove = mv_xor_remove,
  1093. .driver = {
  1094. .owner = THIS_MODULE,
  1095. .name = MV_XOR_NAME,
  1096. .of_match_table = of_match_ptr(mv_xor_dt_ids),
  1097. },
  1098. };
  1099. static int __init mv_xor_init(void)
  1100. {
  1101. return platform_driver_register(&mv_xor_driver);
  1102. }
  1103. module_init(mv_xor_init);
  1104. /* it's currently unsafe to unload this module */
  1105. #if 0
  1106. static void __exit mv_xor_exit(void)
  1107. {
  1108. platform_driver_unregister(&mv_xor_driver);
  1109. return;
  1110. }
  1111. module_exit(mv_xor_exit);
  1112. #endif
  1113. MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
  1114. MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
  1115. MODULE_LICENSE("GPL");