davinci_cpdma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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_TXINTSTATRAW 0x80
  35. #define CPDMA_TXINTSTATMASKED 0x84
  36. #define CPDMA_TXINTMASKSET 0x88
  37. #define CPDMA_TXINTMASKCLEAR 0x8c
  38. #define CPDMA_MACINVECTOR 0x90
  39. #define CPDMA_MACEOIVECTOR 0x94
  40. #define CPDMA_RXINTSTATRAW 0xa0
  41. #define CPDMA_RXINTSTATMASKED 0xa4
  42. #define CPDMA_RXINTMASKSET 0xa8
  43. #define CPDMA_RXINTMASKCLEAR 0xac
  44. #define CPDMA_DMAINTSTATRAW 0xb0
  45. #define CPDMA_DMAINTSTATMASKED 0xb4
  46. #define CPDMA_DMAINTMASKSET 0xb8
  47. #define CPDMA_DMAINTMASKCLEAR 0xbc
  48. #define CPDMA_DMAINT_HOSTERR BIT(1)
  49. /* the following exist only if has_ext_regs is set */
  50. #define CPDMA_DMACONTROL 0x20
  51. #define CPDMA_DMASTATUS 0x24
  52. #define CPDMA_RXBUFFOFS 0x28
  53. #define CPDMA_EM_CONTROL 0x2c
  54. /* Descriptor mode bits */
  55. #define CPDMA_DESC_SOP BIT(31)
  56. #define CPDMA_DESC_EOP BIT(30)
  57. #define CPDMA_DESC_OWNER BIT(29)
  58. #define CPDMA_DESC_EOQ BIT(28)
  59. #define CPDMA_DESC_TD_COMPLETE BIT(27)
  60. #define CPDMA_DESC_PASS_CRC BIT(26)
  61. #define CPDMA_DESC_TO_PORT_EN BIT(20)
  62. #define CPDMA_TO_PORT_SHIFT 16
  63. #define CPDMA_DESC_PORT_MASK (BIT(18) | BIT(17) | BIT(16))
  64. #define CPDMA_DESC_CRC_LEN 4
  65. #define CPDMA_TEARDOWN_VALUE 0xfffffffc
  66. struct cpdma_desc {
  67. /* hardware fields */
  68. u32 hw_next;
  69. u32 hw_buffer;
  70. u32 hw_len;
  71. u32 hw_mode;
  72. /* software fields */
  73. void *sw_token;
  74. u32 sw_buffer;
  75. u32 sw_len;
  76. };
  77. struct cpdma_desc_pool {
  78. phys_addr_t phys;
  79. dma_addr_t hw_addr;
  80. void __iomem *iomap; /* ioremap map */
  81. void *cpumap; /* dma_alloc map */
  82. int desc_size, mem_size;
  83. int num_desc;
  84. struct device *dev;
  85. struct gen_pool *gen_pool;
  86. };
  87. enum cpdma_state {
  88. CPDMA_STATE_IDLE,
  89. CPDMA_STATE_ACTIVE,
  90. CPDMA_STATE_TEARDOWN,
  91. };
  92. struct cpdma_ctlr {
  93. enum cpdma_state state;
  94. struct cpdma_params params;
  95. struct device *dev;
  96. struct cpdma_desc_pool *pool;
  97. spinlock_t lock;
  98. struct cpdma_chan *channels[2 * CPDMA_MAX_CHANNELS];
  99. int chan_num;
  100. };
  101. struct cpdma_chan {
  102. struct cpdma_desc __iomem *head, *tail;
  103. void __iomem *hdp, *cp, *rxfree;
  104. enum cpdma_state state;
  105. struct cpdma_ctlr *ctlr;
  106. int chan_num;
  107. spinlock_t lock;
  108. int count;
  109. u32 desc_num;
  110. u32 mask;
  111. cpdma_handler_fn handler;
  112. enum dma_data_direction dir;
  113. struct cpdma_chan_stats stats;
  114. /* offsets into dmaregs */
  115. int int_set, int_clear, td;
  116. };
  117. #define tx_chan_num(chan) (chan)
  118. #define rx_chan_num(chan) ((chan) + CPDMA_MAX_CHANNELS)
  119. #define is_rx_chan(chan) ((chan)->chan_num >= CPDMA_MAX_CHANNELS)
  120. #define is_tx_chan(chan) (!is_rx_chan(chan))
  121. #define __chan_linear(chan_num) ((chan_num) & (CPDMA_MAX_CHANNELS - 1))
  122. #define chan_linear(chan) __chan_linear((chan)->chan_num)
  123. /* The following make access to common cpdma_ctlr params more readable */
  124. #define dmaregs params.dmaregs
  125. #define num_chan params.num_chan
  126. /* various accessors */
  127. #define dma_reg_read(ctlr, ofs) __raw_readl((ctlr)->dmaregs + (ofs))
  128. #define chan_read(chan, fld) __raw_readl((chan)->fld)
  129. #define desc_read(desc, fld) __raw_readl(&(desc)->fld)
  130. #define dma_reg_write(ctlr, ofs, v) __raw_writel(v, (ctlr)->dmaregs + (ofs))
  131. #define chan_write(chan, fld, v) __raw_writel(v, (chan)->fld)
  132. #define desc_write(desc, fld, v) __raw_writel((u32)(v), &(desc)->fld)
  133. #define cpdma_desc_to_port(chan, mode, directed) \
  134. do { \
  135. if (!is_rx_chan(chan) && ((directed == 1) || \
  136. (directed == 2))) \
  137. mode |= (CPDMA_DESC_TO_PORT_EN | \
  138. (directed << CPDMA_TO_PORT_SHIFT)); \
  139. } while (0)
  140. static void cpdma_desc_pool_destroy(struct cpdma_desc_pool *pool)
  141. {
  142. if (!pool)
  143. return;
  144. WARN(gen_pool_size(pool->gen_pool) != gen_pool_avail(pool->gen_pool),
  145. "cpdma_desc_pool size %d != avail %d",
  146. gen_pool_size(pool->gen_pool),
  147. gen_pool_avail(pool->gen_pool));
  148. if (pool->cpumap)
  149. dma_free_coherent(pool->dev, pool->mem_size, pool->cpumap,
  150. pool->phys);
  151. else
  152. iounmap(pool->iomap);
  153. }
  154. /*
  155. * Utility constructs for a cpdma descriptor pool. Some devices (e.g. davinci
  156. * emac) have dedicated on-chip memory for these descriptors. Some other
  157. * devices (e.g. cpsw switches) use plain old memory. Descriptor pools
  158. * abstract out these details
  159. */
  160. static struct cpdma_desc_pool *
  161. cpdma_desc_pool_create(struct device *dev, u32 phys, dma_addr_t hw_addr,
  162. int size, int align)
  163. {
  164. struct cpdma_desc_pool *pool;
  165. int ret;
  166. pool = devm_kzalloc(dev, sizeof(*pool), GFP_KERNEL);
  167. if (!pool)
  168. goto gen_pool_create_fail;
  169. pool->dev = dev;
  170. pool->mem_size = size;
  171. pool->desc_size = ALIGN(sizeof(struct cpdma_desc), align);
  172. pool->num_desc = size / pool->desc_size;
  173. pool->gen_pool = devm_gen_pool_create(dev, ilog2(pool->desc_size), -1,
  174. "cpdma");
  175. if (IS_ERR(pool->gen_pool)) {
  176. dev_err(dev, "pool create failed %ld\n",
  177. PTR_ERR(pool->gen_pool));
  178. goto gen_pool_create_fail;
  179. }
  180. if (phys) {
  181. pool->phys = phys;
  182. pool->iomap = ioremap(phys, size); /* should be memremap? */
  183. pool->hw_addr = hw_addr;
  184. } else {
  185. pool->cpumap = dma_alloc_coherent(dev, size, &pool->hw_addr,
  186. GFP_KERNEL);
  187. pool->iomap = (void __iomem __force *)pool->cpumap;
  188. pool->phys = pool->hw_addr; /* assumes no IOMMU, don't use this value */
  189. }
  190. if (!pool->iomap)
  191. goto gen_pool_create_fail;
  192. ret = gen_pool_add_virt(pool->gen_pool, (unsigned long)pool->iomap,
  193. pool->phys, pool->mem_size, -1);
  194. if (ret < 0) {
  195. dev_err(dev, "pool add failed %d\n", ret);
  196. goto gen_pool_add_virt_fail;
  197. }
  198. return pool;
  199. gen_pool_add_virt_fail:
  200. cpdma_desc_pool_destroy(pool);
  201. gen_pool_create_fail:
  202. return NULL;
  203. }
  204. static inline dma_addr_t desc_phys(struct cpdma_desc_pool *pool,
  205. struct cpdma_desc __iomem *desc)
  206. {
  207. if (!desc)
  208. return 0;
  209. return pool->hw_addr + (__force long)desc - (__force long)pool->iomap;
  210. }
  211. static inline struct cpdma_desc __iomem *
  212. desc_from_phys(struct cpdma_desc_pool *pool, dma_addr_t dma)
  213. {
  214. return dma ? pool->iomap + dma - pool->hw_addr : NULL;
  215. }
  216. static struct cpdma_desc __iomem *
  217. cpdma_desc_alloc(struct cpdma_desc_pool *pool)
  218. {
  219. return (struct cpdma_desc __iomem *)
  220. gen_pool_alloc(pool->gen_pool, pool->desc_size);
  221. }
  222. static void cpdma_desc_free(struct cpdma_desc_pool *pool,
  223. struct cpdma_desc __iomem *desc, int num_desc)
  224. {
  225. gen_pool_free(pool->gen_pool, (unsigned long)desc, pool->desc_size);
  226. }
  227. struct cpdma_ctlr *cpdma_ctlr_create(struct cpdma_params *params)
  228. {
  229. struct cpdma_ctlr *ctlr;
  230. ctlr = devm_kzalloc(params->dev, sizeof(*ctlr), GFP_KERNEL);
  231. if (!ctlr)
  232. return NULL;
  233. ctlr->state = CPDMA_STATE_IDLE;
  234. ctlr->params = *params;
  235. ctlr->dev = params->dev;
  236. ctlr->chan_num = 0;
  237. spin_lock_init(&ctlr->lock);
  238. ctlr->pool = cpdma_desc_pool_create(ctlr->dev,
  239. ctlr->params.desc_mem_phys,
  240. ctlr->params.desc_hw_addr,
  241. ctlr->params.desc_mem_size,
  242. ctlr->params.desc_align);
  243. if (!ctlr->pool)
  244. return NULL;
  245. if (WARN_ON(ctlr->num_chan > CPDMA_MAX_CHANNELS))
  246. ctlr->num_chan = CPDMA_MAX_CHANNELS;
  247. return ctlr;
  248. }
  249. EXPORT_SYMBOL_GPL(cpdma_ctlr_create);
  250. int cpdma_ctlr_start(struct cpdma_ctlr *ctlr)
  251. {
  252. unsigned long flags;
  253. int i;
  254. spin_lock_irqsave(&ctlr->lock, flags);
  255. if (ctlr->state != CPDMA_STATE_IDLE) {
  256. spin_unlock_irqrestore(&ctlr->lock, flags);
  257. return -EBUSY;
  258. }
  259. if (ctlr->params.has_soft_reset) {
  260. unsigned timeout = 10 * 100;
  261. dma_reg_write(ctlr, CPDMA_SOFTRESET, 1);
  262. while (timeout) {
  263. if (dma_reg_read(ctlr, CPDMA_SOFTRESET) == 0)
  264. break;
  265. udelay(10);
  266. timeout--;
  267. }
  268. WARN_ON(!timeout);
  269. }
  270. for (i = 0; i < ctlr->num_chan; i++) {
  271. __raw_writel(0, ctlr->params.txhdp + 4 * i);
  272. __raw_writel(0, ctlr->params.rxhdp + 4 * i);
  273. __raw_writel(0, ctlr->params.txcp + 4 * i);
  274. __raw_writel(0, ctlr->params.rxcp + 4 * i);
  275. }
  276. dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
  277. dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
  278. dma_reg_write(ctlr, CPDMA_TXCONTROL, 1);
  279. dma_reg_write(ctlr, CPDMA_RXCONTROL, 1);
  280. ctlr->state = CPDMA_STATE_ACTIVE;
  281. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  282. if (ctlr->channels[i])
  283. cpdma_chan_start(ctlr->channels[i]);
  284. }
  285. spin_unlock_irqrestore(&ctlr->lock, flags);
  286. return 0;
  287. }
  288. EXPORT_SYMBOL_GPL(cpdma_ctlr_start);
  289. int cpdma_ctlr_stop(struct cpdma_ctlr *ctlr)
  290. {
  291. unsigned long flags;
  292. int i;
  293. spin_lock_irqsave(&ctlr->lock, flags);
  294. if (ctlr->state == CPDMA_STATE_TEARDOWN) {
  295. spin_unlock_irqrestore(&ctlr->lock, flags);
  296. return -EINVAL;
  297. }
  298. ctlr->state = CPDMA_STATE_TEARDOWN;
  299. spin_unlock_irqrestore(&ctlr->lock, flags);
  300. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  301. if (ctlr->channels[i])
  302. cpdma_chan_stop(ctlr->channels[i]);
  303. }
  304. spin_lock_irqsave(&ctlr->lock, flags);
  305. dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
  306. dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
  307. dma_reg_write(ctlr, CPDMA_TXCONTROL, 0);
  308. dma_reg_write(ctlr, CPDMA_RXCONTROL, 0);
  309. ctlr->state = CPDMA_STATE_IDLE;
  310. spin_unlock_irqrestore(&ctlr->lock, flags);
  311. return 0;
  312. }
  313. EXPORT_SYMBOL_GPL(cpdma_ctlr_stop);
  314. int cpdma_ctlr_destroy(struct cpdma_ctlr *ctlr)
  315. {
  316. int ret = 0, i;
  317. if (!ctlr)
  318. return -EINVAL;
  319. if (ctlr->state != CPDMA_STATE_IDLE)
  320. cpdma_ctlr_stop(ctlr);
  321. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++)
  322. cpdma_chan_destroy(ctlr->channels[i]);
  323. cpdma_desc_pool_destroy(ctlr->pool);
  324. return ret;
  325. }
  326. EXPORT_SYMBOL_GPL(cpdma_ctlr_destroy);
  327. int cpdma_ctlr_int_ctrl(struct cpdma_ctlr *ctlr, bool enable)
  328. {
  329. unsigned long flags;
  330. int i, reg;
  331. spin_lock_irqsave(&ctlr->lock, flags);
  332. if (ctlr->state != CPDMA_STATE_ACTIVE) {
  333. spin_unlock_irqrestore(&ctlr->lock, flags);
  334. return -EINVAL;
  335. }
  336. reg = enable ? CPDMA_DMAINTMASKSET : CPDMA_DMAINTMASKCLEAR;
  337. dma_reg_write(ctlr, reg, CPDMA_DMAINT_HOSTERR);
  338. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  339. if (ctlr->channels[i])
  340. cpdma_chan_int_ctrl(ctlr->channels[i], enable);
  341. }
  342. spin_unlock_irqrestore(&ctlr->lock, flags);
  343. return 0;
  344. }
  345. EXPORT_SYMBOL_GPL(cpdma_ctlr_int_ctrl);
  346. void cpdma_ctlr_eoi(struct cpdma_ctlr *ctlr, u32 value)
  347. {
  348. dma_reg_write(ctlr, CPDMA_MACEOIVECTOR, value);
  349. }
  350. EXPORT_SYMBOL_GPL(cpdma_ctlr_eoi);
  351. u32 cpdma_ctrl_rxchs_state(struct cpdma_ctlr *ctlr)
  352. {
  353. return dma_reg_read(ctlr, CPDMA_RXINTSTATMASKED);
  354. }
  355. EXPORT_SYMBOL_GPL(cpdma_ctrl_rxchs_state);
  356. u32 cpdma_ctrl_txchs_state(struct cpdma_ctlr *ctlr)
  357. {
  358. return dma_reg_read(ctlr, CPDMA_TXINTSTATMASKED);
  359. }
  360. EXPORT_SYMBOL_GPL(cpdma_ctrl_txchs_state);
  361. /**
  362. * cpdma_chan_split_pool - Splits ctrl pool between all channels.
  363. * Has to be called under ctlr lock
  364. */
  365. static void cpdma_chan_split_pool(struct cpdma_ctlr *ctlr)
  366. {
  367. struct cpdma_desc_pool *pool = ctlr->pool;
  368. struct cpdma_chan *chan;
  369. int ch_desc_num;
  370. int i;
  371. if (!ctlr->chan_num)
  372. return;
  373. /* calculate average size of pool slice */
  374. ch_desc_num = pool->num_desc / ctlr->chan_num;
  375. /* split ctlr pool */
  376. for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
  377. chan = ctlr->channels[i];
  378. if (chan)
  379. chan->desc_num = ch_desc_num;
  380. }
  381. }
  382. struct cpdma_chan *cpdma_chan_create(struct cpdma_ctlr *ctlr, int chan_num,
  383. cpdma_handler_fn handler, int rx_type)
  384. {
  385. int offset = chan_num * 4;
  386. struct cpdma_chan *chan;
  387. unsigned long flags;
  388. chan_num = rx_type ? rx_chan_num(chan_num) : tx_chan_num(chan_num);
  389. if (__chan_linear(chan_num) >= ctlr->num_chan)
  390. return NULL;
  391. chan = devm_kzalloc(ctlr->dev, sizeof(*chan), GFP_KERNEL);
  392. if (!chan)
  393. return ERR_PTR(-ENOMEM);
  394. spin_lock_irqsave(&ctlr->lock, flags);
  395. if (ctlr->channels[chan_num]) {
  396. spin_unlock_irqrestore(&ctlr->lock, flags);
  397. devm_kfree(ctlr->dev, chan);
  398. return ERR_PTR(-EBUSY);
  399. }
  400. chan->ctlr = ctlr;
  401. chan->state = CPDMA_STATE_IDLE;
  402. chan->chan_num = chan_num;
  403. chan->handler = handler;
  404. chan->desc_num = ctlr->pool->num_desc / 2;
  405. if (is_rx_chan(chan)) {
  406. chan->hdp = ctlr->params.rxhdp + offset;
  407. chan->cp = ctlr->params.rxcp + offset;
  408. chan->rxfree = ctlr->params.rxfree + offset;
  409. chan->int_set = CPDMA_RXINTMASKSET;
  410. chan->int_clear = CPDMA_RXINTMASKCLEAR;
  411. chan->td = CPDMA_RXTEARDOWN;
  412. chan->dir = DMA_FROM_DEVICE;
  413. } else {
  414. chan->hdp = ctlr->params.txhdp + offset;
  415. chan->cp = ctlr->params.txcp + offset;
  416. chan->int_set = CPDMA_TXINTMASKSET;
  417. chan->int_clear = CPDMA_TXINTMASKCLEAR;
  418. chan->td = CPDMA_TXTEARDOWN;
  419. chan->dir = DMA_TO_DEVICE;
  420. }
  421. chan->mask = BIT(chan_linear(chan));
  422. spin_lock_init(&chan->lock);
  423. ctlr->channels[chan_num] = chan;
  424. ctlr->chan_num++;
  425. cpdma_chan_split_pool(ctlr);
  426. spin_unlock_irqrestore(&ctlr->lock, flags);
  427. return chan;
  428. }
  429. EXPORT_SYMBOL_GPL(cpdma_chan_create);
  430. int cpdma_chan_get_rx_buf_num(struct cpdma_chan *chan)
  431. {
  432. unsigned long flags;
  433. int desc_num;
  434. spin_lock_irqsave(&chan->lock, flags);
  435. desc_num = chan->desc_num;
  436. spin_unlock_irqrestore(&chan->lock, flags);
  437. return desc_num;
  438. }
  439. EXPORT_SYMBOL_GPL(cpdma_chan_get_rx_buf_num);
  440. int cpdma_chan_destroy(struct cpdma_chan *chan)
  441. {
  442. struct cpdma_ctlr *ctlr;
  443. unsigned long flags;
  444. if (!chan)
  445. return -EINVAL;
  446. ctlr = chan->ctlr;
  447. spin_lock_irqsave(&ctlr->lock, flags);
  448. if (chan->state != CPDMA_STATE_IDLE)
  449. cpdma_chan_stop(chan);
  450. ctlr->channels[chan->chan_num] = NULL;
  451. ctlr->chan_num--;
  452. cpdma_chan_split_pool(ctlr);
  453. spin_unlock_irqrestore(&ctlr->lock, flags);
  454. return 0;
  455. }
  456. EXPORT_SYMBOL_GPL(cpdma_chan_destroy);
  457. int cpdma_chan_get_stats(struct cpdma_chan *chan,
  458. struct cpdma_chan_stats *stats)
  459. {
  460. unsigned long flags;
  461. if (!chan)
  462. return -EINVAL;
  463. spin_lock_irqsave(&chan->lock, flags);
  464. memcpy(stats, &chan->stats, sizeof(*stats));
  465. spin_unlock_irqrestore(&chan->lock, flags);
  466. return 0;
  467. }
  468. EXPORT_SYMBOL_GPL(cpdma_chan_get_stats);
  469. static void __cpdma_chan_submit(struct cpdma_chan *chan,
  470. struct cpdma_desc __iomem *desc)
  471. {
  472. struct cpdma_ctlr *ctlr = chan->ctlr;
  473. struct cpdma_desc __iomem *prev = chan->tail;
  474. struct cpdma_desc_pool *pool = ctlr->pool;
  475. dma_addr_t desc_dma;
  476. u32 mode;
  477. desc_dma = desc_phys(pool, desc);
  478. /* simple case - idle channel */
  479. if (!chan->head) {
  480. chan->stats.head_enqueue++;
  481. chan->head = desc;
  482. chan->tail = desc;
  483. if (chan->state == CPDMA_STATE_ACTIVE)
  484. chan_write(chan, hdp, desc_dma);
  485. return;
  486. }
  487. /* first chain the descriptor at the tail of the list */
  488. desc_write(prev, hw_next, desc_dma);
  489. chan->tail = desc;
  490. chan->stats.tail_enqueue++;
  491. /* next check if EOQ has been triggered already */
  492. mode = desc_read(prev, hw_mode);
  493. if (((mode & (CPDMA_DESC_EOQ | CPDMA_DESC_OWNER)) == CPDMA_DESC_EOQ) &&
  494. (chan->state == CPDMA_STATE_ACTIVE)) {
  495. desc_write(prev, hw_mode, mode & ~CPDMA_DESC_EOQ);
  496. chan_write(chan, hdp, desc_dma);
  497. chan->stats.misqueued++;
  498. }
  499. }
  500. int cpdma_chan_submit(struct cpdma_chan *chan, void *token, void *data,
  501. int len, int directed)
  502. {
  503. struct cpdma_ctlr *ctlr = chan->ctlr;
  504. struct cpdma_desc __iomem *desc;
  505. dma_addr_t buffer;
  506. unsigned long flags;
  507. u32 mode;
  508. int ret = 0;
  509. spin_lock_irqsave(&chan->lock, flags);
  510. if (chan->state == CPDMA_STATE_TEARDOWN) {
  511. ret = -EINVAL;
  512. goto unlock_ret;
  513. }
  514. if (chan->count >= chan->desc_num) {
  515. chan->stats.desc_alloc_fail++;
  516. ret = -ENOMEM;
  517. goto unlock_ret;
  518. }
  519. desc = cpdma_desc_alloc(ctlr->pool);
  520. if (!desc) {
  521. chan->stats.desc_alloc_fail++;
  522. ret = -ENOMEM;
  523. goto unlock_ret;
  524. }
  525. if (len < ctlr->params.min_packet_size) {
  526. len = ctlr->params.min_packet_size;
  527. chan->stats.runt_transmit_buff++;
  528. }
  529. buffer = dma_map_single(ctlr->dev, data, len, chan->dir);
  530. ret = dma_mapping_error(ctlr->dev, buffer);
  531. if (ret) {
  532. cpdma_desc_free(ctlr->pool, desc, 1);
  533. ret = -EINVAL;
  534. goto unlock_ret;
  535. }
  536. mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
  537. cpdma_desc_to_port(chan, mode, directed);
  538. desc_write(desc, hw_next, 0);
  539. desc_write(desc, hw_buffer, buffer);
  540. desc_write(desc, hw_len, len);
  541. desc_write(desc, hw_mode, mode | len);
  542. desc_write(desc, sw_token, token);
  543. desc_write(desc, sw_buffer, buffer);
  544. desc_write(desc, sw_len, len);
  545. __cpdma_chan_submit(chan, desc);
  546. if (chan->state == CPDMA_STATE_ACTIVE && chan->rxfree)
  547. chan_write(chan, rxfree, 1);
  548. chan->count++;
  549. unlock_ret:
  550. spin_unlock_irqrestore(&chan->lock, flags);
  551. return ret;
  552. }
  553. EXPORT_SYMBOL_GPL(cpdma_chan_submit);
  554. bool cpdma_check_free_tx_desc(struct cpdma_chan *chan)
  555. {
  556. struct cpdma_ctlr *ctlr = chan->ctlr;
  557. struct cpdma_desc_pool *pool = ctlr->pool;
  558. bool free_tx_desc;
  559. unsigned long flags;
  560. spin_lock_irqsave(&chan->lock, flags);
  561. free_tx_desc = (chan->count < chan->desc_num) &&
  562. gen_pool_avail(pool->gen_pool);
  563. spin_unlock_irqrestore(&chan->lock, flags);
  564. return free_tx_desc;
  565. }
  566. EXPORT_SYMBOL_GPL(cpdma_check_free_tx_desc);
  567. static void __cpdma_chan_free(struct cpdma_chan *chan,
  568. struct cpdma_desc __iomem *desc,
  569. int outlen, int status)
  570. {
  571. struct cpdma_ctlr *ctlr = chan->ctlr;
  572. struct cpdma_desc_pool *pool = ctlr->pool;
  573. dma_addr_t buff_dma;
  574. int origlen;
  575. void *token;
  576. token = (void *)desc_read(desc, sw_token);
  577. buff_dma = desc_read(desc, sw_buffer);
  578. origlen = desc_read(desc, sw_len);
  579. dma_unmap_single(ctlr->dev, buff_dma, origlen, chan->dir);
  580. cpdma_desc_free(pool, desc, 1);
  581. (*chan->handler)(token, outlen, status);
  582. }
  583. static int __cpdma_chan_process(struct cpdma_chan *chan)
  584. {
  585. struct cpdma_ctlr *ctlr = chan->ctlr;
  586. struct cpdma_desc __iomem *desc;
  587. int status, outlen;
  588. int cb_status = 0;
  589. struct cpdma_desc_pool *pool = ctlr->pool;
  590. dma_addr_t desc_dma;
  591. unsigned long flags;
  592. spin_lock_irqsave(&chan->lock, flags);
  593. desc = chan->head;
  594. if (!desc) {
  595. chan->stats.empty_dequeue++;
  596. status = -ENOENT;
  597. goto unlock_ret;
  598. }
  599. desc_dma = desc_phys(pool, desc);
  600. status = __raw_readl(&desc->hw_mode);
  601. outlen = status & 0x7ff;
  602. if (status & CPDMA_DESC_OWNER) {
  603. chan->stats.busy_dequeue++;
  604. status = -EBUSY;
  605. goto unlock_ret;
  606. }
  607. if (status & CPDMA_DESC_PASS_CRC)
  608. outlen -= CPDMA_DESC_CRC_LEN;
  609. status = status & (CPDMA_DESC_EOQ | CPDMA_DESC_TD_COMPLETE |
  610. CPDMA_DESC_PORT_MASK);
  611. chan->head = desc_from_phys(pool, desc_read(desc, hw_next));
  612. chan_write(chan, cp, desc_dma);
  613. chan->count--;
  614. chan->stats.good_dequeue++;
  615. if (status & CPDMA_DESC_EOQ) {
  616. chan->stats.requeue++;
  617. chan_write(chan, hdp, desc_phys(pool, chan->head));
  618. }
  619. spin_unlock_irqrestore(&chan->lock, flags);
  620. if (unlikely(status & CPDMA_DESC_TD_COMPLETE))
  621. cb_status = -ENOSYS;
  622. else
  623. cb_status = status;
  624. __cpdma_chan_free(chan, desc, outlen, cb_status);
  625. return status;
  626. unlock_ret:
  627. spin_unlock_irqrestore(&chan->lock, flags);
  628. return status;
  629. }
  630. int cpdma_chan_process(struct cpdma_chan *chan, int quota)
  631. {
  632. int used = 0, ret = 0;
  633. if (chan->state != CPDMA_STATE_ACTIVE)
  634. return -EINVAL;
  635. while (used < quota) {
  636. ret = __cpdma_chan_process(chan);
  637. if (ret < 0)
  638. break;
  639. used++;
  640. }
  641. return used;
  642. }
  643. EXPORT_SYMBOL_GPL(cpdma_chan_process);
  644. int cpdma_chan_start(struct cpdma_chan *chan)
  645. {
  646. struct cpdma_ctlr *ctlr = chan->ctlr;
  647. struct cpdma_desc_pool *pool = ctlr->pool;
  648. unsigned long flags;
  649. spin_lock_irqsave(&chan->lock, flags);
  650. if (chan->state != CPDMA_STATE_IDLE) {
  651. spin_unlock_irqrestore(&chan->lock, flags);
  652. return -EBUSY;
  653. }
  654. if (ctlr->state != CPDMA_STATE_ACTIVE) {
  655. spin_unlock_irqrestore(&chan->lock, flags);
  656. return -EINVAL;
  657. }
  658. dma_reg_write(ctlr, chan->int_set, chan->mask);
  659. chan->state = CPDMA_STATE_ACTIVE;
  660. if (chan->head) {
  661. chan_write(chan, hdp, desc_phys(pool, chan->head));
  662. if (chan->rxfree)
  663. chan_write(chan, rxfree, chan->count);
  664. }
  665. spin_unlock_irqrestore(&chan->lock, flags);
  666. return 0;
  667. }
  668. EXPORT_SYMBOL_GPL(cpdma_chan_start);
  669. int cpdma_chan_stop(struct cpdma_chan *chan)
  670. {
  671. struct cpdma_ctlr *ctlr = chan->ctlr;
  672. struct cpdma_desc_pool *pool = ctlr->pool;
  673. unsigned long flags;
  674. int ret;
  675. unsigned timeout;
  676. spin_lock_irqsave(&chan->lock, flags);
  677. if (chan->state == CPDMA_STATE_TEARDOWN) {
  678. spin_unlock_irqrestore(&chan->lock, flags);
  679. return -EINVAL;
  680. }
  681. chan->state = CPDMA_STATE_TEARDOWN;
  682. dma_reg_write(ctlr, chan->int_clear, chan->mask);
  683. /* trigger teardown */
  684. dma_reg_write(ctlr, chan->td, chan_linear(chan));
  685. /* wait for teardown complete */
  686. timeout = 100 * 100; /* 100 ms */
  687. while (timeout) {
  688. u32 cp = chan_read(chan, cp);
  689. if ((cp & CPDMA_TEARDOWN_VALUE) == CPDMA_TEARDOWN_VALUE)
  690. break;
  691. udelay(10);
  692. timeout--;
  693. }
  694. WARN_ON(!timeout);
  695. chan_write(chan, cp, CPDMA_TEARDOWN_VALUE);
  696. /* handle completed packets */
  697. spin_unlock_irqrestore(&chan->lock, flags);
  698. do {
  699. ret = __cpdma_chan_process(chan);
  700. if (ret < 0)
  701. break;
  702. } while ((ret & CPDMA_DESC_TD_COMPLETE) == 0);
  703. spin_lock_irqsave(&chan->lock, flags);
  704. /* remaining packets haven't been tx/rx'ed, clean them up */
  705. while (chan->head) {
  706. struct cpdma_desc __iomem *desc = chan->head;
  707. dma_addr_t next_dma;
  708. next_dma = desc_read(desc, hw_next);
  709. chan->head = desc_from_phys(pool, next_dma);
  710. chan->count--;
  711. chan->stats.teardown_dequeue++;
  712. /* issue callback without locks held */
  713. spin_unlock_irqrestore(&chan->lock, flags);
  714. __cpdma_chan_free(chan, desc, 0, -ENOSYS);
  715. spin_lock_irqsave(&chan->lock, flags);
  716. }
  717. chan->state = CPDMA_STATE_IDLE;
  718. spin_unlock_irqrestore(&chan->lock, flags);
  719. return 0;
  720. }
  721. EXPORT_SYMBOL_GPL(cpdma_chan_stop);
  722. int cpdma_chan_int_ctrl(struct cpdma_chan *chan, bool enable)
  723. {
  724. unsigned long flags;
  725. spin_lock_irqsave(&chan->lock, flags);
  726. if (chan->state != CPDMA_STATE_ACTIVE) {
  727. spin_unlock_irqrestore(&chan->lock, flags);
  728. return -EINVAL;
  729. }
  730. dma_reg_write(chan->ctlr, enable ? chan->int_set : chan->int_clear,
  731. chan->mask);
  732. spin_unlock_irqrestore(&chan->lock, flags);
  733. return 0;
  734. }
  735. struct cpdma_control_info {
  736. u32 reg;
  737. u32 shift, mask;
  738. int access;
  739. #define ACCESS_RO BIT(0)
  740. #define ACCESS_WO BIT(1)
  741. #define ACCESS_RW (ACCESS_RO | ACCESS_WO)
  742. };
  743. static struct cpdma_control_info controls[] = {
  744. [CPDMA_CMD_IDLE] = {CPDMA_DMACONTROL, 3, 1, ACCESS_WO},
  745. [CPDMA_COPY_ERROR_FRAMES] = {CPDMA_DMACONTROL, 4, 1, ACCESS_RW},
  746. [CPDMA_RX_OFF_LEN_UPDATE] = {CPDMA_DMACONTROL, 2, 1, ACCESS_RW},
  747. [CPDMA_RX_OWNERSHIP_FLIP] = {CPDMA_DMACONTROL, 1, 1, ACCESS_RW},
  748. [CPDMA_TX_PRIO_FIXED] = {CPDMA_DMACONTROL, 0, 1, ACCESS_RW},
  749. [CPDMA_STAT_IDLE] = {CPDMA_DMASTATUS, 31, 1, ACCESS_RO},
  750. [CPDMA_STAT_TX_ERR_CODE] = {CPDMA_DMASTATUS, 20, 0xf, ACCESS_RW},
  751. [CPDMA_STAT_TX_ERR_CHAN] = {CPDMA_DMASTATUS, 16, 0x7, ACCESS_RW},
  752. [CPDMA_STAT_RX_ERR_CODE] = {CPDMA_DMASTATUS, 12, 0xf, ACCESS_RW},
  753. [CPDMA_STAT_RX_ERR_CHAN] = {CPDMA_DMASTATUS, 8, 0x7, ACCESS_RW},
  754. [CPDMA_RX_BUFFER_OFFSET] = {CPDMA_RXBUFFOFS, 0, 0xffff, ACCESS_RW},
  755. };
  756. int cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
  757. {
  758. unsigned long flags;
  759. struct cpdma_control_info *info = &controls[control];
  760. int ret;
  761. spin_lock_irqsave(&ctlr->lock, flags);
  762. ret = -ENOTSUPP;
  763. if (!ctlr->params.has_ext_regs)
  764. goto unlock_ret;
  765. ret = -EINVAL;
  766. if (ctlr->state != CPDMA_STATE_ACTIVE)
  767. goto unlock_ret;
  768. ret = -ENOENT;
  769. if (control < 0 || control >= ARRAY_SIZE(controls))
  770. goto unlock_ret;
  771. ret = -EPERM;
  772. if ((info->access & ACCESS_RO) != ACCESS_RO)
  773. goto unlock_ret;
  774. ret = (dma_reg_read(ctlr, info->reg) >> info->shift) & info->mask;
  775. unlock_ret:
  776. spin_unlock_irqrestore(&ctlr->lock, flags);
  777. return ret;
  778. }
  779. int cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
  780. {
  781. unsigned long flags;
  782. struct cpdma_control_info *info = &controls[control];
  783. int ret;
  784. u32 val;
  785. spin_lock_irqsave(&ctlr->lock, flags);
  786. ret = -ENOTSUPP;
  787. if (!ctlr->params.has_ext_regs)
  788. goto unlock_ret;
  789. ret = -EINVAL;
  790. if (ctlr->state != CPDMA_STATE_ACTIVE)
  791. goto unlock_ret;
  792. ret = -ENOENT;
  793. if (control < 0 || control >= ARRAY_SIZE(controls))
  794. goto unlock_ret;
  795. ret = -EPERM;
  796. if ((info->access & ACCESS_WO) != ACCESS_WO)
  797. goto unlock_ret;
  798. val = dma_reg_read(ctlr, info->reg);
  799. val &= ~(info->mask << info->shift);
  800. val |= (value & info->mask) << info->shift;
  801. dma_reg_write(ctlr, info->reg, val);
  802. ret = 0;
  803. unlock_ret:
  804. spin_unlock_irqrestore(&ctlr->lock, flags);
  805. return ret;
  806. }
  807. EXPORT_SYMBOL_GPL(cpdma_control_set);
  808. MODULE_LICENSE("GPL");