davinci_cpdma.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. /*
  2. * Texas Instruments CPDMA Driver
  3. *
  4. * Copyright (C) 2010 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/device.h>
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/err.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/io.h>
  23. #include <linux/delay.h>
  24. #include <linux/genalloc.h>
  25. #include "davinci_cpdma.h"
  26. /* DMA Registers */
  27. #define CPDMA_TXIDVER 0x00
  28. #define CPDMA_TXCONTROL 0x04
  29. #define CPDMA_TXTEARDOWN 0x08
  30. #define CPDMA_RXIDVER 0x10
  31. #define CPDMA_RXCONTROL 0x14
  32. #define CPDMA_SOFTRESET 0x1c
  33. #define CPDMA_RXTEARDOWN 0x18
  34. #define CPDMA_TX_PRI0_RATE 0x30
  35. #define CPDMA_TXINTSTATRAW 0x80
  36. #define CPDMA_TXINTSTATMASKED 0x84
  37. #define CPDMA_TXINTMASKSET 0x88
  38. #define CPDMA_TXINTMASKCLEAR 0x8c
  39. #define CPDMA_MACINVECTOR 0x90
  40. #define CPDMA_MACEOIVECTOR 0x94
  41. #define CPDMA_RXINTSTATRAW 0xa0
  42. #define CPDMA_RXINTSTATMASKED 0xa4
  43. #define CPDMA_RXINTMASKSET 0xa8
  44. #define CPDMA_RXINTMASKCLEAR 0xac
  45. #define CPDMA_DMAINTSTATRAW 0xb0
  46. #define CPDMA_DMAINTSTATMASKED 0xb4
  47. #define CPDMA_DMAINTMASKSET 0xb8
  48. #define CPDMA_DMAINTMASKCLEAR 0xbc
  49. #define CPDMA_DMAINT_HOSTERR BIT(1)
  50. /* the following exist only if has_ext_regs is set */
  51. #define CPDMA_DMACONTROL 0x20
  52. #define CPDMA_DMASTATUS 0x24
  53. #define CPDMA_RXBUFFOFS 0x28
  54. #define CPDMA_EM_CONTROL 0x2c
  55. /* Descriptor mode bits */
  56. #define CPDMA_DESC_SOP BIT(31)
  57. #define CPDMA_DESC_EOP BIT(30)
  58. #define CPDMA_DESC_OWNER BIT(29)
  59. #define CPDMA_DESC_EOQ BIT(28)
  60. #define CPDMA_DESC_TD_COMPLETE BIT(27)
  61. #define CPDMA_DESC_PASS_CRC BIT(26)
  62. #define CPDMA_DESC_TO_PORT_EN BIT(20)
  63. #define CPDMA_TO_PORT_SHIFT 16
  64. #define CPDMA_DESC_PORT_MASK (BIT(18) | BIT(17) | BIT(16))
  65. #define CPDMA_DESC_CRC_LEN 4
  66. #define CPDMA_TEARDOWN_VALUE 0xfffffffc
  67. #define CPDMA_MAX_RLIM_CNT 16384
  68. struct cpdma_desc {
  69. /* hardware fields */
  70. u32 hw_next;
  71. u32 hw_buffer;
  72. u32 hw_len;
  73. u32 hw_mode;
  74. /* software fields */
  75. void *sw_token;
  76. u32 sw_buffer;
  77. u32 sw_len;
  78. };
  79. struct cpdma_desc_pool {
  80. phys_addr_t phys;
  81. dma_addr_t hw_addr;
  82. void __iomem *iomap; /* ioremap map */
  83. void *cpumap; /* dma_alloc map */
  84. int desc_size, mem_size;
  85. int num_desc;
  86. struct device *dev;
  87. struct gen_pool *gen_pool;
  88. };
  89. enum cpdma_state {
  90. CPDMA_STATE_IDLE,
  91. CPDMA_STATE_ACTIVE,
  92. CPDMA_STATE_TEARDOWN,
  93. };
  94. struct cpdma_ctlr {
  95. enum cpdma_state state;
  96. struct cpdma_params params;
  97. struct device *dev;
  98. struct cpdma_desc_pool *pool;
  99. spinlock_t lock;
  100. struct cpdma_chan *channels[2 * CPDMA_MAX_CHANNELS];
  101. int chan_num;
  102. int num_rx_desc; /* RX descriptors number */
  103. int num_tx_desc; /* TX descriptors number */
  104. };
  105. struct cpdma_chan {
  106. struct cpdma_desc __iomem *head, *tail;
  107. void __iomem *hdp, *cp, *rxfree;
  108. enum cpdma_state state;
  109. struct cpdma_ctlr *ctlr;
  110. int chan_num;
  111. spinlock_t lock;
  112. int count;
  113. u32 desc_num;
  114. u32 mask;
  115. cpdma_handler_fn handler;
  116. enum dma_data_direction dir;
  117. struct cpdma_chan_stats stats;
  118. /* offsets into dmaregs */
  119. int int_set, int_clear, td;
  120. int weight;
  121. u32 rate_factor;
  122. u32 rate;
  123. };
  124. struct cpdma_control_info {
  125. u32 reg;
  126. u32 shift, mask;
  127. int access;
  128. #define ACCESS_RO BIT(0)
  129. #define ACCESS_WO BIT(1)
  130. #define ACCESS_RW (ACCESS_RO | ACCESS_WO)
  131. };
  132. static struct cpdma_control_info controls[] = {
  133. [CPDMA_TX_RLIM] = {CPDMA_DMACONTROL, 8, 0xffff, ACCESS_RW},
  134. [CPDMA_CMD_IDLE] = {CPDMA_DMACONTROL, 3, 1, ACCESS_WO},
  135. [CPDMA_COPY_ERROR_FRAMES] = {CPDMA_DMACONTROL, 4, 1, ACCESS_RW},
  136. [CPDMA_RX_OFF_LEN_UPDATE] = {CPDMA_DMACONTROL, 2, 1, ACCESS_RW},
  137. [CPDMA_RX_OWNERSHIP_FLIP] = {CPDMA_DMACONTROL, 1, 1, ACCESS_RW},
  138. [CPDMA_TX_PRIO_FIXED] = {CPDMA_DMACONTROL, 0, 1, ACCESS_RW},
  139. [CPDMA_STAT_IDLE] = {CPDMA_DMASTATUS, 31, 1, ACCESS_RO},
  140. [CPDMA_STAT_TX_ERR_CODE] = {CPDMA_DMASTATUS, 20, 0xf, ACCESS_RW},
  141. [CPDMA_STAT_TX_ERR_CHAN] = {CPDMA_DMASTATUS, 16, 0x7, ACCESS_RW},
  142. [CPDMA_STAT_RX_ERR_CODE] = {CPDMA_DMASTATUS, 12, 0xf, ACCESS_RW},
  143. [CPDMA_STAT_RX_ERR_CHAN] = {CPDMA_DMASTATUS, 8, 0x7, ACCESS_RW},
  144. [CPDMA_RX_BUFFER_OFFSET] = {CPDMA_RXBUFFOFS, 0, 0xffff, ACCESS_RW},
  145. };
  146. #define tx_chan_num(chan) (chan)
  147. #define rx_chan_num(chan) ((chan) + CPDMA_MAX_CHANNELS)
  148. #define is_rx_chan(chan) ((chan)->chan_num >= CPDMA_MAX_CHANNELS)
  149. #define is_tx_chan(chan) (!is_rx_chan(chan))
  150. #define __chan_linear(chan_num) ((chan_num) & (CPDMA_MAX_CHANNELS - 1))
  151. #define chan_linear(chan) __chan_linear((chan)->chan_num)
  152. /* The following make access to common cpdma_ctlr params more readable */
  153. #define dmaregs params.dmaregs
  154. #define num_chan params.num_chan
  155. /* various accessors */
  156. #define dma_reg_read(ctlr, ofs) readl((ctlr)->dmaregs + (ofs))
  157. #define chan_read(chan, fld) readl((chan)->fld)
  158. #define desc_read(desc, fld) readl(&(desc)->fld)
  159. #define dma_reg_write(ctlr, ofs, v) writel(v, (ctlr)->dmaregs + (ofs))
  160. #define chan_write(chan, fld, v) writel(v, (chan)->fld)
  161. #define desc_write(desc, fld, v) writel((u32)(v), &(desc)->fld)
  162. #define cpdma_desc_to_port(chan, mode, directed) \
  163. do { \
  164. if (!is_rx_chan(chan) && ((directed == 1) || \
  165. (directed == 2))) \
  166. mode |= (CPDMA_DESC_TO_PORT_EN | \
  167. (directed << CPDMA_TO_PORT_SHIFT)); \
  168. } while (0)
  169. static void cpdma_desc_pool_destroy(struct cpdma_ctlr *ctlr)
  170. {
  171. struct cpdma_desc_pool *pool = ctlr->pool;
  172. if (!pool)
  173. return;
  174. WARN(gen_pool_size(pool->gen_pool) != gen_pool_avail(pool->gen_pool),
  175. "cpdma_desc_pool size %d != avail %d",
  176. gen_pool_size(pool->gen_pool),
  177. gen_pool_avail(pool->gen_pool));
  178. if (pool->cpumap)
  179. dma_free_coherent(ctlr->dev, pool->mem_size, pool->cpumap,
  180. pool->phys);
  181. }
  182. /*
  183. * Utility constructs for a cpdma descriptor pool. Some devices (e.g. davinci
  184. * emac) have dedicated on-chip memory for these descriptors. Some other
  185. * devices (e.g. cpsw switches) use plain old memory. Descriptor pools
  186. * abstract out these details
  187. */
  188. int cpdma_desc_pool_create(struct cpdma_ctlr *ctlr)
  189. {
  190. struct cpdma_params *cpdma_params = &ctlr->params;
  191. struct cpdma_desc_pool *pool;
  192. int ret = -ENOMEM;
  193. pool = devm_kzalloc(ctlr->dev, sizeof(*pool), GFP_KERNEL);
  194. if (!pool)
  195. goto gen_pool_create_fail;
  196. ctlr->pool = pool;
  197. pool->mem_size = cpdma_params->desc_mem_size;
  198. pool->desc_size = ALIGN(sizeof(struct cpdma_desc),
  199. cpdma_params->desc_align);
  200. pool->num_desc = pool->mem_size / pool->desc_size;
  201. if (cpdma_params->descs_pool_size) {
  202. /* recalculate memory size required cpdma descriptor pool
  203. * basing on number of descriptors specified by user and
  204. * if memory size > CPPI internal RAM size (desc_mem_size)
  205. * then switch to use DDR
  206. */
  207. pool->num_desc = cpdma_params->descs_pool_size;
  208. pool->mem_size = pool->desc_size * pool->num_desc;
  209. if (pool->mem_size > cpdma_params->desc_mem_size)
  210. cpdma_params->desc_mem_phys = 0;
  211. }
  212. pool->gen_pool = devm_gen_pool_create(ctlr->dev, ilog2(pool->desc_size),
  213. -1, "cpdma");
  214. if (IS_ERR(pool->gen_pool)) {
  215. ret = PTR_ERR(pool->gen_pool);
  216. dev_err(ctlr->dev, "pool create failed %d\n", ret);
  217. goto gen_pool_create_fail;
  218. }
  219. if (cpdma_params->desc_mem_phys) {
  220. pool->phys = cpdma_params->desc_mem_phys;
  221. pool->iomap = devm_ioremap(ctlr->dev, pool->phys,
  222. pool->mem_size);
  223. pool->hw_addr = cpdma_params->desc_hw_addr;
  224. } else {
  225. pool->cpumap = dma_alloc_coherent(ctlr->dev, pool->mem_size,
  226. &pool->hw_addr, GFP_KERNEL);
  227. pool->iomap = (void __iomem __force *)pool->cpumap;
  228. pool->phys = pool->hw_addr; /* assumes no IOMMU, don't use this value */
  229. }
  230. if (!pool->iomap)
  231. goto gen_pool_create_fail;
  232. ret = gen_pool_add_virt(pool->gen_pool, (unsigned long)pool->iomap,
  233. pool->phys, pool->mem_size, -1);
  234. if (ret < 0) {
  235. dev_err(ctlr->dev, "pool add failed %d\n", ret);
  236. goto gen_pool_add_virt_fail;
  237. }
  238. return 0;
  239. gen_pool_add_virt_fail:
  240. cpdma_desc_pool_destroy(ctlr);
  241. gen_pool_create_fail:
  242. ctlr->pool = NULL;
  243. return ret;
  244. }
  245. static inline dma_addr_t desc_phys(struct cpdma_desc_pool *pool,
  246. struct cpdma_desc __iomem *desc)
  247. {
  248. if (!desc)
  249. return 0;
  250. return pool->hw_addr + (__force long)desc - (__force long)pool->iomap;
  251. }
  252. static inline struct cpdma_desc __iomem *
  253. desc_from_phys(struct cpdma_desc_pool *pool, dma_addr_t dma)
  254. {
  255. return dma ? pool->iomap + dma - pool->hw_addr : NULL;
  256. }
  257. static struct cpdma_desc __iomem *
  258. cpdma_desc_alloc(struct cpdma_desc_pool *pool)
  259. {
  260. return (struct cpdma_desc __iomem *)
  261. gen_pool_alloc(pool->gen_pool, pool->desc_size);
  262. }
  263. static void cpdma_desc_free(struct cpdma_desc_pool *pool,
  264. struct cpdma_desc __iomem *desc, int num_desc)
  265. {
  266. gen_pool_free(pool->gen_pool, (unsigned long)desc, pool->desc_size);
  267. }
  268. static int _cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
  269. {
  270. struct cpdma_control_info *info = &controls[control];
  271. u32 val;
  272. if (!ctlr->params.has_ext_regs)
  273. return -ENOTSUPP;
  274. if (ctlr->state != CPDMA_STATE_ACTIVE)
  275. return -EINVAL;
  276. if (control < 0 || control >= ARRAY_SIZE(controls))
  277. return -ENOENT;
  278. if ((info->access & ACCESS_WO) != ACCESS_WO)
  279. return -EPERM;
  280. val = dma_reg_read(ctlr, info->reg);
  281. val &= ~(info->mask << info->shift);
  282. val |= (value & info->mask) << info->shift;
  283. dma_reg_write(ctlr, info->reg, val);
  284. return 0;
  285. }
  286. static int _cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
  287. {
  288. struct cpdma_control_info *info = &controls[control];
  289. int ret;
  290. if (!ctlr->params.has_ext_regs)
  291. return -ENOTSUPP;
  292. if (ctlr->state != CPDMA_STATE_ACTIVE)
  293. return -EINVAL;
  294. if (control < 0 || control >= ARRAY_SIZE(controls))
  295. return -ENOENT;
  296. if ((info->access & ACCESS_RO) != ACCESS_RO)
  297. return -EPERM;
  298. ret = (dma_reg_read(ctlr, info->reg) >> info->shift) & info->mask;
  299. return ret;
  300. }
  301. /* cpdma_chan_set_chan_shaper - set shaper for a channel
  302. * Has to be called under ctlr lock
  303. */
  304. static int cpdma_chan_set_chan_shaper(struct cpdma_chan *chan)
  305. {
  306. struct cpdma_ctlr *ctlr = chan->ctlr;
  307. u32 rate_reg;
  308. u32 rmask;
  309. int ret;
  310. if (!chan->rate)
  311. return 0;
  312. rate_reg = CPDMA_TX_PRI0_RATE + 4 * chan->chan_num;
  313. dma_reg_write(ctlr, rate_reg, chan->rate_factor);
  314. rmask = _cpdma_control_get(ctlr, CPDMA_TX_RLIM);
  315. rmask |= chan->mask;
  316. ret = _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask);
  317. return ret;
  318. }
  319. static int cpdma_chan_on(struct cpdma_chan *chan)
  320. {
  321. struct cpdma_ctlr *ctlr = chan->ctlr;
  322. struct cpdma_desc_pool *pool = ctlr->pool;
  323. unsigned long flags;
  324. spin_lock_irqsave(&chan->lock, flags);
  325. if (chan->state != CPDMA_STATE_IDLE) {
  326. spin_unlock_irqrestore(&chan->lock, flags);
  327. return -EBUSY;
  328. }
  329. if (ctlr->state != CPDMA_STATE_ACTIVE) {
  330. spin_unlock_irqrestore(&chan->lock, flags);
  331. return -EINVAL;
  332. }
  333. dma_reg_write(ctlr, chan->int_set, chan->mask);
  334. chan->state = CPDMA_STATE_ACTIVE;
  335. if (chan->head) {
  336. chan_write(chan, hdp, desc_phys(pool, chan->head));
  337. if (chan->rxfree)
  338. chan_write(chan, rxfree, chan->count);
  339. }
  340. spin_unlock_irqrestore(&chan->lock, flags);
  341. return 0;
  342. }
  343. /* cpdma_chan_fit_rate - set rate for a channel and check if it's possible.
  344. * rmask - mask of rate limited channels
  345. * Returns min rate in Kb/s
  346. */
  347. static int cpdma_chan_fit_rate(struct cpdma_chan *ch, u32 rate,
  348. u32 *rmask, int *prio_mode)
  349. {
  350. struct cpdma_ctlr *ctlr = ch->ctlr;
  351. struct cpdma_chan *chan;
  352. u32 old_rate = ch->rate;
  353. u32 new_rmask = 0;
  354. int rlim = 1;
  355. int i;
  356. *prio_mode = 0;
  357. for (i = tx_chan_num(0); i < tx_chan_num(CPDMA_MAX_CHANNELS); i++) {
  358. chan = ctlr->channels[i];
  359. if (!chan) {
  360. rlim = 0;
  361. continue;
  362. }
  363. if (chan == ch)
  364. chan->rate = rate;
  365. if (chan->rate) {
  366. if (rlim) {
  367. new_rmask |= chan->mask;
  368. } else {
  369. ch->rate = old_rate;
  370. dev_err(ctlr->dev, "Prev channel of %dch is not rate limited\n",
  371. chan->chan_num);
  372. return -EINVAL;
  373. }
  374. } else {
  375. *prio_mode = 1;
  376. rlim = 0;
  377. }
  378. }
  379. *rmask = new_rmask;
  380. return 0;
  381. }
  382. static u32 cpdma_chan_set_factors(struct cpdma_ctlr *ctlr,
  383. struct cpdma_chan *ch)
  384. {
  385. u32 delta = UINT_MAX, prev_delta = UINT_MAX, best_delta = UINT_MAX;
  386. u32 best_send_cnt = 0, best_idle_cnt = 0;
  387. u32 new_rate, best_rate = 0, rate_reg;
  388. u64 send_cnt, idle_cnt;
  389. u32 min_send_cnt, freq;
  390. u64 divident, divisor;
  391. if (!ch->rate) {
  392. ch->rate_factor = 0;
  393. goto set_factor;
  394. }
  395. freq = ctlr->params.bus_freq_mhz * 1000 * 32;
  396. if (!freq) {
  397. dev_err(ctlr->dev, "The bus frequency is not set\n");
  398. return -EINVAL;
  399. }
  400. min_send_cnt = freq - ch->rate;
  401. send_cnt = DIV_ROUND_UP(min_send_cnt, ch->rate);
  402. while (send_cnt <= CPDMA_MAX_RLIM_CNT) {
  403. divident = ch->rate * send_cnt;
  404. divisor = min_send_cnt;
  405. idle_cnt = DIV_ROUND_CLOSEST_ULL(divident, divisor);
  406. divident = freq * idle_cnt;
  407. divisor = idle_cnt + send_cnt;
  408. new_rate = DIV_ROUND_CLOSEST_ULL(divident, divisor);
  409. delta = new_rate >= ch->rate ? new_rate - ch->rate : delta;
  410. if (delta < best_delta) {
  411. best_delta = delta;
  412. best_send_cnt = send_cnt;
  413. best_idle_cnt = idle_cnt;
  414. best_rate = new_rate;
  415. if (!delta)
  416. break;
  417. }
  418. if (prev_delta >= delta) {
  419. prev_delta = delta;
  420. send_cnt++;
  421. continue;
  422. }
  423. idle_cnt++;
  424. divident = freq * idle_cnt;
  425. send_cnt = DIV_ROUND_CLOSEST_ULL(divident, ch->rate);
  426. send_cnt -= idle_cnt;
  427. prev_delta = UINT_MAX;
  428. }
  429. ch->rate = best_rate;
  430. ch->rate_factor = best_send_cnt | (best_idle_cnt << 16);
  431. set_factor:
  432. rate_reg = CPDMA_TX_PRI0_RATE + 4 * ch->chan_num;
  433. dma_reg_write(ctlr, rate_reg, ch->rate_factor);
  434. return 0;
  435. }
  436. struct cpdma_ctlr *cpdma_ctlr_create(struct cpdma_params *params)
  437. {
  438. struct cpdma_ctlr *ctlr;
  439. ctlr = devm_kzalloc(params->dev, sizeof(*ctlr), GFP_KERNEL);
  440. if (!ctlr)
  441. return NULL;
  442. ctlr->state = CPDMA_STATE_IDLE;
  443. ctlr->params = *params;
  444. ctlr->dev = params->dev;
  445. ctlr->chan_num = 0;
  446. spin_lock_init(&ctlr->lock);
  447. if (cpdma_desc_pool_create(ctlr))
  448. return NULL;
  449. /* split pool equally between RX/TX by default */
  450. ctlr->num_tx_desc = ctlr->pool->num_desc / 2;
  451. ctlr->num_rx_desc = ctlr->pool->num_desc - ctlr->num_tx_desc;
  452. if (WARN_ON(ctlr->num_chan > CPDMA_MAX_CHANNELS))
  453. ctlr->num_chan = CPDMA_MAX_CHANNELS;
  454. return ctlr;
  455. }
  456. EXPORT_SYMBOL_GPL(cpdma_ctlr_create);
  457. int cpdma_ctlr_start(struct cpdma_ctlr *ctlr)
  458. {
  459. struct cpdma_chan *chan;
  460. unsigned long flags;
  461. int i, prio_mode;
  462. spin_lock_irqsave(&ctlr->lock, flags);
  463. if (ctlr->state != CPDMA_STATE_IDLE) {
  464. spin_unlock_irqrestore(&ctlr->lock, flags);
  465. return -EBUSY;
  466. }
  467. if (ctlr->params.has_soft_reset) {
  468. unsigned timeout = 10 * 100;
  469. dma_reg_write(ctlr, CPDMA_SOFTRESET, 1);
  470. while (timeout) {
  471. if (dma_reg_read(ctlr, CPDMA_SOFTRESET) == 0)
  472. break;
  473. udelay(10);
  474. timeout--;
  475. }
  476. WARN_ON(!timeout);
  477. }
  478. for (i = 0; i < ctlr->num_chan; i++) {
  479. writel(0, ctlr->params.txhdp + 4 * i);
  480. writel(0, ctlr->params.rxhdp + 4 * i);
  481. writel(0, ctlr->params.txcp + 4 * i);
  482. writel(0, ctlr->params.rxcp + 4 * i);
  483. }
  484. dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
  485. dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
  486. dma_reg_write(ctlr, CPDMA_TXCONTROL, 1);
  487. dma_reg_write(ctlr, CPDMA_RXCONTROL, 1);
  488. ctlr->state = CPDMA_STATE_ACTIVE;
  489. prio_mode = 0;
  490. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  491. chan = ctlr->channels[i];
  492. if (chan) {
  493. cpdma_chan_set_chan_shaper(chan);
  494. cpdma_chan_on(chan);
  495. /* off prio mode if all tx channels are rate limited */
  496. if (is_tx_chan(chan) && !chan->rate)
  497. prio_mode = 1;
  498. }
  499. }
  500. _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
  501. _cpdma_control_set(ctlr, CPDMA_RX_BUFFER_OFFSET, 0);
  502. spin_unlock_irqrestore(&ctlr->lock, flags);
  503. return 0;
  504. }
  505. EXPORT_SYMBOL_GPL(cpdma_ctlr_start);
  506. int cpdma_ctlr_stop(struct cpdma_ctlr *ctlr)
  507. {
  508. unsigned long flags;
  509. int i;
  510. spin_lock_irqsave(&ctlr->lock, flags);
  511. if (ctlr->state != CPDMA_STATE_ACTIVE) {
  512. spin_unlock_irqrestore(&ctlr->lock, flags);
  513. return -EINVAL;
  514. }
  515. ctlr->state = CPDMA_STATE_TEARDOWN;
  516. spin_unlock_irqrestore(&ctlr->lock, flags);
  517. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  518. if (ctlr->channels[i])
  519. cpdma_chan_stop(ctlr->channels[i]);
  520. }
  521. spin_lock_irqsave(&ctlr->lock, flags);
  522. dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
  523. dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
  524. dma_reg_write(ctlr, CPDMA_TXCONTROL, 0);
  525. dma_reg_write(ctlr, CPDMA_RXCONTROL, 0);
  526. ctlr->state = CPDMA_STATE_IDLE;
  527. spin_unlock_irqrestore(&ctlr->lock, flags);
  528. return 0;
  529. }
  530. EXPORT_SYMBOL_GPL(cpdma_ctlr_stop);
  531. int cpdma_ctlr_destroy(struct cpdma_ctlr *ctlr)
  532. {
  533. int ret = 0, i;
  534. if (!ctlr)
  535. return -EINVAL;
  536. if (ctlr->state != CPDMA_STATE_IDLE)
  537. cpdma_ctlr_stop(ctlr);
  538. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++)
  539. cpdma_chan_destroy(ctlr->channels[i]);
  540. cpdma_desc_pool_destroy(ctlr);
  541. return ret;
  542. }
  543. EXPORT_SYMBOL_GPL(cpdma_ctlr_destroy);
  544. int cpdma_ctlr_int_ctrl(struct cpdma_ctlr *ctlr, bool enable)
  545. {
  546. unsigned long flags;
  547. int i, reg;
  548. spin_lock_irqsave(&ctlr->lock, flags);
  549. if (ctlr->state != CPDMA_STATE_ACTIVE) {
  550. spin_unlock_irqrestore(&ctlr->lock, flags);
  551. return -EINVAL;
  552. }
  553. reg = enable ? CPDMA_DMAINTMASKSET : CPDMA_DMAINTMASKCLEAR;
  554. dma_reg_write(ctlr, reg, CPDMA_DMAINT_HOSTERR);
  555. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  556. if (ctlr->channels[i])
  557. cpdma_chan_int_ctrl(ctlr->channels[i], enable);
  558. }
  559. spin_unlock_irqrestore(&ctlr->lock, flags);
  560. return 0;
  561. }
  562. EXPORT_SYMBOL_GPL(cpdma_ctlr_int_ctrl);
  563. void cpdma_ctlr_eoi(struct cpdma_ctlr *ctlr, u32 value)
  564. {
  565. dma_reg_write(ctlr, CPDMA_MACEOIVECTOR, value);
  566. }
  567. EXPORT_SYMBOL_GPL(cpdma_ctlr_eoi);
  568. u32 cpdma_ctrl_rxchs_state(struct cpdma_ctlr *ctlr)
  569. {
  570. return dma_reg_read(ctlr, CPDMA_RXINTSTATMASKED);
  571. }
  572. EXPORT_SYMBOL_GPL(cpdma_ctrl_rxchs_state);
  573. u32 cpdma_ctrl_txchs_state(struct cpdma_ctlr *ctlr)
  574. {
  575. return dma_reg_read(ctlr, CPDMA_TXINTSTATMASKED);
  576. }
  577. EXPORT_SYMBOL_GPL(cpdma_ctrl_txchs_state);
  578. static void cpdma_chan_set_descs(struct cpdma_ctlr *ctlr,
  579. int rx, int desc_num,
  580. int per_ch_desc)
  581. {
  582. struct cpdma_chan *chan, *most_chan = NULL;
  583. int desc_cnt = desc_num;
  584. int most_dnum = 0;
  585. int min, max, i;
  586. if (!desc_num)
  587. return;
  588. if (rx) {
  589. min = rx_chan_num(0);
  590. max = rx_chan_num(CPDMA_MAX_CHANNELS);
  591. } else {
  592. min = tx_chan_num(0);
  593. max = tx_chan_num(CPDMA_MAX_CHANNELS);
  594. }
  595. for (i = min; i < max; i++) {
  596. chan = ctlr->channels[i];
  597. if (!chan)
  598. continue;
  599. if (chan->weight)
  600. chan->desc_num = (chan->weight * desc_num) / 100;
  601. else
  602. chan->desc_num = per_ch_desc;
  603. desc_cnt -= chan->desc_num;
  604. if (most_dnum < chan->desc_num) {
  605. most_dnum = chan->desc_num;
  606. most_chan = chan;
  607. }
  608. }
  609. /* use remains */
  610. if (most_chan)
  611. most_chan->desc_num += desc_cnt;
  612. }
  613. /**
  614. * cpdma_chan_split_pool - Splits ctrl pool between all channels.
  615. * Has to be called under ctlr lock
  616. */
  617. int cpdma_chan_split_pool(struct cpdma_ctlr *ctlr)
  618. {
  619. int tx_per_ch_desc = 0, rx_per_ch_desc = 0;
  620. int free_rx_num = 0, free_tx_num = 0;
  621. int rx_weight = 0, tx_weight = 0;
  622. int tx_desc_num, rx_desc_num;
  623. struct cpdma_chan *chan;
  624. int i;
  625. if (!ctlr->chan_num)
  626. return 0;
  627. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  628. chan = ctlr->channels[i];
  629. if (!chan)
  630. continue;
  631. if (is_rx_chan(chan)) {
  632. if (!chan->weight)
  633. free_rx_num++;
  634. rx_weight += chan->weight;
  635. } else {
  636. if (!chan->weight)
  637. free_tx_num++;
  638. tx_weight += chan->weight;
  639. }
  640. }
  641. if (rx_weight > 100 || tx_weight > 100)
  642. return -EINVAL;
  643. tx_desc_num = ctlr->num_tx_desc;
  644. rx_desc_num = ctlr->num_rx_desc;
  645. if (free_tx_num) {
  646. tx_per_ch_desc = tx_desc_num - (tx_weight * tx_desc_num) / 100;
  647. tx_per_ch_desc /= free_tx_num;
  648. }
  649. if (free_rx_num) {
  650. rx_per_ch_desc = rx_desc_num - (rx_weight * rx_desc_num) / 100;
  651. rx_per_ch_desc /= free_rx_num;
  652. }
  653. cpdma_chan_set_descs(ctlr, 0, tx_desc_num, tx_per_ch_desc);
  654. cpdma_chan_set_descs(ctlr, 1, rx_desc_num, rx_per_ch_desc);
  655. return 0;
  656. }
  657. EXPORT_SYMBOL_GPL(cpdma_chan_split_pool);
  658. /* cpdma_chan_set_weight - set weight of a channel in percentage.
  659. * Tx and Rx channels have separate weights. That is 100% for RX
  660. * and 100% for Tx. The weight is used to split cpdma resources
  661. * in correct proportion required by the channels, including number
  662. * of descriptors. The channel rate is not enough to know the
  663. * weight of a channel as the maximum rate of an interface is needed.
  664. * If weight = 0, then channel uses rest of descriptors leaved by
  665. * weighted channels.
  666. */
  667. int cpdma_chan_set_weight(struct cpdma_chan *ch, int weight)
  668. {
  669. struct cpdma_ctlr *ctlr = ch->ctlr;
  670. unsigned long flags, ch_flags;
  671. int ret;
  672. spin_lock_irqsave(&ctlr->lock, flags);
  673. spin_lock_irqsave(&ch->lock, ch_flags);
  674. if (ch->weight == weight) {
  675. spin_unlock_irqrestore(&ch->lock, ch_flags);
  676. spin_unlock_irqrestore(&ctlr->lock, flags);
  677. return 0;
  678. }
  679. ch->weight = weight;
  680. spin_unlock_irqrestore(&ch->lock, ch_flags);
  681. /* re-split pool using new channel weight */
  682. ret = cpdma_chan_split_pool(ctlr);
  683. spin_unlock_irqrestore(&ctlr->lock, flags);
  684. return ret;
  685. }
  686. EXPORT_SYMBOL_GPL(cpdma_chan_set_weight);
  687. /* cpdma_chan_get_min_rate - get minimum allowed rate for channel
  688. * Should be called before cpdma_chan_set_rate.
  689. * Returns min rate in Kb/s
  690. */
  691. u32 cpdma_chan_get_min_rate(struct cpdma_ctlr *ctlr)
  692. {
  693. unsigned int divident, divisor;
  694. divident = ctlr->params.bus_freq_mhz * 32 * 1000;
  695. divisor = 1 + CPDMA_MAX_RLIM_CNT;
  696. return DIV_ROUND_UP(divident, divisor);
  697. }
  698. EXPORT_SYMBOL_GPL(cpdma_chan_get_min_rate);
  699. /* cpdma_chan_set_rate - limits bandwidth for transmit channel.
  700. * The bandwidth * limited channels have to be in order beginning from lowest.
  701. * ch - transmit channel the bandwidth is configured for
  702. * rate - bandwidth in Kb/s, if 0 - then off shaper
  703. */
  704. int cpdma_chan_set_rate(struct cpdma_chan *ch, u32 rate)
  705. {
  706. unsigned long flags, ch_flags;
  707. struct cpdma_ctlr *ctlr;
  708. int ret, prio_mode;
  709. u32 rmask;
  710. if (!ch || !is_tx_chan(ch))
  711. return -EINVAL;
  712. if (ch->rate == rate)
  713. return rate;
  714. ctlr = ch->ctlr;
  715. spin_lock_irqsave(&ctlr->lock, flags);
  716. spin_lock_irqsave(&ch->lock, ch_flags);
  717. ret = cpdma_chan_fit_rate(ch, rate, &rmask, &prio_mode);
  718. if (ret)
  719. goto err;
  720. ret = cpdma_chan_set_factors(ctlr, ch);
  721. if (ret)
  722. goto err;
  723. spin_unlock_irqrestore(&ch->lock, ch_flags);
  724. /* on shapers */
  725. _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask);
  726. _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
  727. spin_unlock_irqrestore(&ctlr->lock, flags);
  728. return ret;
  729. err:
  730. spin_unlock_irqrestore(&ch->lock, ch_flags);
  731. spin_unlock_irqrestore(&ctlr->lock, flags);
  732. return ret;
  733. }
  734. EXPORT_SYMBOL_GPL(cpdma_chan_set_rate);
  735. u32 cpdma_chan_get_rate(struct cpdma_chan *ch)
  736. {
  737. unsigned long flags;
  738. u32 rate;
  739. spin_lock_irqsave(&ch->lock, flags);
  740. rate = ch->rate;
  741. spin_unlock_irqrestore(&ch->lock, flags);
  742. return rate;
  743. }
  744. EXPORT_SYMBOL_GPL(cpdma_chan_get_rate);
  745. struct cpdma_chan *cpdma_chan_create(struct cpdma_ctlr *ctlr, int chan_num,
  746. cpdma_handler_fn handler, int rx_type)
  747. {
  748. int offset = chan_num * 4;
  749. struct cpdma_chan *chan;
  750. unsigned long flags;
  751. chan_num = rx_type ? rx_chan_num(chan_num) : tx_chan_num(chan_num);
  752. if (__chan_linear(chan_num) >= ctlr->num_chan)
  753. return NULL;
  754. chan = devm_kzalloc(ctlr->dev, sizeof(*chan), GFP_KERNEL);
  755. if (!chan)
  756. return ERR_PTR(-ENOMEM);
  757. spin_lock_irqsave(&ctlr->lock, flags);
  758. if (ctlr->channels[chan_num]) {
  759. spin_unlock_irqrestore(&ctlr->lock, flags);
  760. devm_kfree(ctlr->dev, chan);
  761. return ERR_PTR(-EBUSY);
  762. }
  763. chan->ctlr = ctlr;
  764. chan->state = CPDMA_STATE_IDLE;
  765. chan->chan_num = chan_num;
  766. chan->handler = handler;
  767. chan->rate = 0;
  768. chan->weight = 0;
  769. if (is_rx_chan(chan)) {
  770. chan->hdp = ctlr->params.rxhdp + offset;
  771. chan->cp = ctlr->params.rxcp + offset;
  772. chan->rxfree = ctlr->params.rxfree + offset;
  773. chan->int_set = CPDMA_RXINTMASKSET;
  774. chan->int_clear = CPDMA_RXINTMASKCLEAR;
  775. chan->td = CPDMA_RXTEARDOWN;
  776. chan->dir = DMA_FROM_DEVICE;
  777. } else {
  778. chan->hdp = ctlr->params.txhdp + offset;
  779. chan->cp = ctlr->params.txcp + offset;
  780. chan->int_set = CPDMA_TXINTMASKSET;
  781. chan->int_clear = CPDMA_TXINTMASKCLEAR;
  782. chan->td = CPDMA_TXTEARDOWN;
  783. chan->dir = DMA_TO_DEVICE;
  784. }
  785. chan->mask = BIT(chan_linear(chan));
  786. spin_lock_init(&chan->lock);
  787. ctlr->channels[chan_num] = chan;
  788. ctlr->chan_num++;
  789. cpdma_chan_split_pool(ctlr);
  790. spin_unlock_irqrestore(&ctlr->lock, flags);
  791. return chan;
  792. }
  793. EXPORT_SYMBOL_GPL(cpdma_chan_create);
  794. int cpdma_chan_get_rx_buf_num(struct cpdma_chan *chan)
  795. {
  796. unsigned long flags;
  797. int desc_num;
  798. spin_lock_irqsave(&chan->lock, flags);
  799. desc_num = chan->desc_num;
  800. spin_unlock_irqrestore(&chan->lock, flags);
  801. return desc_num;
  802. }
  803. EXPORT_SYMBOL_GPL(cpdma_chan_get_rx_buf_num);
  804. int cpdma_chan_destroy(struct cpdma_chan *chan)
  805. {
  806. struct cpdma_ctlr *ctlr;
  807. unsigned long flags;
  808. if (!chan)
  809. return -EINVAL;
  810. ctlr = chan->ctlr;
  811. spin_lock_irqsave(&ctlr->lock, flags);
  812. if (chan->state != CPDMA_STATE_IDLE)
  813. cpdma_chan_stop(chan);
  814. ctlr->channels[chan->chan_num] = NULL;
  815. ctlr->chan_num--;
  816. devm_kfree(ctlr->dev, chan);
  817. cpdma_chan_split_pool(ctlr);
  818. spin_unlock_irqrestore(&ctlr->lock, flags);
  819. return 0;
  820. }
  821. EXPORT_SYMBOL_GPL(cpdma_chan_destroy);
  822. int cpdma_chan_get_stats(struct cpdma_chan *chan,
  823. struct cpdma_chan_stats *stats)
  824. {
  825. unsigned long flags;
  826. if (!chan)
  827. return -EINVAL;
  828. spin_lock_irqsave(&chan->lock, flags);
  829. memcpy(stats, &chan->stats, sizeof(*stats));
  830. spin_unlock_irqrestore(&chan->lock, flags);
  831. return 0;
  832. }
  833. EXPORT_SYMBOL_GPL(cpdma_chan_get_stats);
  834. static void __cpdma_chan_submit(struct cpdma_chan *chan,
  835. struct cpdma_desc __iomem *desc)
  836. {
  837. struct cpdma_ctlr *ctlr = chan->ctlr;
  838. struct cpdma_desc __iomem *prev = chan->tail;
  839. struct cpdma_desc_pool *pool = ctlr->pool;
  840. dma_addr_t desc_dma;
  841. u32 mode;
  842. desc_dma = desc_phys(pool, desc);
  843. /* simple case - idle channel */
  844. if (!chan->head) {
  845. chan->stats.head_enqueue++;
  846. chan->head = desc;
  847. chan->tail = desc;
  848. if (chan->state == CPDMA_STATE_ACTIVE)
  849. chan_write(chan, hdp, desc_dma);
  850. return;
  851. }
  852. /* first chain the descriptor at the tail of the list */
  853. desc_write(prev, hw_next, desc_dma);
  854. chan->tail = desc;
  855. chan->stats.tail_enqueue++;
  856. /* next check if EOQ has been triggered already */
  857. mode = desc_read(prev, hw_mode);
  858. if (((mode & (CPDMA_DESC_EOQ | CPDMA_DESC_OWNER)) == CPDMA_DESC_EOQ) &&
  859. (chan->state == CPDMA_STATE_ACTIVE)) {
  860. desc_write(prev, hw_mode, mode & ~CPDMA_DESC_EOQ);
  861. chan_write(chan, hdp, desc_dma);
  862. chan->stats.misqueued++;
  863. }
  864. }
  865. int cpdma_chan_submit(struct cpdma_chan *chan, void *token, void *data,
  866. int len, int directed)
  867. {
  868. struct cpdma_ctlr *ctlr = chan->ctlr;
  869. struct cpdma_desc __iomem *desc;
  870. dma_addr_t buffer;
  871. unsigned long flags;
  872. u32 mode;
  873. int ret = 0;
  874. spin_lock_irqsave(&chan->lock, flags);
  875. if (chan->state == CPDMA_STATE_TEARDOWN) {
  876. ret = -EINVAL;
  877. goto unlock_ret;
  878. }
  879. if (chan->count >= chan->desc_num) {
  880. chan->stats.desc_alloc_fail++;
  881. ret = -ENOMEM;
  882. goto unlock_ret;
  883. }
  884. desc = cpdma_desc_alloc(ctlr->pool);
  885. if (!desc) {
  886. chan->stats.desc_alloc_fail++;
  887. ret = -ENOMEM;
  888. goto unlock_ret;
  889. }
  890. if (len < ctlr->params.min_packet_size) {
  891. len = ctlr->params.min_packet_size;
  892. chan->stats.runt_transmit_buff++;
  893. }
  894. buffer = dma_map_single(ctlr->dev, data, len, chan->dir);
  895. ret = dma_mapping_error(ctlr->dev, buffer);
  896. if (ret) {
  897. cpdma_desc_free(ctlr->pool, desc, 1);
  898. ret = -EINVAL;
  899. goto unlock_ret;
  900. }
  901. mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
  902. cpdma_desc_to_port(chan, mode, directed);
  903. /* Relaxed IO accessors can be used here as there is read barrier
  904. * at the end of write sequence.
  905. */
  906. writel_relaxed(0, &desc->hw_next);
  907. writel_relaxed(buffer, &desc->hw_buffer);
  908. writel_relaxed(len, &desc->hw_len);
  909. writel_relaxed(mode | len, &desc->hw_mode);
  910. writel_relaxed(token, &desc->sw_token);
  911. writel_relaxed(buffer, &desc->sw_buffer);
  912. writel_relaxed(len, &desc->sw_len);
  913. desc_read(desc, sw_len);
  914. __cpdma_chan_submit(chan, desc);
  915. if (chan->state == CPDMA_STATE_ACTIVE && chan->rxfree)
  916. chan_write(chan, rxfree, 1);
  917. chan->count++;
  918. unlock_ret:
  919. spin_unlock_irqrestore(&chan->lock, flags);
  920. return ret;
  921. }
  922. EXPORT_SYMBOL_GPL(cpdma_chan_submit);
  923. bool cpdma_check_free_tx_desc(struct cpdma_chan *chan)
  924. {
  925. struct cpdma_ctlr *ctlr = chan->ctlr;
  926. struct cpdma_desc_pool *pool = ctlr->pool;
  927. bool free_tx_desc;
  928. unsigned long flags;
  929. spin_lock_irqsave(&chan->lock, flags);
  930. free_tx_desc = (chan->count < chan->desc_num) &&
  931. gen_pool_avail(pool->gen_pool);
  932. spin_unlock_irqrestore(&chan->lock, flags);
  933. return free_tx_desc;
  934. }
  935. EXPORT_SYMBOL_GPL(cpdma_check_free_tx_desc);
  936. static void __cpdma_chan_free(struct cpdma_chan *chan,
  937. struct cpdma_desc __iomem *desc,
  938. int outlen, int status)
  939. {
  940. struct cpdma_ctlr *ctlr = chan->ctlr;
  941. struct cpdma_desc_pool *pool = ctlr->pool;
  942. dma_addr_t buff_dma;
  943. int origlen;
  944. void *token;
  945. token = (void *)desc_read(desc, sw_token);
  946. buff_dma = desc_read(desc, sw_buffer);
  947. origlen = desc_read(desc, sw_len);
  948. dma_unmap_single(ctlr->dev, buff_dma, origlen, chan->dir);
  949. cpdma_desc_free(pool, desc, 1);
  950. (*chan->handler)(token, outlen, status);
  951. }
  952. static int __cpdma_chan_process(struct cpdma_chan *chan)
  953. {
  954. struct cpdma_ctlr *ctlr = chan->ctlr;
  955. struct cpdma_desc __iomem *desc;
  956. int status, outlen;
  957. int cb_status = 0;
  958. struct cpdma_desc_pool *pool = ctlr->pool;
  959. dma_addr_t desc_dma;
  960. unsigned long flags;
  961. spin_lock_irqsave(&chan->lock, flags);
  962. desc = chan->head;
  963. if (!desc) {
  964. chan->stats.empty_dequeue++;
  965. status = -ENOENT;
  966. goto unlock_ret;
  967. }
  968. desc_dma = desc_phys(pool, desc);
  969. status = desc_read(desc, hw_mode);
  970. outlen = status & 0x7ff;
  971. if (status & CPDMA_DESC_OWNER) {
  972. chan->stats.busy_dequeue++;
  973. status = -EBUSY;
  974. goto unlock_ret;
  975. }
  976. if (status & CPDMA_DESC_PASS_CRC)
  977. outlen -= CPDMA_DESC_CRC_LEN;
  978. status = status & (CPDMA_DESC_EOQ | CPDMA_DESC_TD_COMPLETE |
  979. CPDMA_DESC_PORT_MASK);
  980. chan->head = desc_from_phys(pool, desc_read(desc, hw_next));
  981. chan_write(chan, cp, desc_dma);
  982. chan->count--;
  983. chan->stats.good_dequeue++;
  984. if ((status & CPDMA_DESC_EOQ) && chan->head) {
  985. chan->stats.requeue++;
  986. chan_write(chan, hdp, desc_phys(pool, chan->head));
  987. }
  988. spin_unlock_irqrestore(&chan->lock, flags);
  989. if (unlikely(status & CPDMA_DESC_TD_COMPLETE))
  990. cb_status = -ENOSYS;
  991. else
  992. cb_status = status;
  993. __cpdma_chan_free(chan, desc, outlen, cb_status);
  994. return status;
  995. unlock_ret:
  996. spin_unlock_irqrestore(&chan->lock, flags);
  997. return status;
  998. }
  999. int cpdma_chan_process(struct cpdma_chan *chan, int quota)
  1000. {
  1001. int used = 0, ret = 0;
  1002. if (chan->state != CPDMA_STATE_ACTIVE)
  1003. return -EINVAL;
  1004. while (used < quota) {
  1005. ret = __cpdma_chan_process(chan);
  1006. if (ret < 0)
  1007. break;
  1008. used++;
  1009. }
  1010. return used;
  1011. }
  1012. EXPORT_SYMBOL_GPL(cpdma_chan_process);
  1013. int cpdma_chan_start(struct cpdma_chan *chan)
  1014. {
  1015. struct cpdma_ctlr *ctlr = chan->ctlr;
  1016. unsigned long flags;
  1017. int ret;
  1018. spin_lock_irqsave(&ctlr->lock, flags);
  1019. ret = cpdma_chan_set_chan_shaper(chan);
  1020. spin_unlock_irqrestore(&ctlr->lock, flags);
  1021. if (ret)
  1022. return ret;
  1023. ret = cpdma_chan_on(chan);
  1024. if (ret)
  1025. return ret;
  1026. return 0;
  1027. }
  1028. EXPORT_SYMBOL_GPL(cpdma_chan_start);
  1029. int cpdma_chan_stop(struct cpdma_chan *chan)
  1030. {
  1031. struct cpdma_ctlr *ctlr = chan->ctlr;
  1032. struct cpdma_desc_pool *pool = ctlr->pool;
  1033. unsigned long flags;
  1034. int ret;
  1035. unsigned timeout;
  1036. spin_lock_irqsave(&chan->lock, flags);
  1037. if (chan->state == CPDMA_STATE_TEARDOWN) {
  1038. spin_unlock_irqrestore(&chan->lock, flags);
  1039. return -EINVAL;
  1040. }
  1041. chan->state = CPDMA_STATE_TEARDOWN;
  1042. dma_reg_write(ctlr, chan->int_clear, chan->mask);
  1043. /* trigger teardown */
  1044. dma_reg_write(ctlr, chan->td, chan_linear(chan));
  1045. /* wait for teardown complete */
  1046. timeout = 100 * 100; /* 100 ms */
  1047. while (timeout) {
  1048. u32 cp = chan_read(chan, cp);
  1049. if ((cp & CPDMA_TEARDOWN_VALUE) == CPDMA_TEARDOWN_VALUE)
  1050. break;
  1051. udelay(10);
  1052. timeout--;
  1053. }
  1054. WARN_ON(!timeout);
  1055. chan_write(chan, cp, CPDMA_TEARDOWN_VALUE);
  1056. /* handle completed packets */
  1057. spin_unlock_irqrestore(&chan->lock, flags);
  1058. do {
  1059. ret = __cpdma_chan_process(chan);
  1060. if (ret < 0)
  1061. break;
  1062. } while ((ret & CPDMA_DESC_TD_COMPLETE) == 0);
  1063. spin_lock_irqsave(&chan->lock, flags);
  1064. /* remaining packets haven't been tx/rx'ed, clean them up */
  1065. while (chan->head) {
  1066. struct cpdma_desc __iomem *desc = chan->head;
  1067. dma_addr_t next_dma;
  1068. next_dma = desc_read(desc, hw_next);
  1069. chan->head = desc_from_phys(pool, next_dma);
  1070. chan->count--;
  1071. chan->stats.teardown_dequeue++;
  1072. /* issue callback without locks held */
  1073. spin_unlock_irqrestore(&chan->lock, flags);
  1074. __cpdma_chan_free(chan, desc, 0, -ENOSYS);
  1075. spin_lock_irqsave(&chan->lock, flags);
  1076. }
  1077. chan->state = CPDMA_STATE_IDLE;
  1078. spin_unlock_irqrestore(&chan->lock, flags);
  1079. return 0;
  1080. }
  1081. EXPORT_SYMBOL_GPL(cpdma_chan_stop);
  1082. int cpdma_chan_int_ctrl(struct cpdma_chan *chan, bool enable)
  1083. {
  1084. unsigned long flags;
  1085. spin_lock_irqsave(&chan->lock, flags);
  1086. if (chan->state != CPDMA_STATE_ACTIVE) {
  1087. spin_unlock_irqrestore(&chan->lock, flags);
  1088. return -EINVAL;
  1089. }
  1090. dma_reg_write(chan->ctlr, enable ? chan->int_set : chan->int_clear,
  1091. chan->mask);
  1092. spin_unlock_irqrestore(&chan->lock, flags);
  1093. return 0;
  1094. }
  1095. int cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
  1096. {
  1097. unsigned long flags;
  1098. int ret;
  1099. spin_lock_irqsave(&ctlr->lock, flags);
  1100. ret = _cpdma_control_get(ctlr, control);
  1101. spin_unlock_irqrestore(&ctlr->lock, flags);
  1102. return ret;
  1103. }
  1104. int cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
  1105. {
  1106. unsigned long flags;
  1107. int ret;
  1108. spin_lock_irqsave(&ctlr->lock, flags);
  1109. ret = _cpdma_control_set(ctlr, control, value);
  1110. spin_unlock_irqrestore(&ctlr->lock, flags);
  1111. return ret;
  1112. }
  1113. EXPORT_SYMBOL_GPL(cpdma_control_set);
  1114. int cpdma_get_num_rx_descs(struct cpdma_ctlr *ctlr)
  1115. {
  1116. return ctlr->num_rx_desc;
  1117. }
  1118. EXPORT_SYMBOL_GPL(cpdma_get_num_rx_descs);
  1119. int cpdma_get_num_tx_descs(struct cpdma_ctlr *ctlr)
  1120. {
  1121. return ctlr->num_tx_desc;
  1122. }
  1123. EXPORT_SYMBOL_GPL(cpdma_get_num_tx_descs);
  1124. void cpdma_set_num_rx_descs(struct cpdma_ctlr *ctlr, int num_rx_desc)
  1125. {
  1126. ctlr->num_rx_desc = num_rx_desc;
  1127. ctlr->num_tx_desc = ctlr->pool->num_desc - ctlr->num_rx_desc;
  1128. }
  1129. EXPORT_SYMBOL_GPL(cpdma_set_num_rx_descs);
  1130. MODULE_LICENSE("GPL");