mv_xor.c 35 KB

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