mv_xor.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/delay.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/of_device.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/memory.h>
  23. #include <linux/clk.h>
  24. #include <linux/of.h>
  25. #include <linux/of_irq.h>
  26. #include <linux/irqdomain.h>
  27. #include <linux/cpumask.h>
  28. #include <linux/platform_data/dma-mv_xor.h>
  29. #include "dmaengine.h"
  30. #include "mv_xor.h"
  31. enum mv_xor_type {
  32. XOR_ORION,
  33. XOR_ARMADA_38X,
  34. XOR_ARMADA_37XX,
  35. };
  36. enum mv_xor_mode {
  37. XOR_MODE_IN_REG,
  38. XOR_MODE_IN_DESC,
  39. };
  40. static void mv_xor_issue_pending(struct dma_chan *chan);
  41. #define to_mv_xor_chan(chan) \
  42. container_of(chan, struct mv_xor_chan, dmachan)
  43. #define to_mv_xor_slot(tx) \
  44. container_of(tx, struct mv_xor_desc_slot, async_tx)
  45. #define mv_chan_to_devp(chan) \
  46. ((chan)->dmadev.dev)
  47. static void mv_desc_init(struct mv_xor_desc_slot *desc,
  48. dma_addr_t addr, u32 byte_count,
  49. enum dma_ctrl_flags flags)
  50. {
  51. struct mv_xor_desc *hw_desc = desc->hw_desc;
  52. hw_desc->status = XOR_DESC_DMA_OWNED;
  53. hw_desc->phy_next_desc = 0;
  54. /* Enable end-of-descriptor interrupts only for DMA_PREP_INTERRUPT */
  55. hw_desc->desc_command = (flags & DMA_PREP_INTERRUPT) ?
  56. XOR_DESC_EOD_INT_EN : 0;
  57. hw_desc->phy_dest_addr = addr;
  58. hw_desc->byte_count = byte_count;
  59. }
  60. static void mv_desc_set_mode(struct mv_xor_desc_slot *desc)
  61. {
  62. struct mv_xor_desc *hw_desc = desc->hw_desc;
  63. switch (desc->type) {
  64. case DMA_XOR:
  65. case DMA_INTERRUPT:
  66. hw_desc->desc_command |= XOR_DESC_OPERATION_XOR;
  67. break;
  68. case DMA_MEMCPY:
  69. hw_desc->desc_command |= XOR_DESC_OPERATION_MEMCPY;
  70. break;
  71. default:
  72. BUG();
  73. return;
  74. }
  75. }
  76. static void mv_desc_set_next_desc(struct mv_xor_desc_slot *desc,
  77. u32 next_desc_addr)
  78. {
  79. struct mv_xor_desc *hw_desc = desc->hw_desc;
  80. BUG_ON(hw_desc->phy_next_desc);
  81. hw_desc->phy_next_desc = next_desc_addr;
  82. }
  83. static void mv_desc_set_src_addr(struct mv_xor_desc_slot *desc,
  84. int index, dma_addr_t addr)
  85. {
  86. struct mv_xor_desc *hw_desc = desc->hw_desc;
  87. hw_desc->phy_src_addr[mv_phy_src_idx(index)] = addr;
  88. if (desc->type == DMA_XOR)
  89. hw_desc->desc_command |= (1 << index);
  90. }
  91. static u32 mv_chan_get_current_desc(struct mv_xor_chan *chan)
  92. {
  93. return readl_relaxed(XOR_CURR_DESC(chan));
  94. }
  95. static void mv_chan_set_next_descriptor(struct mv_xor_chan *chan,
  96. u32 next_desc_addr)
  97. {
  98. writel_relaxed(next_desc_addr, XOR_NEXT_DESC(chan));
  99. }
  100. static void mv_chan_unmask_interrupts(struct mv_xor_chan *chan)
  101. {
  102. u32 val = readl_relaxed(XOR_INTR_MASK(chan));
  103. val |= XOR_INTR_MASK_VALUE << (chan->idx * 16);
  104. writel_relaxed(val, XOR_INTR_MASK(chan));
  105. }
  106. static u32 mv_chan_get_intr_cause(struct mv_xor_chan *chan)
  107. {
  108. u32 intr_cause = readl_relaxed(XOR_INTR_CAUSE(chan));
  109. intr_cause = (intr_cause >> (chan->idx * 16)) & 0xFFFF;
  110. return intr_cause;
  111. }
  112. static void mv_chan_clear_eoc_cause(struct mv_xor_chan *chan)
  113. {
  114. u32 val;
  115. val = XOR_INT_END_OF_DESC | XOR_INT_END_OF_CHAIN | XOR_INT_STOPPED;
  116. val = ~(val << (chan->idx * 16));
  117. dev_dbg(mv_chan_to_devp(chan), "%s, val 0x%08x\n", __func__, val);
  118. writel_relaxed(val, XOR_INTR_CAUSE(chan));
  119. }
  120. static void mv_chan_clear_err_status(struct mv_xor_chan *chan)
  121. {
  122. u32 val = 0xFFFF0000 >> (chan->idx * 16);
  123. writel_relaxed(val, XOR_INTR_CAUSE(chan));
  124. }
  125. static void mv_chan_set_mode(struct mv_xor_chan *chan,
  126. u32 op_mode)
  127. {
  128. u32 config = readl_relaxed(XOR_CONFIG(chan));
  129. config &= ~0x7;
  130. config |= op_mode;
  131. #if defined(__BIG_ENDIAN)
  132. config |= XOR_DESCRIPTOR_SWAP;
  133. #else
  134. config &= ~XOR_DESCRIPTOR_SWAP;
  135. #endif
  136. writel_relaxed(config, XOR_CONFIG(chan));
  137. }
  138. static void mv_chan_activate(struct mv_xor_chan *chan)
  139. {
  140. dev_dbg(mv_chan_to_devp(chan), " activate chan.\n");
  141. /* writel ensures all descriptors are flushed before activation */
  142. writel(BIT(0), XOR_ACTIVATION(chan));
  143. }
  144. static char mv_chan_is_busy(struct mv_xor_chan *chan)
  145. {
  146. u32 state = readl_relaxed(XOR_ACTIVATION(chan));
  147. state = (state >> 4) & 0x3;
  148. return (state == 1) ? 1 : 0;
  149. }
  150. /*
  151. * mv_chan_start_new_chain - program the engine to operate on new
  152. * chain headed by sw_desc
  153. * Caller must hold &mv_chan->lock while calling this function
  154. */
  155. static void mv_chan_start_new_chain(struct mv_xor_chan *mv_chan,
  156. struct mv_xor_desc_slot *sw_desc)
  157. {
  158. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: sw_desc %p\n",
  159. __func__, __LINE__, sw_desc);
  160. /* set the hardware chain */
  161. mv_chan_set_next_descriptor(mv_chan, sw_desc->async_tx.phys);
  162. mv_chan->pending++;
  163. mv_xor_issue_pending(&mv_chan->dmachan);
  164. }
  165. static dma_cookie_t
  166. mv_desc_run_tx_complete_actions(struct mv_xor_desc_slot *desc,
  167. struct mv_xor_chan *mv_chan,
  168. dma_cookie_t cookie)
  169. {
  170. BUG_ON(desc->async_tx.cookie < 0);
  171. if (desc->async_tx.cookie > 0) {
  172. cookie = desc->async_tx.cookie;
  173. /* call the callback (must not sleep or submit new
  174. * operations to this channel)
  175. */
  176. if (desc->async_tx.callback)
  177. desc->async_tx.callback(
  178. desc->async_tx.callback_param);
  179. dma_descriptor_unmap(&desc->async_tx);
  180. }
  181. /* run dependent operations */
  182. dma_run_dependencies(&desc->async_tx);
  183. return cookie;
  184. }
  185. static int
  186. mv_chan_clean_completed_slots(struct mv_xor_chan *mv_chan)
  187. {
  188. struct mv_xor_desc_slot *iter, *_iter;
  189. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
  190. list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
  191. node) {
  192. if (async_tx_test_ack(&iter->async_tx))
  193. list_move_tail(&iter->node, &mv_chan->free_slots);
  194. }
  195. return 0;
  196. }
  197. static int
  198. mv_desc_clean_slot(struct mv_xor_desc_slot *desc,
  199. struct mv_xor_chan *mv_chan)
  200. {
  201. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d: desc %p flags %d\n",
  202. __func__, __LINE__, desc, desc->async_tx.flags);
  203. /* the client is allowed to attach dependent operations
  204. * until 'ack' is set
  205. */
  206. if (!async_tx_test_ack(&desc->async_tx))
  207. /* move this slot to the completed_slots */
  208. list_move_tail(&desc->node, &mv_chan->completed_slots);
  209. else
  210. list_move_tail(&desc->node, &mv_chan->free_slots);
  211. return 0;
  212. }
  213. /* This function must be called with the mv_xor_chan spinlock held */
  214. static void mv_chan_slot_cleanup(struct mv_xor_chan *mv_chan)
  215. {
  216. struct mv_xor_desc_slot *iter, *_iter;
  217. dma_cookie_t cookie = 0;
  218. int busy = mv_chan_is_busy(mv_chan);
  219. u32 current_desc = mv_chan_get_current_desc(mv_chan);
  220. int current_cleaned = 0;
  221. struct mv_xor_desc *hw_desc;
  222. dev_dbg(mv_chan_to_devp(mv_chan), "%s %d\n", __func__, __LINE__);
  223. dev_dbg(mv_chan_to_devp(mv_chan), "current_desc %x\n", current_desc);
  224. mv_chan_clean_completed_slots(mv_chan);
  225. /* free completed slots from the chain starting with
  226. * the oldest descriptor
  227. */
  228. list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
  229. node) {
  230. /* clean finished descriptors */
  231. hw_desc = iter->hw_desc;
  232. if (hw_desc->status & XOR_DESC_SUCCESS) {
  233. cookie = mv_desc_run_tx_complete_actions(iter, mv_chan,
  234. cookie);
  235. /* done processing desc, clean slot */
  236. mv_desc_clean_slot(iter, mv_chan);
  237. /* break if we did cleaned the current */
  238. if (iter->async_tx.phys == current_desc) {
  239. current_cleaned = 1;
  240. break;
  241. }
  242. } else {
  243. if (iter->async_tx.phys == current_desc) {
  244. current_cleaned = 0;
  245. break;
  246. }
  247. }
  248. }
  249. if ((busy == 0) && !list_empty(&mv_chan->chain)) {
  250. if (current_cleaned) {
  251. /*
  252. * current descriptor cleaned and removed, run
  253. * from list head
  254. */
  255. iter = list_entry(mv_chan->chain.next,
  256. struct mv_xor_desc_slot,
  257. node);
  258. mv_chan_start_new_chain(mv_chan, iter);
  259. } else {
  260. if (!list_is_last(&iter->node, &mv_chan->chain)) {
  261. /*
  262. * descriptors are still waiting after
  263. * current, trigger them
  264. */
  265. iter = list_entry(iter->node.next,
  266. struct mv_xor_desc_slot,
  267. node);
  268. mv_chan_start_new_chain(mv_chan, iter);
  269. } else {
  270. /*
  271. * some descriptors are still waiting
  272. * to be cleaned
  273. */
  274. tasklet_schedule(&mv_chan->irq_tasklet);
  275. }
  276. }
  277. }
  278. if (cookie > 0)
  279. mv_chan->dmachan.completed_cookie = cookie;
  280. }
  281. static void mv_xor_tasklet(unsigned long data)
  282. {
  283. struct mv_xor_chan *chan = (struct mv_xor_chan *) data;
  284. spin_lock_bh(&chan->lock);
  285. mv_chan_slot_cleanup(chan);
  286. spin_unlock_bh(&chan->lock);
  287. }
  288. static struct mv_xor_desc_slot *
  289. mv_chan_alloc_slot(struct mv_xor_chan *mv_chan)
  290. {
  291. struct mv_xor_desc_slot *iter;
  292. spin_lock_bh(&mv_chan->lock);
  293. if (!list_empty(&mv_chan->free_slots)) {
  294. iter = list_first_entry(&mv_chan->free_slots,
  295. struct mv_xor_desc_slot,
  296. node);
  297. list_move_tail(&iter->node, &mv_chan->allocated_slots);
  298. spin_unlock_bh(&mv_chan->lock);
  299. /* pre-ack descriptor */
  300. async_tx_ack(&iter->async_tx);
  301. iter->async_tx.cookie = -EBUSY;
  302. return iter;
  303. }
  304. spin_unlock_bh(&mv_chan->lock);
  305. /* try to free some slots if the allocation fails */
  306. tasklet_schedule(&mv_chan->irq_tasklet);
  307. return NULL;
  308. }
  309. /************************ DMA engine API functions ****************************/
  310. static dma_cookie_t
  311. mv_xor_tx_submit(struct dma_async_tx_descriptor *tx)
  312. {
  313. struct mv_xor_desc_slot *sw_desc = to_mv_xor_slot(tx);
  314. struct mv_xor_chan *mv_chan = to_mv_xor_chan(tx->chan);
  315. struct mv_xor_desc_slot *old_chain_tail;
  316. dma_cookie_t cookie;
  317. int new_hw_chain = 1;
  318. dev_dbg(mv_chan_to_devp(mv_chan),
  319. "%s sw_desc %p: async_tx %p\n",
  320. __func__, sw_desc, &sw_desc->async_tx);
  321. spin_lock_bh(&mv_chan->lock);
  322. cookie = dma_cookie_assign(tx);
  323. if (list_empty(&mv_chan->chain))
  324. list_move_tail(&sw_desc->node, &mv_chan->chain);
  325. else {
  326. new_hw_chain = 0;
  327. old_chain_tail = list_entry(mv_chan->chain.prev,
  328. struct mv_xor_desc_slot,
  329. node);
  330. list_move_tail(&sw_desc->node, &mv_chan->chain);
  331. dev_dbg(mv_chan_to_devp(mv_chan), "Append to last desc %pa\n",
  332. &old_chain_tail->async_tx.phys);
  333. /* fix up the hardware chain */
  334. mv_desc_set_next_desc(old_chain_tail, sw_desc->async_tx.phys);
  335. /* if the channel is not busy */
  336. if (!mv_chan_is_busy(mv_chan)) {
  337. u32 current_desc = mv_chan_get_current_desc(mv_chan);
  338. /*
  339. * and the curren desc is the end of the chain before
  340. * the append, then we need to start the channel
  341. */
  342. if (current_desc == old_chain_tail->async_tx.phys)
  343. new_hw_chain = 1;
  344. }
  345. }
  346. if (new_hw_chain)
  347. mv_chan_start_new_chain(mv_chan, sw_desc);
  348. spin_unlock_bh(&mv_chan->lock);
  349. return cookie;
  350. }
  351. /* returns the number of allocated descriptors */
  352. static int mv_xor_alloc_chan_resources(struct dma_chan *chan)
  353. {
  354. void *virt_desc;
  355. dma_addr_t dma_desc;
  356. int idx;
  357. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  358. struct mv_xor_desc_slot *slot = NULL;
  359. int num_descs_in_pool = MV_XOR_POOL_SIZE/MV_XOR_SLOT_SIZE;
  360. /* Allocate descriptor slots */
  361. idx = mv_chan->slots_allocated;
  362. while (idx < num_descs_in_pool) {
  363. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  364. if (!slot) {
  365. dev_info(mv_chan_to_devp(mv_chan),
  366. "channel only initialized %d descriptor slots",
  367. idx);
  368. break;
  369. }
  370. virt_desc = mv_chan->dma_desc_pool_virt;
  371. slot->hw_desc = virt_desc + idx * MV_XOR_SLOT_SIZE;
  372. dma_async_tx_descriptor_init(&slot->async_tx, chan);
  373. slot->async_tx.tx_submit = mv_xor_tx_submit;
  374. INIT_LIST_HEAD(&slot->node);
  375. dma_desc = mv_chan->dma_desc_pool;
  376. slot->async_tx.phys = dma_desc + idx * MV_XOR_SLOT_SIZE;
  377. slot->idx = idx++;
  378. spin_lock_bh(&mv_chan->lock);
  379. mv_chan->slots_allocated = idx;
  380. list_add_tail(&slot->node, &mv_chan->free_slots);
  381. spin_unlock_bh(&mv_chan->lock);
  382. }
  383. dev_dbg(mv_chan_to_devp(mv_chan),
  384. "allocated %d descriptor slots\n",
  385. mv_chan->slots_allocated);
  386. return mv_chan->slots_allocated ? : -ENOMEM;
  387. }
  388. static struct dma_async_tx_descriptor *
  389. mv_xor_prep_dma_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  390. unsigned int src_cnt, size_t len, unsigned long flags)
  391. {
  392. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  393. struct mv_xor_desc_slot *sw_desc;
  394. if (unlikely(len < MV_XOR_MIN_BYTE_COUNT))
  395. return NULL;
  396. BUG_ON(len > MV_XOR_MAX_BYTE_COUNT);
  397. dev_dbg(mv_chan_to_devp(mv_chan),
  398. "%s src_cnt: %d len: %zu dest %pad flags: %ld\n",
  399. __func__, src_cnt, len, &dest, flags);
  400. sw_desc = mv_chan_alloc_slot(mv_chan);
  401. if (sw_desc) {
  402. sw_desc->type = DMA_XOR;
  403. sw_desc->async_tx.flags = flags;
  404. mv_desc_init(sw_desc, dest, len, flags);
  405. if (mv_chan->op_in_desc == XOR_MODE_IN_DESC)
  406. mv_desc_set_mode(sw_desc);
  407. while (src_cnt--)
  408. mv_desc_set_src_addr(sw_desc, src_cnt, src[src_cnt]);
  409. }
  410. dev_dbg(mv_chan_to_devp(mv_chan),
  411. "%s sw_desc %p async_tx %p \n",
  412. __func__, sw_desc, &sw_desc->async_tx);
  413. return sw_desc ? &sw_desc->async_tx : NULL;
  414. }
  415. static struct dma_async_tx_descriptor *
  416. mv_xor_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  417. size_t len, unsigned long flags)
  418. {
  419. /*
  420. * A MEMCPY operation is identical to an XOR operation with only
  421. * a single source address.
  422. */
  423. return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags);
  424. }
  425. static struct dma_async_tx_descriptor *
  426. mv_xor_prep_dma_interrupt(struct dma_chan *chan, unsigned long flags)
  427. {
  428. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  429. dma_addr_t src, dest;
  430. size_t len;
  431. src = mv_chan->dummy_src_addr;
  432. dest = mv_chan->dummy_dst_addr;
  433. len = MV_XOR_MIN_BYTE_COUNT;
  434. /*
  435. * We implement the DMA_INTERRUPT operation as a minimum sized
  436. * XOR operation with a single dummy source address.
  437. */
  438. return mv_xor_prep_dma_xor(chan, dest, &src, 1, len, flags);
  439. }
  440. static void mv_xor_free_chan_resources(struct dma_chan *chan)
  441. {
  442. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  443. struct mv_xor_desc_slot *iter, *_iter;
  444. int in_use_descs = 0;
  445. spin_lock_bh(&mv_chan->lock);
  446. mv_chan_slot_cleanup(mv_chan);
  447. list_for_each_entry_safe(iter, _iter, &mv_chan->chain,
  448. node) {
  449. in_use_descs++;
  450. list_move_tail(&iter->node, &mv_chan->free_slots);
  451. }
  452. list_for_each_entry_safe(iter, _iter, &mv_chan->completed_slots,
  453. node) {
  454. in_use_descs++;
  455. list_move_tail(&iter->node, &mv_chan->free_slots);
  456. }
  457. list_for_each_entry_safe(iter, _iter, &mv_chan->allocated_slots,
  458. node) {
  459. in_use_descs++;
  460. list_move_tail(&iter->node, &mv_chan->free_slots);
  461. }
  462. list_for_each_entry_safe_reverse(
  463. iter, _iter, &mv_chan->free_slots, node) {
  464. list_del(&iter->node);
  465. kfree(iter);
  466. mv_chan->slots_allocated--;
  467. }
  468. dev_dbg(mv_chan_to_devp(mv_chan), "%s slots_allocated %d\n",
  469. __func__, mv_chan->slots_allocated);
  470. spin_unlock_bh(&mv_chan->lock);
  471. if (in_use_descs)
  472. dev_err(mv_chan_to_devp(mv_chan),
  473. "freeing %d in use descriptors!\n", in_use_descs);
  474. }
  475. /**
  476. * mv_xor_status - poll the status of an XOR transaction
  477. * @chan: XOR channel handle
  478. * @cookie: XOR transaction identifier
  479. * @txstate: XOR transactions state holder (or NULL)
  480. */
  481. static enum dma_status mv_xor_status(struct dma_chan *chan,
  482. dma_cookie_t cookie,
  483. struct dma_tx_state *txstate)
  484. {
  485. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  486. enum dma_status ret;
  487. ret = dma_cookie_status(chan, cookie, txstate);
  488. if (ret == DMA_COMPLETE)
  489. return ret;
  490. spin_lock_bh(&mv_chan->lock);
  491. mv_chan_slot_cleanup(mv_chan);
  492. spin_unlock_bh(&mv_chan->lock);
  493. return dma_cookie_status(chan, cookie, txstate);
  494. }
  495. static void mv_chan_dump_regs(struct mv_xor_chan *chan)
  496. {
  497. u32 val;
  498. val = readl_relaxed(XOR_CONFIG(chan));
  499. dev_err(mv_chan_to_devp(chan), "config 0x%08x\n", val);
  500. val = readl_relaxed(XOR_ACTIVATION(chan));
  501. dev_err(mv_chan_to_devp(chan), "activation 0x%08x\n", val);
  502. val = readl_relaxed(XOR_INTR_CAUSE(chan));
  503. dev_err(mv_chan_to_devp(chan), "intr cause 0x%08x\n", val);
  504. val = readl_relaxed(XOR_INTR_MASK(chan));
  505. dev_err(mv_chan_to_devp(chan), "intr mask 0x%08x\n", val);
  506. val = readl_relaxed(XOR_ERROR_CAUSE(chan));
  507. dev_err(mv_chan_to_devp(chan), "error cause 0x%08x\n", val);
  508. val = readl_relaxed(XOR_ERROR_ADDR(chan));
  509. dev_err(mv_chan_to_devp(chan), "error addr 0x%08x\n", val);
  510. }
  511. static void mv_chan_err_interrupt_handler(struct mv_xor_chan *chan,
  512. u32 intr_cause)
  513. {
  514. if (intr_cause & XOR_INT_ERR_DECODE) {
  515. dev_dbg(mv_chan_to_devp(chan), "ignoring address decode error\n");
  516. return;
  517. }
  518. dev_err(mv_chan_to_devp(chan), "error on chan %d. intr cause 0x%08x\n",
  519. chan->idx, intr_cause);
  520. mv_chan_dump_regs(chan);
  521. WARN_ON(1);
  522. }
  523. static irqreturn_t mv_xor_interrupt_handler(int irq, void *data)
  524. {
  525. struct mv_xor_chan *chan = data;
  526. u32 intr_cause = mv_chan_get_intr_cause(chan);
  527. dev_dbg(mv_chan_to_devp(chan), "intr cause %x\n", intr_cause);
  528. if (intr_cause & XOR_INTR_ERRORS)
  529. mv_chan_err_interrupt_handler(chan, intr_cause);
  530. tasklet_schedule(&chan->irq_tasklet);
  531. mv_chan_clear_eoc_cause(chan);
  532. return IRQ_HANDLED;
  533. }
  534. static void mv_xor_issue_pending(struct dma_chan *chan)
  535. {
  536. struct mv_xor_chan *mv_chan = to_mv_xor_chan(chan);
  537. if (mv_chan->pending >= MV_XOR_THRESHOLD) {
  538. mv_chan->pending = 0;
  539. mv_chan_activate(mv_chan);
  540. }
  541. }
  542. /*
  543. * Perform a transaction to verify the HW works.
  544. */
  545. static int mv_chan_memcpy_self_test(struct mv_xor_chan *mv_chan)
  546. {
  547. int i, ret;
  548. void *src, *dest;
  549. dma_addr_t src_dma, dest_dma;
  550. struct dma_chan *dma_chan;
  551. dma_cookie_t cookie;
  552. struct dma_async_tx_descriptor *tx;
  553. struct dmaengine_unmap_data *unmap;
  554. int err = 0;
  555. src = kmalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
  556. if (!src)
  557. return -ENOMEM;
  558. dest = kzalloc(sizeof(u8) * PAGE_SIZE, GFP_KERNEL);
  559. if (!dest) {
  560. kfree(src);
  561. return -ENOMEM;
  562. }
  563. /* Fill in src buffer */
  564. for (i = 0; i < PAGE_SIZE; i++)
  565. ((u8 *) src)[i] = (u8)i;
  566. dma_chan = &mv_chan->dmachan;
  567. if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
  568. err = -ENODEV;
  569. goto out;
  570. }
  571. unmap = dmaengine_get_unmap_data(dma_chan->device->dev, 2, GFP_KERNEL);
  572. if (!unmap) {
  573. err = -ENOMEM;
  574. goto free_resources;
  575. }
  576. src_dma = dma_map_page(dma_chan->device->dev, virt_to_page(src),
  577. (size_t)src & ~PAGE_MASK, PAGE_SIZE,
  578. DMA_TO_DEVICE);
  579. unmap->addr[0] = src_dma;
  580. ret = dma_mapping_error(dma_chan->device->dev, src_dma);
  581. if (ret) {
  582. err = -ENOMEM;
  583. goto free_resources;
  584. }
  585. unmap->to_cnt = 1;
  586. dest_dma = dma_map_page(dma_chan->device->dev, virt_to_page(dest),
  587. (size_t)dest & ~PAGE_MASK, PAGE_SIZE,
  588. DMA_FROM_DEVICE);
  589. unmap->addr[1] = dest_dma;
  590. ret = dma_mapping_error(dma_chan->device->dev, dest_dma);
  591. if (ret) {
  592. err = -ENOMEM;
  593. goto free_resources;
  594. }
  595. unmap->from_cnt = 1;
  596. unmap->len = PAGE_SIZE;
  597. tx = mv_xor_prep_dma_memcpy(dma_chan, dest_dma, src_dma,
  598. PAGE_SIZE, 0);
  599. if (!tx) {
  600. dev_err(dma_chan->device->dev,
  601. "Self-test cannot prepare operation, disabling\n");
  602. err = -ENODEV;
  603. goto free_resources;
  604. }
  605. cookie = mv_xor_tx_submit(tx);
  606. if (dma_submit_error(cookie)) {
  607. dev_err(dma_chan->device->dev,
  608. "Self-test submit error, disabling\n");
  609. err = -ENODEV;
  610. goto free_resources;
  611. }
  612. mv_xor_issue_pending(dma_chan);
  613. async_tx_ack(tx);
  614. msleep(1);
  615. if (mv_xor_status(dma_chan, cookie, NULL) !=
  616. DMA_COMPLETE) {
  617. dev_err(dma_chan->device->dev,
  618. "Self-test copy timed out, disabling\n");
  619. err = -ENODEV;
  620. goto free_resources;
  621. }
  622. dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
  623. PAGE_SIZE, DMA_FROM_DEVICE);
  624. if (memcmp(src, dest, PAGE_SIZE)) {
  625. dev_err(dma_chan->device->dev,
  626. "Self-test copy failed compare, disabling\n");
  627. err = -ENODEV;
  628. goto free_resources;
  629. }
  630. free_resources:
  631. dmaengine_unmap_put(unmap);
  632. mv_xor_free_chan_resources(dma_chan);
  633. out:
  634. kfree(src);
  635. kfree(dest);
  636. return err;
  637. }
  638. #define MV_XOR_NUM_SRC_TEST 4 /* must be <= 15 */
  639. static int
  640. mv_chan_xor_self_test(struct mv_xor_chan *mv_chan)
  641. {
  642. int i, src_idx, ret;
  643. struct page *dest;
  644. struct page *xor_srcs[MV_XOR_NUM_SRC_TEST];
  645. dma_addr_t dma_srcs[MV_XOR_NUM_SRC_TEST];
  646. dma_addr_t dest_dma;
  647. struct dma_async_tx_descriptor *tx;
  648. struct dmaengine_unmap_data *unmap;
  649. struct dma_chan *dma_chan;
  650. dma_cookie_t cookie;
  651. u8 cmp_byte = 0;
  652. u32 cmp_word;
  653. int err = 0;
  654. int src_count = MV_XOR_NUM_SRC_TEST;
  655. for (src_idx = 0; src_idx < src_count; src_idx++) {
  656. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  657. if (!xor_srcs[src_idx]) {
  658. while (src_idx--)
  659. __free_page(xor_srcs[src_idx]);
  660. return -ENOMEM;
  661. }
  662. }
  663. dest = alloc_page(GFP_KERNEL);
  664. if (!dest) {
  665. while (src_idx--)
  666. __free_page(xor_srcs[src_idx]);
  667. return -ENOMEM;
  668. }
  669. /* Fill in src buffers */
  670. for (src_idx = 0; src_idx < src_count; src_idx++) {
  671. u8 *ptr = page_address(xor_srcs[src_idx]);
  672. for (i = 0; i < PAGE_SIZE; i++)
  673. ptr[i] = (1 << src_idx);
  674. }
  675. for (src_idx = 0; src_idx < src_count; src_idx++)
  676. cmp_byte ^= (u8) (1 << src_idx);
  677. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  678. (cmp_byte << 8) | cmp_byte;
  679. memset(page_address(dest), 0, PAGE_SIZE);
  680. dma_chan = &mv_chan->dmachan;
  681. if (mv_xor_alloc_chan_resources(dma_chan) < 1) {
  682. err = -ENODEV;
  683. goto out;
  684. }
  685. unmap = dmaengine_get_unmap_data(dma_chan->device->dev, src_count + 1,
  686. GFP_KERNEL);
  687. if (!unmap) {
  688. err = -ENOMEM;
  689. goto free_resources;
  690. }
  691. /* test xor */
  692. for (i = 0; i < src_count; i++) {
  693. unmap->addr[i] = dma_map_page(dma_chan->device->dev, xor_srcs[i],
  694. 0, PAGE_SIZE, DMA_TO_DEVICE);
  695. dma_srcs[i] = unmap->addr[i];
  696. ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[i]);
  697. if (ret) {
  698. err = -ENOMEM;
  699. goto free_resources;
  700. }
  701. unmap->to_cnt++;
  702. }
  703. unmap->addr[src_count] = dma_map_page(dma_chan->device->dev, dest, 0, PAGE_SIZE,
  704. DMA_FROM_DEVICE);
  705. dest_dma = unmap->addr[src_count];
  706. ret = dma_mapping_error(dma_chan->device->dev, unmap->addr[src_count]);
  707. if (ret) {
  708. err = -ENOMEM;
  709. goto free_resources;
  710. }
  711. unmap->from_cnt = 1;
  712. unmap->len = PAGE_SIZE;
  713. tx = mv_xor_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  714. src_count, PAGE_SIZE, 0);
  715. if (!tx) {
  716. dev_err(dma_chan->device->dev,
  717. "Self-test cannot prepare operation, disabling\n");
  718. err = -ENODEV;
  719. goto free_resources;
  720. }
  721. cookie = mv_xor_tx_submit(tx);
  722. if (dma_submit_error(cookie)) {
  723. dev_err(dma_chan->device->dev,
  724. "Self-test submit error, disabling\n");
  725. err = -ENODEV;
  726. goto free_resources;
  727. }
  728. mv_xor_issue_pending(dma_chan);
  729. async_tx_ack(tx);
  730. msleep(8);
  731. if (mv_xor_status(dma_chan, cookie, NULL) !=
  732. DMA_COMPLETE) {
  733. dev_err(dma_chan->device->dev,
  734. "Self-test xor timed out, disabling\n");
  735. err = -ENODEV;
  736. goto free_resources;
  737. }
  738. dma_sync_single_for_cpu(dma_chan->device->dev, dest_dma,
  739. PAGE_SIZE, DMA_FROM_DEVICE);
  740. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  741. u32 *ptr = page_address(dest);
  742. if (ptr[i] != cmp_word) {
  743. dev_err(dma_chan->device->dev,
  744. "Self-test xor failed compare, disabling. index %d, data %x, expected %x\n",
  745. i, ptr[i], cmp_word);
  746. err = -ENODEV;
  747. goto free_resources;
  748. }
  749. }
  750. free_resources:
  751. dmaengine_unmap_put(unmap);
  752. mv_xor_free_chan_resources(dma_chan);
  753. out:
  754. src_idx = src_count;
  755. while (src_idx--)
  756. __free_page(xor_srcs[src_idx]);
  757. __free_page(dest);
  758. return err;
  759. }
  760. static int mv_xor_channel_remove(struct mv_xor_chan *mv_chan)
  761. {
  762. struct dma_chan *chan, *_chan;
  763. struct device *dev = mv_chan->dmadev.dev;
  764. dma_async_device_unregister(&mv_chan->dmadev);
  765. dma_free_coherent(dev, MV_XOR_POOL_SIZE,
  766. mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
  767. dma_unmap_single(dev, mv_chan->dummy_src_addr,
  768. MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE);
  769. dma_unmap_single(dev, mv_chan->dummy_dst_addr,
  770. MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE);
  771. list_for_each_entry_safe(chan, _chan, &mv_chan->dmadev.channels,
  772. device_node) {
  773. list_del(&chan->device_node);
  774. }
  775. free_irq(mv_chan->irq, mv_chan);
  776. return 0;
  777. }
  778. static struct mv_xor_chan *
  779. mv_xor_channel_add(struct mv_xor_device *xordev,
  780. struct platform_device *pdev,
  781. int idx, dma_cap_mask_t cap_mask, int irq)
  782. {
  783. int ret = 0;
  784. struct mv_xor_chan *mv_chan;
  785. struct dma_device *dma_dev;
  786. mv_chan = devm_kzalloc(&pdev->dev, sizeof(*mv_chan), GFP_KERNEL);
  787. if (!mv_chan)
  788. return ERR_PTR(-ENOMEM);
  789. mv_chan->idx = idx;
  790. mv_chan->irq = irq;
  791. if (xordev->xor_type == XOR_ORION)
  792. mv_chan->op_in_desc = XOR_MODE_IN_REG;
  793. else
  794. mv_chan->op_in_desc = XOR_MODE_IN_DESC;
  795. dma_dev = &mv_chan->dmadev;
  796. /*
  797. * These source and destination dummy buffers are used to implement
  798. * a DMA_INTERRUPT operation as a minimum-sized XOR operation.
  799. * Hence, we only need to map the buffers at initialization-time.
  800. */
  801. mv_chan->dummy_src_addr = dma_map_single(dma_dev->dev,
  802. mv_chan->dummy_src, MV_XOR_MIN_BYTE_COUNT, DMA_FROM_DEVICE);
  803. mv_chan->dummy_dst_addr = dma_map_single(dma_dev->dev,
  804. mv_chan->dummy_dst, MV_XOR_MIN_BYTE_COUNT, DMA_TO_DEVICE);
  805. /* allocate coherent memory for hardware descriptors
  806. * note: writecombine gives slightly better performance, but
  807. * requires that we explicitly flush the writes
  808. */
  809. mv_chan->dma_desc_pool_virt =
  810. dma_alloc_wc(&pdev->dev, MV_XOR_POOL_SIZE, &mv_chan->dma_desc_pool,
  811. GFP_KERNEL);
  812. if (!mv_chan->dma_desc_pool_virt)
  813. return ERR_PTR(-ENOMEM);
  814. /* discover transaction capabilites from the platform data */
  815. dma_dev->cap_mask = cap_mask;
  816. INIT_LIST_HEAD(&dma_dev->channels);
  817. /* set base routines */
  818. dma_dev->device_alloc_chan_resources = mv_xor_alloc_chan_resources;
  819. dma_dev->device_free_chan_resources = mv_xor_free_chan_resources;
  820. dma_dev->device_tx_status = mv_xor_status;
  821. dma_dev->device_issue_pending = mv_xor_issue_pending;
  822. dma_dev->dev = &pdev->dev;
  823. /* set prep routines based on capability */
  824. if (dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask))
  825. dma_dev->device_prep_dma_interrupt = mv_xor_prep_dma_interrupt;
  826. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask))
  827. dma_dev->device_prep_dma_memcpy = mv_xor_prep_dma_memcpy;
  828. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  829. dma_dev->max_xor = 8;
  830. dma_dev->device_prep_dma_xor = mv_xor_prep_dma_xor;
  831. }
  832. mv_chan->mmr_base = xordev->xor_base;
  833. mv_chan->mmr_high_base = xordev->xor_high_base;
  834. tasklet_init(&mv_chan->irq_tasklet, mv_xor_tasklet, (unsigned long)
  835. mv_chan);
  836. /* clear errors before enabling interrupts */
  837. mv_chan_clear_err_status(mv_chan);
  838. ret = request_irq(mv_chan->irq, mv_xor_interrupt_handler,
  839. 0, dev_name(&pdev->dev), mv_chan);
  840. if (ret)
  841. goto err_free_dma;
  842. mv_chan_unmask_interrupts(mv_chan);
  843. if (mv_chan->op_in_desc == XOR_MODE_IN_DESC)
  844. mv_chan_set_mode(mv_chan, XOR_OPERATION_MODE_IN_DESC);
  845. else
  846. mv_chan_set_mode(mv_chan, XOR_OPERATION_MODE_XOR);
  847. spin_lock_init(&mv_chan->lock);
  848. INIT_LIST_HEAD(&mv_chan->chain);
  849. INIT_LIST_HEAD(&mv_chan->completed_slots);
  850. INIT_LIST_HEAD(&mv_chan->free_slots);
  851. INIT_LIST_HEAD(&mv_chan->allocated_slots);
  852. mv_chan->dmachan.device = dma_dev;
  853. dma_cookie_init(&mv_chan->dmachan);
  854. list_add_tail(&mv_chan->dmachan.device_node, &dma_dev->channels);
  855. if (dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask)) {
  856. ret = mv_chan_memcpy_self_test(mv_chan);
  857. dev_dbg(&pdev->dev, "memcpy self test returned %d\n", ret);
  858. if (ret)
  859. goto err_free_irq;
  860. }
  861. if (dma_has_cap(DMA_XOR, dma_dev->cap_mask)) {
  862. ret = mv_chan_xor_self_test(mv_chan);
  863. dev_dbg(&pdev->dev, "xor self test returned %d\n", ret);
  864. if (ret)
  865. goto err_free_irq;
  866. }
  867. dev_info(&pdev->dev, "Marvell XOR (%s): ( %s%s%s)\n",
  868. mv_chan->op_in_desc ? "Descriptor Mode" : "Registers Mode",
  869. dma_has_cap(DMA_XOR, dma_dev->cap_mask) ? "xor " : "",
  870. dma_has_cap(DMA_MEMCPY, dma_dev->cap_mask) ? "cpy " : "",
  871. dma_has_cap(DMA_INTERRUPT, dma_dev->cap_mask) ? "intr " : "");
  872. dma_async_device_register(dma_dev);
  873. return mv_chan;
  874. err_free_irq:
  875. free_irq(mv_chan->irq, mv_chan);
  876. err_free_dma:
  877. dma_free_coherent(&pdev->dev, MV_XOR_POOL_SIZE,
  878. mv_chan->dma_desc_pool_virt, mv_chan->dma_desc_pool);
  879. return ERR_PTR(ret);
  880. }
  881. static void
  882. mv_xor_conf_mbus_windows(struct mv_xor_device *xordev,
  883. const struct mbus_dram_target_info *dram)
  884. {
  885. void __iomem *base = xordev->xor_high_base;
  886. u32 win_enable = 0;
  887. int i;
  888. for (i = 0; i < 8; i++) {
  889. writel(0, base + WINDOW_BASE(i));
  890. writel(0, base + WINDOW_SIZE(i));
  891. if (i < 4)
  892. writel(0, base + WINDOW_REMAP_HIGH(i));
  893. }
  894. for (i = 0; i < dram->num_cs; i++) {
  895. const struct mbus_dram_window *cs = dram->cs + i;
  896. writel((cs->base & 0xffff0000) |
  897. (cs->mbus_attr << 8) |
  898. dram->mbus_dram_target_id, base + WINDOW_BASE(i));
  899. writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
  900. win_enable |= (1 << i);
  901. win_enable |= 3 << (16 + (2 * i));
  902. }
  903. writel(win_enable, base + WINDOW_BAR_ENABLE(0));
  904. writel(win_enable, base + WINDOW_BAR_ENABLE(1));
  905. writel(0, base + WINDOW_OVERRIDE_CTRL(0));
  906. writel(0, base + WINDOW_OVERRIDE_CTRL(1));
  907. }
  908. static void
  909. mv_xor_conf_mbus_windows_a3700(struct mv_xor_device *xordev)
  910. {
  911. void __iomem *base = xordev->xor_high_base;
  912. u32 win_enable = 0;
  913. int i;
  914. for (i = 0; i < 8; i++) {
  915. writel(0, base + WINDOW_BASE(i));
  916. writel(0, base + WINDOW_SIZE(i));
  917. if (i < 4)
  918. writel(0, base + WINDOW_REMAP_HIGH(i));
  919. }
  920. /*
  921. * For Armada3700 open default 4GB Mbus window. The dram
  922. * related configuration are done at AXIS level.
  923. */
  924. writel(0xffff0000, base + WINDOW_SIZE(0));
  925. win_enable |= 1;
  926. win_enable |= 3 << 16;
  927. writel(win_enable, base + WINDOW_BAR_ENABLE(0));
  928. writel(win_enable, base + WINDOW_BAR_ENABLE(1));
  929. writel(0, base + WINDOW_OVERRIDE_CTRL(0));
  930. writel(0, base + WINDOW_OVERRIDE_CTRL(1));
  931. }
  932. /*
  933. * Since this XOR driver is basically used only for RAID5, we don't
  934. * need to care about synchronizing ->suspend with DMA activity,
  935. * because the DMA engine will naturally be quiet due to the block
  936. * devices being suspended.
  937. */
  938. static int mv_xor_suspend(struct platform_device *pdev, pm_message_t state)
  939. {
  940. struct mv_xor_device *xordev = platform_get_drvdata(pdev);
  941. int i;
  942. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
  943. struct mv_xor_chan *mv_chan = xordev->channels[i];
  944. if (!mv_chan)
  945. continue;
  946. mv_chan->saved_config_reg =
  947. readl_relaxed(XOR_CONFIG(mv_chan));
  948. mv_chan->saved_int_mask_reg =
  949. readl_relaxed(XOR_INTR_MASK(mv_chan));
  950. }
  951. return 0;
  952. }
  953. static int mv_xor_resume(struct platform_device *dev)
  954. {
  955. struct mv_xor_device *xordev = platform_get_drvdata(dev);
  956. const struct mbus_dram_target_info *dram;
  957. int i;
  958. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++) {
  959. struct mv_xor_chan *mv_chan = xordev->channels[i];
  960. if (!mv_chan)
  961. continue;
  962. writel_relaxed(mv_chan->saved_config_reg,
  963. XOR_CONFIG(mv_chan));
  964. writel_relaxed(mv_chan->saved_int_mask_reg,
  965. XOR_INTR_MASK(mv_chan));
  966. }
  967. if (xordev->xor_type == XOR_ARMADA_37XX) {
  968. mv_xor_conf_mbus_windows_a3700(xordev);
  969. return 0;
  970. }
  971. dram = mv_mbus_dram_info();
  972. if (dram)
  973. mv_xor_conf_mbus_windows(xordev, dram);
  974. return 0;
  975. }
  976. static const struct of_device_id mv_xor_dt_ids[] = {
  977. { .compatible = "marvell,orion-xor", .data = (void *)XOR_ORION },
  978. { .compatible = "marvell,armada-380-xor", .data = (void *)XOR_ARMADA_38X },
  979. { .compatible = "marvell,armada-3700-xor", .data = (void *)XOR_ARMADA_37XX },
  980. {},
  981. };
  982. static unsigned int mv_xor_engine_count;
  983. static int mv_xor_probe(struct platform_device *pdev)
  984. {
  985. const struct mbus_dram_target_info *dram;
  986. struct mv_xor_device *xordev;
  987. struct mv_xor_platform_data *pdata = dev_get_platdata(&pdev->dev);
  988. struct resource *res;
  989. unsigned int max_engines, max_channels;
  990. int i, ret;
  991. dev_notice(&pdev->dev, "Marvell shared XOR driver\n");
  992. xordev = devm_kzalloc(&pdev->dev, sizeof(*xordev), GFP_KERNEL);
  993. if (!xordev)
  994. return -ENOMEM;
  995. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  996. if (!res)
  997. return -ENODEV;
  998. xordev->xor_base = devm_ioremap(&pdev->dev, res->start,
  999. resource_size(res));
  1000. if (!xordev->xor_base)
  1001. return -EBUSY;
  1002. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1003. if (!res)
  1004. return -ENODEV;
  1005. xordev->xor_high_base = devm_ioremap(&pdev->dev, res->start,
  1006. resource_size(res));
  1007. if (!xordev->xor_high_base)
  1008. return -EBUSY;
  1009. platform_set_drvdata(pdev, xordev);
  1010. /*
  1011. * We need to know which type of XOR device we use before
  1012. * setting up. In non-dt case it can only be the legacy one.
  1013. */
  1014. xordev->xor_type = XOR_ORION;
  1015. if (pdev->dev.of_node) {
  1016. const struct of_device_id *of_id =
  1017. of_match_device(mv_xor_dt_ids,
  1018. &pdev->dev);
  1019. xordev->xor_type = (uintptr_t)of_id->data;
  1020. }
  1021. /*
  1022. * (Re-)program MBUS remapping windows if we are asked to.
  1023. */
  1024. if (xordev->xor_type == XOR_ARMADA_37XX) {
  1025. mv_xor_conf_mbus_windows_a3700(xordev);
  1026. } else {
  1027. dram = mv_mbus_dram_info();
  1028. if (dram)
  1029. mv_xor_conf_mbus_windows(xordev, dram);
  1030. }
  1031. /* Not all platforms can gate the clock, so it is not
  1032. * an error if the clock does not exists.
  1033. */
  1034. xordev->clk = clk_get(&pdev->dev, NULL);
  1035. if (!IS_ERR(xordev->clk))
  1036. clk_prepare_enable(xordev->clk);
  1037. /*
  1038. * We don't want to have more than one channel per CPU in
  1039. * order for async_tx to perform well. So we limit the number
  1040. * of engines and channels so that we take into account this
  1041. * constraint. Note that we also want to use channels from
  1042. * separate engines when possible. For dual-CPU Armada 3700
  1043. * SoC with single XOR engine allow using its both channels.
  1044. */
  1045. max_engines = num_present_cpus();
  1046. if (xordev->xor_type == XOR_ARMADA_37XX)
  1047. max_channels = num_present_cpus();
  1048. else
  1049. max_channels = min_t(unsigned int,
  1050. MV_XOR_MAX_CHANNELS,
  1051. DIV_ROUND_UP(num_present_cpus(), 2));
  1052. if (mv_xor_engine_count >= max_engines)
  1053. return 0;
  1054. if (pdev->dev.of_node) {
  1055. struct device_node *np;
  1056. int i = 0;
  1057. for_each_child_of_node(pdev->dev.of_node, np) {
  1058. struct mv_xor_chan *chan;
  1059. dma_cap_mask_t cap_mask;
  1060. int irq;
  1061. if (i >= max_channels)
  1062. continue;
  1063. dma_cap_zero(cap_mask);
  1064. dma_cap_set(DMA_MEMCPY, cap_mask);
  1065. dma_cap_set(DMA_XOR, cap_mask);
  1066. dma_cap_set(DMA_INTERRUPT, cap_mask);
  1067. irq = irq_of_parse_and_map(np, 0);
  1068. if (!irq) {
  1069. ret = -ENODEV;
  1070. goto err_channel_add;
  1071. }
  1072. chan = mv_xor_channel_add(xordev, pdev, i,
  1073. cap_mask, irq);
  1074. if (IS_ERR(chan)) {
  1075. ret = PTR_ERR(chan);
  1076. irq_dispose_mapping(irq);
  1077. goto err_channel_add;
  1078. }
  1079. xordev->channels[i] = chan;
  1080. i++;
  1081. }
  1082. } else if (pdata && pdata->channels) {
  1083. for (i = 0; i < max_channels; i++) {
  1084. struct mv_xor_channel_data *cd;
  1085. struct mv_xor_chan *chan;
  1086. int irq;
  1087. cd = &pdata->channels[i];
  1088. if (!cd) {
  1089. ret = -ENODEV;
  1090. goto err_channel_add;
  1091. }
  1092. irq = platform_get_irq(pdev, i);
  1093. if (irq < 0) {
  1094. ret = irq;
  1095. goto err_channel_add;
  1096. }
  1097. chan = mv_xor_channel_add(xordev, pdev, i,
  1098. cd->cap_mask, irq);
  1099. if (IS_ERR(chan)) {
  1100. ret = PTR_ERR(chan);
  1101. goto err_channel_add;
  1102. }
  1103. xordev->channels[i] = chan;
  1104. }
  1105. }
  1106. return 0;
  1107. err_channel_add:
  1108. for (i = 0; i < MV_XOR_MAX_CHANNELS; i++)
  1109. if (xordev->channels[i]) {
  1110. mv_xor_channel_remove(xordev->channels[i]);
  1111. if (pdev->dev.of_node)
  1112. irq_dispose_mapping(xordev->channels[i]->irq);
  1113. }
  1114. if (!IS_ERR(xordev->clk)) {
  1115. clk_disable_unprepare(xordev->clk);
  1116. clk_put(xordev->clk);
  1117. }
  1118. return ret;
  1119. }
  1120. static struct platform_driver mv_xor_driver = {
  1121. .probe = mv_xor_probe,
  1122. .suspend = mv_xor_suspend,
  1123. .resume = mv_xor_resume,
  1124. .driver = {
  1125. .name = MV_XOR_NAME,
  1126. .of_match_table = of_match_ptr(mv_xor_dt_ids),
  1127. },
  1128. };
  1129. static int __init mv_xor_init(void)
  1130. {
  1131. return platform_driver_register(&mv_xor_driver);
  1132. }
  1133. device_initcall(mv_xor_init);
  1134. /*
  1135. MODULE_AUTHOR("Saeed Bishara <saeed@marvell.com>");
  1136. MODULE_DESCRIPTION("DMA engine driver for Marvell's XOR engine");
  1137. MODULE_LICENSE("GPL");
  1138. */