mv_xor.c 36 KB

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