davinci_cpdma.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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 %zd != avail %zd",
  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. static 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 = 0;
  355. int i;
  356. for (i = tx_chan_num(0); i < tx_chan_num(CPDMA_MAX_CHANNELS); i++) {
  357. chan = ctlr->channels[i];
  358. if (!chan)
  359. continue;
  360. if (chan == ch)
  361. chan->rate = rate;
  362. if (chan->rate) {
  363. rlim = 1;
  364. new_rmask |= chan->mask;
  365. continue;
  366. }
  367. if (rlim)
  368. goto err;
  369. }
  370. *rmask = new_rmask;
  371. *prio_mode = rlim;
  372. return 0;
  373. err:
  374. ch->rate = old_rate;
  375. dev_err(ctlr->dev, "Upper cpdma ch%d is not rate limited\n",
  376. chan->chan_num);
  377. return -EINVAL;
  378. }
  379. static u32 cpdma_chan_set_factors(struct cpdma_ctlr *ctlr,
  380. struct cpdma_chan *ch)
  381. {
  382. u32 delta = UINT_MAX, prev_delta = UINT_MAX, best_delta = UINT_MAX;
  383. u32 best_send_cnt = 0, best_idle_cnt = 0;
  384. u32 new_rate, best_rate = 0, rate_reg;
  385. u64 send_cnt, idle_cnt;
  386. u32 min_send_cnt, freq;
  387. u64 divident, divisor;
  388. if (!ch->rate) {
  389. ch->rate_factor = 0;
  390. goto set_factor;
  391. }
  392. freq = ctlr->params.bus_freq_mhz * 1000 * 32;
  393. if (!freq) {
  394. dev_err(ctlr->dev, "The bus frequency is not set\n");
  395. return -EINVAL;
  396. }
  397. min_send_cnt = freq - ch->rate;
  398. send_cnt = DIV_ROUND_UP(min_send_cnt, ch->rate);
  399. while (send_cnt <= CPDMA_MAX_RLIM_CNT) {
  400. divident = ch->rate * send_cnt;
  401. divisor = min_send_cnt;
  402. idle_cnt = DIV_ROUND_CLOSEST_ULL(divident, divisor);
  403. divident = freq * idle_cnt;
  404. divisor = idle_cnt + send_cnt;
  405. new_rate = DIV_ROUND_CLOSEST_ULL(divident, divisor);
  406. delta = new_rate >= ch->rate ? new_rate - ch->rate : delta;
  407. if (delta < best_delta) {
  408. best_delta = delta;
  409. best_send_cnt = send_cnt;
  410. best_idle_cnt = idle_cnt;
  411. best_rate = new_rate;
  412. if (!delta)
  413. break;
  414. }
  415. if (prev_delta >= delta) {
  416. prev_delta = delta;
  417. send_cnt++;
  418. continue;
  419. }
  420. idle_cnt++;
  421. divident = freq * idle_cnt;
  422. send_cnt = DIV_ROUND_CLOSEST_ULL(divident, ch->rate);
  423. send_cnt -= idle_cnt;
  424. prev_delta = UINT_MAX;
  425. }
  426. ch->rate = best_rate;
  427. ch->rate_factor = best_send_cnt | (best_idle_cnt << 16);
  428. set_factor:
  429. rate_reg = CPDMA_TX_PRI0_RATE + 4 * ch->chan_num;
  430. dma_reg_write(ctlr, rate_reg, ch->rate_factor);
  431. return 0;
  432. }
  433. struct cpdma_ctlr *cpdma_ctlr_create(struct cpdma_params *params)
  434. {
  435. struct cpdma_ctlr *ctlr;
  436. ctlr = devm_kzalloc(params->dev, sizeof(*ctlr), GFP_KERNEL);
  437. if (!ctlr)
  438. return NULL;
  439. ctlr->state = CPDMA_STATE_IDLE;
  440. ctlr->params = *params;
  441. ctlr->dev = params->dev;
  442. ctlr->chan_num = 0;
  443. spin_lock_init(&ctlr->lock);
  444. if (cpdma_desc_pool_create(ctlr))
  445. return NULL;
  446. /* split pool equally between RX/TX by default */
  447. ctlr->num_tx_desc = ctlr->pool->num_desc / 2;
  448. ctlr->num_rx_desc = ctlr->pool->num_desc - ctlr->num_tx_desc;
  449. if (WARN_ON(ctlr->num_chan > CPDMA_MAX_CHANNELS))
  450. ctlr->num_chan = CPDMA_MAX_CHANNELS;
  451. return ctlr;
  452. }
  453. EXPORT_SYMBOL_GPL(cpdma_ctlr_create);
  454. int cpdma_ctlr_start(struct cpdma_ctlr *ctlr)
  455. {
  456. struct cpdma_chan *chan;
  457. unsigned long flags;
  458. int i, prio_mode;
  459. spin_lock_irqsave(&ctlr->lock, flags);
  460. if (ctlr->state != CPDMA_STATE_IDLE) {
  461. spin_unlock_irqrestore(&ctlr->lock, flags);
  462. return -EBUSY;
  463. }
  464. if (ctlr->params.has_soft_reset) {
  465. unsigned timeout = 10 * 100;
  466. dma_reg_write(ctlr, CPDMA_SOFTRESET, 1);
  467. while (timeout) {
  468. if (dma_reg_read(ctlr, CPDMA_SOFTRESET) == 0)
  469. break;
  470. udelay(10);
  471. timeout--;
  472. }
  473. WARN_ON(!timeout);
  474. }
  475. for (i = 0; i < ctlr->num_chan; i++) {
  476. writel(0, ctlr->params.txhdp + 4 * i);
  477. writel(0, ctlr->params.rxhdp + 4 * i);
  478. writel(0, ctlr->params.txcp + 4 * i);
  479. writel(0, ctlr->params.rxcp + 4 * i);
  480. }
  481. dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
  482. dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
  483. dma_reg_write(ctlr, CPDMA_TXCONTROL, 1);
  484. dma_reg_write(ctlr, CPDMA_RXCONTROL, 1);
  485. ctlr->state = CPDMA_STATE_ACTIVE;
  486. prio_mode = 0;
  487. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  488. chan = ctlr->channels[i];
  489. if (chan) {
  490. cpdma_chan_set_chan_shaper(chan);
  491. cpdma_chan_on(chan);
  492. /* off prio mode if all tx channels are rate limited */
  493. if (is_tx_chan(chan) && !chan->rate)
  494. prio_mode = 1;
  495. }
  496. }
  497. _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
  498. _cpdma_control_set(ctlr, CPDMA_RX_BUFFER_OFFSET, 0);
  499. spin_unlock_irqrestore(&ctlr->lock, flags);
  500. return 0;
  501. }
  502. EXPORT_SYMBOL_GPL(cpdma_ctlr_start);
  503. int cpdma_ctlr_stop(struct cpdma_ctlr *ctlr)
  504. {
  505. unsigned long flags;
  506. int i;
  507. spin_lock_irqsave(&ctlr->lock, flags);
  508. if (ctlr->state != CPDMA_STATE_ACTIVE) {
  509. spin_unlock_irqrestore(&ctlr->lock, flags);
  510. return -EINVAL;
  511. }
  512. ctlr->state = CPDMA_STATE_TEARDOWN;
  513. spin_unlock_irqrestore(&ctlr->lock, flags);
  514. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  515. if (ctlr->channels[i])
  516. cpdma_chan_stop(ctlr->channels[i]);
  517. }
  518. spin_lock_irqsave(&ctlr->lock, flags);
  519. dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
  520. dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
  521. dma_reg_write(ctlr, CPDMA_TXCONTROL, 0);
  522. dma_reg_write(ctlr, CPDMA_RXCONTROL, 0);
  523. ctlr->state = CPDMA_STATE_IDLE;
  524. spin_unlock_irqrestore(&ctlr->lock, flags);
  525. return 0;
  526. }
  527. EXPORT_SYMBOL_GPL(cpdma_ctlr_stop);
  528. int cpdma_ctlr_destroy(struct cpdma_ctlr *ctlr)
  529. {
  530. int ret = 0, i;
  531. if (!ctlr)
  532. return -EINVAL;
  533. if (ctlr->state != CPDMA_STATE_IDLE)
  534. cpdma_ctlr_stop(ctlr);
  535. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++)
  536. cpdma_chan_destroy(ctlr->channels[i]);
  537. cpdma_desc_pool_destroy(ctlr);
  538. return ret;
  539. }
  540. EXPORT_SYMBOL_GPL(cpdma_ctlr_destroy);
  541. int cpdma_ctlr_int_ctrl(struct cpdma_ctlr *ctlr, bool enable)
  542. {
  543. unsigned long flags;
  544. int i;
  545. spin_lock_irqsave(&ctlr->lock, flags);
  546. if (ctlr->state != CPDMA_STATE_ACTIVE) {
  547. spin_unlock_irqrestore(&ctlr->lock, flags);
  548. return -EINVAL;
  549. }
  550. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  551. if (ctlr->channels[i])
  552. cpdma_chan_int_ctrl(ctlr->channels[i], enable);
  553. }
  554. spin_unlock_irqrestore(&ctlr->lock, flags);
  555. return 0;
  556. }
  557. EXPORT_SYMBOL_GPL(cpdma_ctlr_int_ctrl);
  558. void cpdma_ctlr_eoi(struct cpdma_ctlr *ctlr, u32 value)
  559. {
  560. dma_reg_write(ctlr, CPDMA_MACEOIVECTOR, value);
  561. }
  562. EXPORT_SYMBOL_GPL(cpdma_ctlr_eoi);
  563. u32 cpdma_ctrl_rxchs_state(struct cpdma_ctlr *ctlr)
  564. {
  565. return dma_reg_read(ctlr, CPDMA_RXINTSTATMASKED);
  566. }
  567. EXPORT_SYMBOL_GPL(cpdma_ctrl_rxchs_state);
  568. u32 cpdma_ctrl_txchs_state(struct cpdma_ctlr *ctlr)
  569. {
  570. return dma_reg_read(ctlr, CPDMA_TXINTSTATMASKED);
  571. }
  572. EXPORT_SYMBOL_GPL(cpdma_ctrl_txchs_state);
  573. static void cpdma_chan_set_descs(struct cpdma_ctlr *ctlr,
  574. int rx, int desc_num,
  575. int per_ch_desc)
  576. {
  577. struct cpdma_chan *chan, *most_chan = NULL;
  578. int desc_cnt = desc_num;
  579. int most_dnum = 0;
  580. int min, max, i;
  581. if (!desc_num)
  582. return;
  583. if (rx) {
  584. min = rx_chan_num(0);
  585. max = rx_chan_num(CPDMA_MAX_CHANNELS);
  586. } else {
  587. min = tx_chan_num(0);
  588. max = tx_chan_num(CPDMA_MAX_CHANNELS);
  589. }
  590. for (i = min; i < max; i++) {
  591. chan = ctlr->channels[i];
  592. if (!chan)
  593. continue;
  594. if (chan->weight)
  595. chan->desc_num = (chan->weight * desc_num) / 100;
  596. else
  597. chan->desc_num = per_ch_desc;
  598. desc_cnt -= chan->desc_num;
  599. if (most_dnum < chan->desc_num) {
  600. most_dnum = chan->desc_num;
  601. most_chan = chan;
  602. }
  603. }
  604. /* use remains */
  605. if (most_chan)
  606. most_chan->desc_num += desc_cnt;
  607. }
  608. /**
  609. * cpdma_chan_split_pool - Splits ctrl pool between all channels.
  610. * Has to be called under ctlr lock
  611. */
  612. int cpdma_chan_split_pool(struct cpdma_ctlr *ctlr)
  613. {
  614. int tx_per_ch_desc = 0, rx_per_ch_desc = 0;
  615. int free_rx_num = 0, free_tx_num = 0;
  616. int rx_weight = 0, tx_weight = 0;
  617. int tx_desc_num, rx_desc_num;
  618. struct cpdma_chan *chan;
  619. int i;
  620. if (!ctlr->chan_num)
  621. return 0;
  622. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  623. chan = ctlr->channels[i];
  624. if (!chan)
  625. continue;
  626. if (is_rx_chan(chan)) {
  627. if (!chan->weight)
  628. free_rx_num++;
  629. rx_weight += chan->weight;
  630. } else {
  631. if (!chan->weight)
  632. free_tx_num++;
  633. tx_weight += chan->weight;
  634. }
  635. }
  636. if (rx_weight > 100 || tx_weight > 100)
  637. return -EINVAL;
  638. tx_desc_num = ctlr->num_tx_desc;
  639. rx_desc_num = ctlr->num_rx_desc;
  640. if (free_tx_num) {
  641. tx_per_ch_desc = tx_desc_num - (tx_weight * tx_desc_num) / 100;
  642. tx_per_ch_desc /= free_tx_num;
  643. }
  644. if (free_rx_num) {
  645. rx_per_ch_desc = rx_desc_num - (rx_weight * rx_desc_num) / 100;
  646. rx_per_ch_desc /= free_rx_num;
  647. }
  648. cpdma_chan_set_descs(ctlr, 0, tx_desc_num, tx_per_ch_desc);
  649. cpdma_chan_set_descs(ctlr, 1, rx_desc_num, rx_per_ch_desc);
  650. return 0;
  651. }
  652. EXPORT_SYMBOL_GPL(cpdma_chan_split_pool);
  653. /* cpdma_chan_set_weight - set weight of a channel in percentage.
  654. * Tx and Rx channels have separate weights. That is 100% for RX
  655. * and 100% for Tx. The weight is used to split cpdma resources
  656. * in correct proportion required by the channels, including number
  657. * of descriptors. The channel rate is not enough to know the
  658. * weight of a channel as the maximum rate of an interface is needed.
  659. * If weight = 0, then channel uses rest of descriptors leaved by
  660. * weighted channels.
  661. */
  662. int cpdma_chan_set_weight(struct cpdma_chan *ch, int weight)
  663. {
  664. struct cpdma_ctlr *ctlr = ch->ctlr;
  665. unsigned long flags, ch_flags;
  666. int ret;
  667. spin_lock_irqsave(&ctlr->lock, flags);
  668. spin_lock_irqsave(&ch->lock, ch_flags);
  669. if (ch->weight == weight) {
  670. spin_unlock_irqrestore(&ch->lock, ch_flags);
  671. spin_unlock_irqrestore(&ctlr->lock, flags);
  672. return 0;
  673. }
  674. ch->weight = weight;
  675. spin_unlock_irqrestore(&ch->lock, ch_flags);
  676. /* re-split pool using new channel weight */
  677. ret = cpdma_chan_split_pool(ctlr);
  678. spin_unlock_irqrestore(&ctlr->lock, flags);
  679. return ret;
  680. }
  681. EXPORT_SYMBOL_GPL(cpdma_chan_set_weight);
  682. /* cpdma_chan_get_min_rate - get minimum allowed rate for channel
  683. * Should be called before cpdma_chan_set_rate.
  684. * Returns min rate in Kb/s
  685. */
  686. u32 cpdma_chan_get_min_rate(struct cpdma_ctlr *ctlr)
  687. {
  688. unsigned int divident, divisor;
  689. divident = ctlr->params.bus_freq_mhz * 32 * 1000;
  690. divisor = 1 + CPDMA_MAX_RLIM_CNT;
  691. return DIV_ROUND_UP(divident, divisor);
  692. }
  693. EXPORT_SYMBOL_GPL(cpdma_chan_get_min_rate);
  694. /* cpdma_chan_set_rate - limits bandwidth for transmit channel.
  695. * The bandwidth * limited channels have to be in order beginning from lowest.
  696. * ch - transmit channel the bandwidth is configured for
  697. * rate - bandwidth in Kb/s, if 0 - then off shaper
  698. */
  699. int cpdma_chan_set_rate(struct cpdma_chan *ch, u32 rate)
  700. {
  701. unsigned long flags, ch_flags;
  702. struct cpdma_ctlr *ctlr;
  703. int ret, prio_mode;
  704. u32 rmask;
  705. if (!ch || !is_tx_chan(ch))
  706. return -EINVAL;
  707. if (ch->rate == rate)
  708. return rate;
  709. ctlr = ch->ctlr;
  710. spin_lock_irqsave(&ctlr->lock, flags);
  711. spin_lock_irqsave(&ch->lock, ch_flags);
  712. ret = cpdma_chan_fit_rate(ch, rate, &rmask, &prio_mode);
  713. if (ret)
  714. goto err;
  715. ret = cpdma_chan_set_factors(ctlr, ch);
  716. if (ret)
  717. goto err;
  718. spin_unlock_irqrestore(&ch->lock, ch_flags);
  719. /* on shapers */
  720. _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask);
  721. _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
  722. spin_unlock_irqrestore(&ctlr->lock, flags);
  723. return ret;
  724. err:
  725. spin_unlock_irqrestore(&ch->lock, ch_flags);
  726. spin_unlock_irqrestore(&ctlr->lock, flags);
  727. return ret;
  728. }
  729. EXPORT_SYMBOL_GPL(cpdma_chan_set_rate);
  730. u32 cpdma_chan_get_rate(struct cpdma_chan *ch)
  731. {
  732. unsigned long flags;
  733. u32 rate;
  734. spin_lock_irqsave(&ch->lock, flags);
  735. rate = ch->rate;
  736. spin_unlock_irqrestore(&ch->lock, flags);
  737. return rate;
  738. }
  739. EXPORT_SYMBOL_GPL(cpdma_chan_get_rate);
  740. struct cpdma_chan *cpdma_chan_create(struct cpdma_ctlr *ctlr, int chan_num,
  741. cpdma_handler_fn handler, int rx_type)
  742. {
  743. int offset = chan_num * 4;
  744. struct cpdma_chan *chan;
  745. unsigned long flags;
  746. chan_num = rx_type ? rx_chan_num(chan_num) : tx_chan_num(chan_num);
  747. if (__chan_linear(chan_num) >= ctlr->num_chan)
  748. return ERR_PTR(-EINVAL);
  749. chan = devm_kzalloc(ctlr->dev, sizeof(*chan), GFP_KERNEL);
  750. if (!chan)
  751. return ERR_PTR(-ENOMEM);
  752. spin_lock_irqsave(&ctlr->lock, flags);
  753. if (ctlr->channels[chan_num]) {
  754. spin_unlock_irqrestore(&ctlr->lock, flags);
  755. devm_kfree(ctlr->dev, chan);
  756. return ERR_PTR(-EBUSY);
  757. }
  758. chan->ctlr = ctlr;
  759. chan->state = CPDMA_STATE_IDLE;
  760. chan->chan_num = chan_num;
  761. chan->handler = handler;
  762. chan->rate = 0;
  763. chan->weight = 0;
  764. if (is_rx_chan(chan)) {
  765. chan->hdp = ctlr->params.rxhdp + offset;
  766. chan->cp = ctlr->params.rxcp + offset;
  767. chan->rxfree = ctlr->params.rxfree + offset;
  768. chan->int_set = CPDMA_RXINTMASKSET;
  769. chan->int_clear = CPDMA_RXINTMASKCLEAR;
  770. chan->td = CPDMA_RXTEARDOWN;
  771. chan->dir = DMA_FROM_DEVICE;
  772. } else {
  773. chan->hdp = ctlr->params.txhdp + offset;
  774. chan->cp = ctlr->params.txcp + offset;
  775. chan->int_set = CPDMA_TXINTMASKSET;
  776. chan->int_clear = CPDMA_TXINTMASKCLEAR;
  777. chan->td = CPDMA_TXTEARDOWN;
  778. chan->dir = DMA_TO_DEVICE;
  779. }
  780. chan->mask = BIT(chan_linear(chan));
  781. spin_lock_init(&chan->lock);
  782. ctlr->channels[chan_num] = chan;
  783. ctlr->chan_num++;
  784. cpdma_chan_split_pool(ctlr);
  785. spin_unlock_irqrestore(&ctlr->lock, flags);
  786. return chan;
  787. }
  788. EXPORT_SYMBOL_GPL(cpdma_chan_create);
  789. int cpdma_chan_get_rx_buf_num(struct cpdma_chan *chan)
  790. {
  791. unsigned long flags;
  792. int desc_num;
  793. spin_lock_irqsave(&chan->lock, flags);
  794. desc_num = chan->desc_num;
  795. spin_unlock_irqrestore(&chan->lock, flags);
  796. return desc_num;
  797. }
  798. EXPORT_SYMBOL_GPL(cpdma_chan_get_rx_buf_num);
  799. int cpdma_chan_destroy(struct cpdma_chan *chan)
  800. {
  801. struct cpdma_ctlr *ctlr;
  802. unsigned long flags;
  803. if (!chan)
  804. return -EINVAL;
  805. ctlr = chan->ctlr;
  806. spin_lock_irqsave(&ctlr->lock, flags);
  807. if (chan->state != CPDMA_STATE_IDLE)
  808. cpdma_chan_stop(chan);
  809. ctlr->channels[chan->chan_num] = NULL;
  810. ctlr->chan_num--;
  811. devm_kfree(ctlr->dev, chan);
  812. cpdma_chan_split_pool(ctlr);
  813. spin_unlock_irqrestore(&ctlr->lock, flags);
  814. return 0;
  815. }
  816. EXPORT_SYMBOL_GPL(cpdma_chan_destroy);
  817. int cpdma_chan_get_stats(struct cpdma_chan *chan,
  818. struct cpdma_chan_stats *stats)
  819. {
  820. unsigned long flags;
  821. if (!chan)
  822. return -EINVAL;
  823. spin_lock_irqsave(&chan->lock, flags);
  824. memcpy(stats, &chan->stats, sizeof(*stats));
  825. spin_unlock_irqrestore(&chan->lock, flags);
  826. return 0;
  827. }
  828. EXPORT_SYMBOL_GPL(cpdma_chan_get_stats);
  829. static void __cpdma_chan_submit(struct cpdma_chan *chan,
  830. struct cpdma_desc __iomem *desc)
  831. {
  832. struct cpdma_ctlr *ctlr = chan->ctlr;
  833. struct cpdma_desc __iomem *prev = chan->tail;
  834. struct cpdma_desc_pool *pool = ctlr->pool;
  835. dma_addr_t desc_dma;
  836. u32 mode;
  837. desc_dma = desc_phys(pool, desc);
  838. /* simple case - idle channel */
  839. if (!chan->head) {
  840. chan->stats.head_enqueue++;
  841. chan->head = desc;
  842. chan->tail = desc;
  843. if (chan->state == CPDMA_STATE_ACTIVE)
  844. chan_write(chan, hdp, desc_dma);
  845. return;
  846. }
  847. /* first chain the descriptor at the tail of the list */
  848. desc_write(prev, hw_next, desc_dma);
  849. chan->tail = desc;
  850. chan->stats.tail_enqueue++;
  851. /* next check if EOQ has been triggered already */
  852. mode = desc_read(prev, hw_mode);
  853. if (((mode & (CPDMA_DESC_EOQ | CPDMA_DESC_OWNER)) == CPDMA_DESC_EOQ) &&
  854. (chan->state == CPDMA_STATE_ACTIVE)) {
  855. desc_write(prev, hw_mode, mode & ~CPDMA_DESC_EOQ);
  856. chan_write(chan, hdp, desc_dma);
  857. chan->stats.misqueued++;
  858. }
  859. }
  860. int cpdma_chan_submit(struct cpdma_chan *chan, void *token, void *data,
  861. int len, int directed)
  862. {
  863. struct cpdma_ctlr *ctlr = chan->ctlr;
  864. struct cpdma_desc __iomem *desc;
  865. dma_addr_t buffer;
  866. unsigned long flags;
  867. u32 mode;
  868. int ret = 0;
  869. spin_lock_irqsave(&chan->lock, flags);
  870. if (chan->state == CPDMA_STATE_TEARDOWN) {
  871. ret = -EINVAL;
  872. goto unlock_ret;
  873. }
  874. if (chan->count >= chan->desc_num) {
  875. chan->stats.desc_alloc_fail++;
  876. ret = -ENOMEM;
  877. goto unlock_ret;
  878. }
  879. desc = cpdma_desc_alloc(ctlr->pool);
  880. if (!desc) {
  881. chan->stats.desc_alloc_fail++;
  882. ret = -ENOMEM;
  883. goto unlock_ret;
  884. }
  885. if (len < ctlr->params.min_packet_size) {
  886. len = ctlr->params.min_packet_size;
  887. chan->stats.runt_transmit_buff++;
  888. }
  889. buffer = dma_map_single(ctlr->dev, data, len, chan->dir);
  890. ret = dma_mapping_error(ctlr->dev, buffer);
  891. if (ret) {
  892. cpdma_desc_free(ctlr->pool, desc, 1);
  893. ret = -EINVAL;
  894. goto unlock_ret;
  895. }
  896. mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
  897. cpdma_desc_to_port(chan, mode, directed);
  898. /* Relaxed IO accessors can be used here as there is read barrier
  899. * at the end of write sequence.
  900. */
  901. writel_relaxed(0, &desc->hw_next);
  902. writel_relaxed(buffer, &desc->hw_buffer);
  903. writel_relaxed(len, &desc->hw_len);
  904. writel_relaxed(mode | len, &desc->hw_mode);
  905. writel_relaxed((uintptr_t)token, &desc->sw_token);
  906. writel_relaxed(buffer, &desc->sw_buffer);
  907. writel_relaxed(len, &desc->sw_len);
  908. desc_read(desc, sw_len);
  909. __cpdma_chan_submit(chan, desc);
  910. if (chan->state == CPDMA_STATE_ACTIVE && chan->rxfree)
  911. chan_write(chan, rxfree, 1);
  912. chan->count++;
  913. unlock_ret:
  914. spin_unlock_irqrestore(&chan->lock, flags);
  915. return ret;
  916. }
  917. EXPORT_SYMBOL_GPL(cpdma_chan_submit);
  918. bool cpdma_check_free_tx_desc(struct cpdma_chan *chan)
  919. {
  920. struct cpdma_ctlr *ctlr = chan->ctlr;
  921. struct cpdma_desc_pool *pool = ctlr->pool;
  922. bool free_tx_desc;
  923. unsigned long flags;
  924. spin_lock_irqsave(&chan->lock, flags);
  925. free_tx_desc = (chan->count < chan->desc_num) &&
  926. gen_pool_avail(pool->gen_pool);
  927. spin_unlock_irqrestore(&chan->lock, flags);
  928. return free_tx_desc;
  929. }
  930. EXPORT_SYMBOL_GPL(cpdma_check_free_tx_desc);
  931. static void __cpdma_chan_free(struct cpdma_chan *chan,
  932. struct cpdma_desc __iomem *desc,
  933. int outlen, int status)
  934. {
  935. struct cpdma_ctlr *ctlr = chan->ctlr;
  936. struct cpdma_desc_pool *pool = ctlr->pool;
  937. dma_addr_t buff_dma;
  938. int origlen;
  939. uintptr_t token;
  940. token = desc_read(desc, sw_token);
  941. buff_dma = desc_read(desc, sw_buffer);
  942. origlen = desc_read(desc, sw_len);
  943. dma_unmap_single(ctlr->dev, buff_dma, origlen, chan->dir);
  944. cpdma_desc_free(pool, desc, 1);
  945. (*chan->handler)((void *)token, outlen, status);
  946. }
  947. static int __cpdma_chan_process(struct cpdma_chan *chan)
  948. {
  949. struct cpdma_ctlr *ctlr = chan->ctlr;
  950. struct cpdma_desc __iomem *desc;
  951. int status, outlen;
  952. int cb_status = 0;
  953. struct cpdma_desc_pool *pool = ctlr->pool;
  954. dma_addr_t desc_dma;
  955. unsigned long flags;
  956. spin_lock_irqsave(&chan->lock, flags);
  957. desc = chan->head;
  958. if (!desc) {
  959. chan->stats.empty_dequeue++;
  960. status = -ENOENT;
  961. goto unlock_ret;
  962. }
  963. desc_dma = desc_phys(pool, desc);
  964. status = desc_read(desc, hw_mode);
  965. outlen = status & 0x7ff;
  966. if (status & CPDMA_DESC_OWNER) {
  967. chan->stats.busy_dequeue++;
  968. status = -EBUSY;
  969. goto unlock_ret;
  970. }
  971. if (status & CPDMA_DESC_PASS_CRC)
  972. outlen -= CPDMA_DESC_CRC_LEN;
  973. status = status & (CPDMA_DESC_EOQ | CPDMA_DESC_TD_COMPLETE |
  974. CPDMA_DESC_PORT_MASK | CPDMA_RX_VLAN_ENCAP);
  975. chan->head = desc_from_phys(pool, desc_read(desc, hw_next));
  976. chan_write(chan, cp, desc_dma);
  977. chan->count--;
  978. chan->stats.good_dequeue++;
  979. if ((status & CPDMA_DESC_EOQ) && chan->head) {
  980. chan->stats.requeue++;
  981. chan_write(chan, hdp, desc_phys(pool, chan->head));
  982. }
  983. spin_unlock_irqrestore(&chan->lock, flags);
  984. if (unlikely(status & CPDMA_DESC_TD_COMPLETE))
  985. cb_status = -ENOSYS;
  986. else
  987. cb_status = status;
  988. __cpdma_chan_free(chan, desc, outlen, cb_status);
  989. return status;
  990. unlock_ret:
  991. spin_unlock_irqrestore(&chan->lock, flags);
  992. return status;
  993. }
  994. int cpdma_chan_process(struct cpdma_chan *chan, int quota)
  995. {
  996. int used = 0, ret = 0;
  997. if (chan->state != CPDMA_STATE_ACTIVE)
  998. return -EINVAL;
  999. while (used < quota) {
  1000. ret = __cpdma_chan_process(chan);
  1001. if (ret < 0)
  1002. break;
  1003. used++;
  1004. }
  1005. return used;
  1006. }
  1007. EXPORT_SYMBOL_GPL(cpdma_chan_process);
  1008. int cpdma_chan_start(struct cpdma_chan *chan)
  1009. {
  1010. struct cpdma_ctlr *ctlr = chan->ctlr;
  1011. unsigned long flags;
  1012. int ret;
  1013. spin_lock_irqsave(&ctlr->lock, flags);
  1014. ret = cpdma_chan_set_chan_shaper(chan);
  1015. spin_unlock_irqrestore(&ctlr->lock, flags);
  1016. if (ret)
  1017. return ret;
  1018. ret = cpdma_chan_on(chan);
  1019. if (ret)
  1020. return ret;
  1021. return 0;
  1022. }
  1023. EXPORT_SYMBOL_GPL(cpdma_chan_start);
  1024. int cpdma_chan_stop(struct cpdma_chan *chan)
  1025. {
  1026. struct cpdma_ctlr *ctlr = chan->ctlr;
  1027. struct cpdma_desc_pool *pool = ctlr->pool;
  1028. unsigned long flags;
  1029. int ret;
  1030. unsigned timeout;
  1031. spin_lock_irqsave(&chan->lock, flags);
  1032. if (chan->state == CPDMA_STATE_TEARDOWN) {
  1033. spin_unlock_irqrestore(&chan->lock, flags);
  1034. return -EINVAL;
  1035. }
  1036. chan->state = CPDMA_STATE_TEARDOWN;
  1037. dma_reg_write(ctlr, chan->int_clear, chan->mask);
  1038. /* trigger teardown */
  1039. dma_reg_write(ctlr, chan->td, chan_linear(chan));
  1040. /* wait for teardown complete */
  1041. timeout = 100 * 100; /* 100 ms */
  1042. while (timeout) {
  1043. u32 cp = chan_read(chan, cp);
  1044. if ((cp & CPDMA_TEARDOWN_VALUE) == CPDMA_TEARDOWN_VALUE)
  1045. break;
  1046. udelay(10);
  1047. timeout--;
  1048. }
  1049. WARN_ON(!timeout);
  1050. chan_write(chan, cp, CPDMA_TEARDOWN_VALUE);
  1051. /* handle completed packets */
  1052. spin_unlock_irqrestore(&chan->lock, flags);
  1053. do {
  1054. ret = __cpdma_chan_process(chan);
  1055. if (ret < 0)
  1056. break;
  1057. } while ((ret & CPDMA_DESC_TD_COMPLETE) == 0);
  1058. spin_lock_irqsave(&chan->lock, flags);
  1059. /* remaining packets haven't been tx/rx'ed, clean them up */
  1060. while (chan->head) {
  1061. struct cpdma_desc __iomem *desc = chan->head;
  1062. dma_addr_t next_dma;
  1063. next_dma = desc_read(desc, hw_next);
  1064. chan->head = desc_from_phys(pool, next_dma);
  1065. chan->count--;
  1066. chan->stats.teardown_dequeue++;
  1067. /* issue callback without locks held */
  1068. spin_unlock_irqrestore(&chan->lock, flags);
  1069. __cpdma_chan_free(chan, desc, 0, -ENOSYS);
  1070. spin_lock_irqsave(&chan->lock, flags);
  1071. }
  1072. chan->state = CPDMA_STATE_IDLE;
  1073. spin_unlock_irqrestore(&chan->lock, flags);
  1074. return 0;
  1075. }
  1076. EXPORT_SYMBOL_GPL(cpdma_chan_stop);
  1077. int cpdma_chan_int_ctrl(struct cpdma_chan *chan, bool enable)
  1078. {
  1079. unsigned long flags;
  1080. spin_lock_irqsave(&chan->lock, flags);
  1081. if (chan->state != CPDMA_STATE_ACTIVE) {
  1082. spin_unlock_irqrestore(&chan->lock, flags);
  1083. return -EINVAL;
  1084. }
  1085. dma_reg_write(chan->ctlr, enable ? chan->int_set : chan->int_clear,
  1086. chan->mask);
  1087. spin_unlock_irqrestore(&chan->lock, flags);
  1088. return 0;
  1089. }
  1090. int cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
  1091. {
  1092. unsigned long flags;
  1093. int ret;
  1094. spin_lock_irqsave(&ctlr->lock, flags);
  1095. ret = _cpdma_control_get(ctlr, control);
  1096. spin_unlock_irqrestore(&ctlr->lock, flags);
  1097. return ret;
  1098. }
  1099. int cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
  1100. {
  1101. unsigned long flags;
  1102. int ret;
  1103. spin_lock_irqsave(&ctlr->lock, flags);
  1104. ret = _cpdma_control_set(ctlr, control, value);
  1105. spin_unlock_irqrestore(&ctlr->lock, flags);
  1106. return ret;
  1107. }
  1108. EXPORT_SYMBOL_GPL(cpdma_control_set);
  1109. int cpdma_get_num_rx_descs(struct cpdma_ctlr *ctlr)
  1110. {
  1111. return ctlr->num_rx_desc;
  1112. }
  1113. EXPORT_SYMBOL_GPL(cpdma_get_num_rx_descs);
  1114. int cpdma_get_num_tx_descs(struct cpdma_ctlr *ctlr)
  1115. {
  1116. return ctlr->num_tx_desc;
  1117. }
  1118. EXPORT_SYMBOL_GPL(cpdma_get_num_tx_descs);
  1119. void cpdma_set_num_rx_descs(struct cpdma_ctlr *ctlr, int num_rx_desc)
  1120. {
  1121. ctlr->num_rx_desc = num_rx_desc;
  1122. ctlr->num_tx_desc = ctlr->pool->num_desc - ctlr->num_rx_desc;
  1123. }
  1124. EXPORT_SYMBOL_GPL(cpdma_set_num_rx_descs);
  1125. MODULE_LICENSE("GPL");