mv_xor.c 33 KB

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