knav_dma.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. * Copyright (C) 2014 Texas Instruments Incorporated
  3. * Authors: Santosh Shilimkar <santosh.shilimkar@ti.com>
  4. * Sandeep Nair <sandeep_n@ti.com>
  5. * Cyril Chemparathy <cyril@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation version 2.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/io.h>
  17. #include <linux/sched.h>
  18. #include <linux/module.h>
  19. #include <linux/dma-direction.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/of_dma.h>
  23. #include <linux/of_address.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/soc/ti/knav_dma.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/seq_file.h>
  28. #define REG_MASK 0xffffffff
  29. #define DMA_LOOPBACK BIT(31)
  30. #define DMA_ENABLE BIT(31)
  31. #define DMA_TEARDOWN BIT(30)
  32. #define DMA_TX_FILT_PSWORDS BIT(29)
  33. #define DMA_TX_FILT_EINFO BIT(30)
  34. #define DMA_TX_PRIO_SHIFT 0
  35. #define DMA_RX_PRIO_SHIFT 16
  36. #define DMA_PRIO_MASK GENMASK(3, 0)
  37. #define DMA_PRIO_DEFAULT 0
  38. #define DMA_RX_TIMEOUT_DEFAULT 17500 /* cycles */
  39. #define DMA_RX_TIMEOUT_MASK GENMASK(16, 0)
  40. #define DMA_RX_TIMEOUT_SHIFT 0
  41. #define CHAN_HAS_EPIB BIT(30)
  42. #define CHAN_HAS_PSINFO BIT(29)
  43. #define CHAN_ERR_RETRY BIT(28)
  44. #define CHAN_PSINFO_AT_SOP BIT(25)
  45. #define CHAN_SOP_OFF_SHIFT 16
  46. #define CHAN_SOP_OFF_MASK GENMASK(9, 0)
  47. #define DESC_TYPE_SHIFT 26
  48. #define DESC_TYPE_MASK GENMASK(2, 0)
  49. /*
  50. * QMGR & QNUM together make up 14 bits with QMGR as the 2 MSb's in the logical
  51. * navigator cloud mapping scheme.
  52. * using the 14bit physical queue numbers directly maps into this scheme.
  53. */
  54. #define CHAN_QNUM_MASK GENMASK(14, 0)
  55. #define DMA_MAX_QMS 4
  56. #define DMA_TIMEOUT 1 /* msecs */
  57. #define DMA_INVALID_ID 0xffff
  58. struct reg_global {
  59. u32 revision;
  60. u32 perf_control;
  61. u32 emulation_control;
  62. u32 priority_control;
  63. u32 qm_base_address[DMA_MAX_QMS];
  64. };
  65. struct reg_chan {
  66. u32 control;
  67. u32 mode;
  68. u32 __rsvd[6];
  69. };
  70. struct reg_tx_sched {
  71. u32 prio;
  72. };
  73. struct reg_rx_flow {
  74. u32 control;
  75. u32 tags;
  76. u32 tag_sel;
  77. u32 fdq_sel[2];
  78. u32 thresh[3];
  79. };
  80. struct knav_dma_pool_device {
  81. struct device *dev;
  82. struct list_head list;
  83. };
  84. struct knav_dma_device {
  85. bool loopback, enable_all;
  86. unsigned tx_priority, rx_priority, rx_timeout;
  87. unsigned logical_queue_managers;
  88. unsigned qm_base_address[DMA_MAX_QMS];
  89. struct reg_global __iomem *reg_global;
  90. struct reg_chan __iomem *reg_tx_chan;
  91. struct reg_rx_flow __iomem *reg_rx_flow;
  92. struct reg_chan __iomem *reg_rx_chan;
  93. struct reg_tx_sched __iomem *reg_tx_sched;
  94. unsigned max_rx_chan, max_tx_chan;
  95. unsigned max_rx_flow;
  96. char name[32];
  97. atomic_t ref_count;
  98. struct list_head list;
  99. struct list_head chan_list;
  100. spinlock_t lock;
  101. };
  102. struct knav_dma_chan {
  103. enum dma_transfer_direction direction;
  104. struct knav_dma_device *dma;
  105. atomic_t ref_count;
  106. /* registers */
  107. struct reg_chan __iomem *reg_chan;
  108. struct reg_tx_sched __iomem *reg_tx_sched;
  109. struct reg_rx_flow __iomem *reg_rx_flow;
  110. /* configuration stuff */
  111. unsigned channel, flow;
  112. struct knav_dma_cfg cfg;
  113. struct list_head list;
  114. spinlock_t lock;
  115. };
  116. #define chan_number(ch) ((ch->direction == DMA_MEM_TO_DEV) ? \
  117. ch->channel : ch->flow)
  118. static struct knav_dma_pool_device *kdev;
  119. static bool check_config(struct knav_dma_chan *chan, struct knav_dma_cfg *cfg)
  120. {
  121. if (!memcmp(&chan->cfg, cfg, sizeof(*cfg)))
  122. return true;
  123. else
  124. return false;
  125. }
  126. static int chan_start(struct knav_dma_chan *chan,
  127. struct knav_dma_cfg *cfg)
  128. {
  129. u32 v = 0;
  130. spin_lock(&chan->lock);
  131. if ((chan->direction == DMA_MEM_TO_DEV) && chan->reg_chan) {
  132. if (cfg->u.tx.filt_pswords)
  133. v |= DMA_TX_FILT_PSWORDS;
  134. if (cfg->u.tx.filt_einfo)
  135. v |= DMA_TX_FILT_EINFO;
  136. writel_relaxed(v, &chan->reg_chan->mode);
  137. writel_relaxed(DMA_ENABLE, &chan->reg_chan->control);
  138. }
  139. if (chan->reg_tx_sched)
  140. writel_relaxed(cfg->u.tx.priority, &chan->reg_tx_sched->prio);
  141. if (chan->reg_rx_flow) {
  142. v = 0;
  143. if (cfg->u.rx.einfo_present)
  144. v |= CHAN_HAS_EPIB;
  145. if (cfg->u.rx.psinfo_present)
  146. v |= CHAN_HAS_PSINFO;
  147. if (cfg->u.rx.err_mode == DMA_RETRY)
  148. v |= CHAN_ERR_RETRY;
  149. v |= (cfg->u.rx.desc_type & DESC_TYPE_MASK) << DESC_TYPE_SHIFT;
  150. if (cfg->u.rx.psinfo_at_sop)
  151. v |= CHAN_PSINFO_AT_SOP;
  152. v |= (cfg->u.rx.sop_offset & CHAN_SOP_OFF_MASK)
  153. << CHAN_SOP_OFF_SHIFT;
  154. v |= cfg->u.rx.dst_q & CHAN_QNUM_MASK;
  155. writel_relaxed(v, &chan->reg_rx_flow->control);
  156. writel_relaxed(0, &chan->reg_rx_flow->tags);
  157. writel_relaxed(0, &chan->reg_rx_flow->tag_sel);
  158. v = cfg->u.rx.fdq[0] << 16;
  159. v |= cfg->u.rx.fdq[1] & CHAN_QNUM_MASK;
  160. writel_relaxed(v, &chan->reg_rx_flow->fdq_sel[0]);
  161. v = cfg->u.rx.fdq[2] << 16;
  162. v |= cfg->u.rx.fdq[3] & CHAN_QNUM_MASK;
  163. writel_relaxed(v, &chan->reg_rx_flow->fdq_sel[1]);
  164. writel_relaxed(0, &chan->reg_rx_flow->thresh[0]);
  165. writel_relaxed(0, &chan->reg_rx_flow->thresh[1]);
  166. writel_relaxed(0, &chan->reg_rx_flow->thresh[2]);
  167. }
  168. /* Keep a copy of the cfg */
  169. memcpy(&chan->cfg, cfg, sizeof(*cfg));
  170. spin_unlock(&chan->lock);
  171. return 0;
  172. }
  173. static int chan_teardown(struct knav_dma_chan *chan)
  174. {
  175. unsigned long end, value;
  176. if (!chan->reg_chan)
  177. return 0;
  178. /* indicate teardown */
  179. writel_relaxed(DMA_TEARDOWN, &chan->reg_chan->control);
  180. /* wait for the dma to shut itself down */
  181. end = jiffies + msecs_to_jiffies(DMA_TIMEOUT);
  182. do {
  183. value = readl_relaxed(&chan->reg_chan->control);
  184. if ((value & DMA_ENABLE) == 0)
  185. break;
  186. } while (time_after(end, jiffies));
  187. if (readl_relaxed(&chan->reg_chan->control) & DMA_ENABLE) {
  188. dev_err(kdev->dev, "timeout waiting for teardown\n");
  189. return -ETIMEDOUT;
  190. }
  191. return 0;
  192. }
  193. static void chan_stop(struct knav_dma_chan *chan)
  194. {
  195. spin_lock(&chan->lock);
  196. if (chan->reg_rx_flow) {
  197. /* first detach fdqs, starve out the flow */
  198. writel_relaxed(0, &chan->reg_rx_flow->fdq_sel[0]);
  199. writel_relaxed(0, &chan->reg_rx_flow->fdq_sel[1]);
  200. writel_relaxed(0, &chan->reg_rx_flow->thresh[0]);
  201. writel_relaxed(0, &chan->reg_rx_flow->thresh[1]);
  202. writel_relaxed(0, &chan->reg_rx_flow->thresh[2]);
  203. }
  204. /* teardown the dma channel */
  205. chan_teardown(chan);
  206. /* then disconnect the completion side */
  207. if (chan->reg_rx_flow) {
  208. writel_relaxed(0, &chan->reg_rx_flow->control);
  209. writel_relaxed(0, &chan->reg_rx_flow->tags);
  210. writel_relaxed(0, &chan->reg_rx_flow->tag_sel);
  211. }
  212. memset(&chan->cfg, 0, sizeof(struct knav_dma_cfg));
  213. spin_unlock(&chan->lock);
  214. dev_dbg(kdev->dev, "channel stopped\n");
  215. }
  216. static void dma_hw_enable_all(struct knav_dma_device *dma)
  217. {
  218. int i;
  219. for (i = 0; i < dma->max_tx_chan; i++) {
  220. writel_relaxed(0, &dma->reg_tx_chan[i].mode);
  221. writel_relaxed(DMA_ENABLE, &dma->reg_tx_chan[i].control);
  222. }
  223. }
  224. static void knav_dma_hw_init(struct knav_dma_device *dma)
  225. {
  226. unsigned v;
  227. int i;
  228. spin_lock(&dma->lock);
  229. v = dma->loopback ? DMA_LOOPBACK : 0;
  230. writel_relaxed(v, &dma->reg_global->emulation_control);
  231. v = readl_relaxed(&dma->reg_global->perf_control);
  232. v |= ((dma->rx_timeout & DMA_RX_TIMEOUT_MASK) << DMA_RX_TIMEOUT_SHIFT);
  233. writel_relaxed(v, &dma->reg_global->perf_control);
  234. v = ((dma->tx_priority << DMA_TX_PRIO_SHIFT) |
  235. (dma->rx_priority << DMA_RX_PRIO_SHIFT));
  236. writel_relaxed(v, &dma->reg_global->priority_control);
  237. /* Always enable all Rx channels. Rx paths are managed using flows */
  238. for (i = 0; i < dma->max_rx_chan; i++)
  239. writel_relaxed(DMA_ENABLE, &dma->reg_rx_chan[i].control);
  240. for (i = 0; i < dma->logical_queue_managers; i++)
  241. writel_relaxed(dma->qm_base_address[i],
  242. &dma->reg_global->qm_base_address[i]);
  243. spin_unlock(&dma->lock);
  244. }
  245. static void knav_dma_hw_destroy(struct knav_dma_device *dma)
  246. {
  247. int i;
  248. unsigned v;
  249. spin_lock(&dma->lock);
  250. v = ~DMA_ENABLE & REG_MASK;
  251. for (i = 0; i < dma->max_rx_chan; i++)
  252. writel_relaxed(v, &dma->reg_rx_chan[i].control);
  253. for (i = 0; i < dma->max_tx_chan; i++)
  254. writel_relaxed(v, &dma->reg_tx_chan[i].control);
  255. spin_unlock(&dma->lock);
  256. }
  257. static void dma_debug_show_channels(struct seq_file *s,
  258. struct knav_dma_chan *chan)
  259. {
  260. int i;
  261. seq_printf(s, "\t%s %d:\t",
  262. ((chan->direction == DMA_MEM_TO_DEV) ? "tx chan" : "rx flow"),
  263. chan_number(chan));
  264. if (chan->direction == DMA_MEM_TO_DEV) {
  265. seq_printf(s, "einfo - %d, pswords - %d, priority - %d\n",
  266. chan->cfg.u.tx.filt_einfo,
  267. chan->cfg.u.tx.filt_pswords,
  268. chan->cfg.u.tx.priority);
  269. } else {
  270. seq_printf(s, "einfo - %d, psinfo - %d, desc_type - %d\n",
  271. chan->cfg.u.rx.einfo_present,
  272. chan->cfg.u.rx.psinfo_present,
  273. chan->cfg.u.rx.desc_type);
  274. seq_printf(s, "\t\t\tdst_q: [%d], thresh: %d fdq: ",
  275. chan->cfg.u.rx.dst_q,
  276. chan->cfg.u.rx.thresh);
  277. for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN; i++)
  278. seq_printf(s, "[%d]", chan->cfg.u.rx.fdq[i]);
  279. seq_printf(s, "\n");
  280. }
  281. }
  282. static void dma_debug_show_devices(struct seq_file *s,
  283. struct knav_dma_device *dma)
  284. {
  285. struct knav_dma_chan *chan;
  286. list_for_each_entry(chan, &dma->chan_list, list) {
  287. if (atomic_read(&chan->ref_count))
  288. dma_debug_show_channels(s, chan);
  289. }
  290. }
  291. static int dma_debug_show(struct seq_file *s, void *v)
  292. {
  293. struct knav_dma_device *dma;
  294. list_for_each_entry(dma, &kdev->list, list) {
  295. if (atomic_read(&dma->ref_count)) {
  296. seq_printf(s, "%s : max_tx_chan: (%d), max_rx_flows: (%d)\n",
  297. dma->name, dma->max_tx_chan, dma->max_rx_flow);
  298. dma_debug_show_devices(s, dma);
  299. }
  300. }
  301. return 0;
  302. }
  303. static int knav_dma_debug_open(struct inode *inode, struct file *file)
  304. {
  305. return single_open(file, dma_debug_show, NULL);
  306. }
  307. static const struct file_operations knav_dma_debug_ops = {
  308. .open = knav_dma_debug_open,
  309. .read = seq_read,
  310. .llseek = seq_lseek,
  311. .release = single_release,
  312. };
  313. static int of_channel_match_helper(struct device_node *np, const char *name,
  314. const char **dma_instance)
  315. {
  316. struct of_phandle_args args;
  317. struct device_node *dma_node;
  318. int index;
  319. dma_node = of_parse_phandle(np, "ti,navigator-dmas", 0);
  320. if (!dma_node)
  321. return -ENODEV;
  322. *dma_instance = dma_node->name;
  323. index = of_property_match_string(np, "ti,navigator-dma-names", name);
  324. if (index < 0) {
  325. dev_err(kdev->dev, "No 'ti,navigator-dma-names' propery\n");
  326. return -ENODEV;
  327. }
  328. if (of_parse_phandle_with_fixed_args(np, "ti,navigator-dmas",
  329. 1, index, &args)) {
  330. dev_err(kdev->dev, "Missing the pahndle args name %s\n", name);
  331. return -ENODEV;
  332. }
  333. if (args.args[0] < 0) {
  334. dev_err(kdev->dev, "Missing args for %s\n", name);
  335. return -ENODEV;
  336. }
  337. return args.args[0];
  338. }
  339. /**
  340. * knav_dma_open_channel() - try to setup an exclusive slave channel
  341. * @dev: pointer to client device structure
  342. * @name: slave channel name
  343. * @config: dma configuration parameters
  344. *
  345. * Returns pointer to appropriate DMA channel on success or NULL.
  346. */
  347. void *knav_dma_open_channel(struct device *dev, const char *name,
  348. struct knav_dma_cfg *config)
  349. {
  350. struct knav_dma_chan *chan;
  351. struct knav_dma_device *dma;
  352. bool found = false;
  353. int chan_num = -1;
  354. const char *instance;
  355. if (!kdev) {
  356. pr_err("keystone-navigator-dma driver not registered\n");
  357. return (void *)-EINVAL;
  358. }
  359. chan_num = of_channel_match_helper(dev->of_node, name, &instance);
  360. if (chan_num < 0) {
  361. dev_err(kdev->dev, "No DMA instace with name %s\n", name);
  362. return (void *)-EINVAL;
  363. }
  364. dev_dbg(kdev->dev, "initializing %s channel %d from DMA %s\n",
  365. config->direction == DMA_MEM_TO_DEV ? "transmit" :
  366. config->direction == DMA_DEV_TO_MEM ? "receive" :
  367. "unknown", chan_num, instance);
  368. if (config->direction != DMA_MEM_TO_DEV &&
  369. config->direction != DMA_DEV_TO_MEM) {
  370. dev_err(kdev->dev, "bad direction\n");
  371. return (void *)-EINVAL;
  372. }
  373. /* Look for correct dma instance */
  374. list_for_each_entry(dma, &kdev->list, list) {
  375. if (!strcmp(dma->name, instance)) {
  376. found = true;
  377. break;
  378. }
  379. }
  380. if (!found) {
  381. dev_err(kdev->dev, "No DMA instace with name %s\n", instance);
  382. return (void *)-EINVAL;
  383. }
  384. /* Look for correct dma channel from dma instance */
  385. found = false;
  386. list_for_each_entry(chan, &dma->chan_list, list) {
  387. if (config->direction == DMA_MEM_TO_DEV) {
  388. if (chan->channel == chan_num) {
  389. found = true;
  390. break;
  391. }
  392. } else {
  393. if (chan->flow == chan_num) {
  394. found = true;
  395. break;
  396. }
  397. }
  398. }
  399. if (!found) {
  400. dev_err(kdev->dev, "channel %d is not in DMA %s\n",
  401. chan_num, instance);
  402. return (void *)-EINVAL;
  403. }
  404. if (atomic_read(&chan->ref_count) >= 1) {
  405. if (!check_config(chan, config)) {
  406. dev_err(kdev->dev, "channel %d config miss-match\n",
  407. chan_num);
  408. return (void *)-EINVAL;
  409. }
  410. }
  411. if (atomic_inc_return(&chan->dma->ref_count) <= 1)
  412. knav_dma_hw_init(chan->dma);
  413. if (atomic_inc_return(&chan->ref_count) <= 1)
  414. chan_start(chan, config);
  415. dev_dbg(kdev->dev, "channel %d opened from DMA %s\n",
  416. chan_num, instance);
  417. return chan;
  418. }
  419. EXPORT_SYMBOL_GPL(knav_dma_open_channel);
  420. /**
  421. * knav_dma_close_channel() - Destroy a dma channel
  422. *
  423. * channel: dma channel handle
  424. *
  425. */
  426. void knav_dma_close_channel(void *channel)
  427. {
  428. struct knav_dma_chan *chan = channel;
  429. if (!kdev) {
  430. pr_err("keystone-navigator-dma driver not registered\n");
  431. return;
  432. }
  433. if (atomic_dec_return(&chan->ref_count) <= 0)
  434. chan_stop(chan);
  435. if (atomic_dec_return(&chan->dma->ref_count) <= 0)
  436. knav_dma_hw_destroy(chan->dma);
  437. dev_dbg(kdev->dev, "channel %d or flow %d closed from DMA %s\n",
  438. chan->channel, chan->flow, chan->dma->name);
  439. }
  440. EXPORT_SYMBOL_GPL(knav_dma_close_channel);
  441. static void __iomem *pktdma_get_regs(struct knav_dma_device *dma,
  442. struct device_node *node,
  443. unsigned index, resource_size_t *_size)
  444. {
  445. struct device *dev = kdev->dev;
  446. struct resource res;
  447. void __iomem *regs;
  448. int ret;
  449. ret = of_address_to_resource(node, index, &res);
  450. if (ret) {
  451. dev_err(dev, "Can't translate of node(%s) address for index(%d)\n",
  452. node->name, index);
  453. return ERR_PTR(ret);
  454. }
  455. regs = devm_ioremap_resource(kdev->dev, &res);
  456. if (IS_ERR(regs))
  457. dev_err(dev, "Failed to map register base for index(%d) node(%s)\n",
  458. index, node->name);
  459. if (_size)
  460. *_size = resource_size(&res);
  461. return regs;
  462. }
  463. static int pktdma_init_rx_chan(struct knav_dma_chan *chan, u32 flow)
  464. {
  465. struct knav_dma_device *dma = chan->dma;
  466. chan->flow = flow;
  467. chan->reg_rx_flow = dma->reg_rx_flow + flow;
  468. chan->channel = DMA_INVALID_ID;
  469. dev_dbg(kdev->dev, "rx flow(%d) (%p)\n", chan->flow, chan->reg_rx_flow);
  470. return 0;
  471. }
  472. static int pktdma_init_tx_chan(struct knav_dma_chan *chan, u32 channel)
  473. {
  474. struct knav_dma_device *dma = chan->dma;
  475. chan->channel = channel;
  476. chan->reg_chan = dma->reg_tx_chan + channel;
  477. chan->reg_tx_sched = dma->reg_tx_sched + channel;
  478. chan->flow = DMA_INVALID_ID;
  479. dev_dbg(kdev->dev, "tx channel(%d) (%p)\n", chan->channel, chan->reg_chan);
  480. return 0;
  481. }
  482. static int pktdma_init_chan(struct knav_dma_device *dma,
  483. enum dma_transfer_direction dir,
  484. unsigned chan_num)
  485. {
  486. struct device *dev = kdev->dev;
  487. struct knav_dma_chan *chan;
  488. int ret = -EINVAL;
  489. chan = devm_kzalloc(dev, sizeof(*chan), GFP_KERNEL);
  490. if (!chan)
  491. return -ENOMEM;
  492. INIT_LIST_HEAD(&chan->list);
  493. chan->dma = dma;
  494. chan->direction = DMA_NONE;
  495. atomic_set(&chan->ref_count, 0);
  496. spin_lock_init(&chan->lock);
  497. if (dir == DMA_MEM_TO_DEV) {
  498. chan->direction = dir;
  499. ret = pktdma_init_tx_chan(chan, chan_num);
  500. } else if (dir == DMA_DEV_TO_MEM) {
  501. chan->direction = dir;
  502. ret = pktdma_init_rx_chan(chan, chan_num);
  503. } else {
  504. dev_err(dev, "channel(%d) direction unknown\n", chan_num);
  505. }
  506. list_add_tail(&chan->list, &dma->chan_list);
  507. return ret;
  508. }
  509. static int dma_init(struct device_node *cloud, struct device_node *dma_node)
  510. {
  511. unsigned max_tx_chan, max_rx_chan, max_rx_flow, max_tx_sched;
  512. struct device_node *node = dma_node;
  513. struct knav_dma_device *dma;
  514. int ret, len, num_chan = 0;
  515. resource_size_t size;
  516. u32 timeout;
  517. u32 i;
  518. dma = devm_kzalloc(kdev->dev, sizeof(*dma), GFP_KERNEL);
  519. if (!dma) {
  520. dev_err(kdev->dev, "could not allocate driver mem\n");
  521. return -ENOMEM;
  522. }
  523. INIT_LIST_HEAD(&dma->list);
  524. INIT_LIST_HEAD(&dma->chan_list);
  525. if (!of_find_property(cloud, "ti,navigator-cloud-address", &len)) {
  526. dev_err(kdev->dev, "unspecified navigator cloud addresses\n");
  527. return -ENODEV;
  528. }
  529. dma->logical_queue_managers = len / sizeof(u32);
  530. if (dma->logical_queue_managers > DMA_MAX_QMS) {
  531. dev_warn(kdev->dev, "too many queue mgrs(>%d) rest ignored\n",
  532. dma->logical_queue_managers);
  533. dma->logical_queue_managers = DMA_MAX_QMS;
  534. }
  535. ret = of_property_read_u32_array(cloud, "ti,navigator-cloud-address",
  536. dma->qm_base_address,
  537. dma->logical_queue_managers);
  538. if (ret) {
  539. dev_err(kdev->dev, "invalid navigator cloud addresses\n");
  540. return -ENODEV;
  541. }
  542. dma->reg_global = pktdma_get_regs(dma, node, 0, &size);
  543. if (!dma->reg_global)
  544. return -ENODEV;
  545. if (size < sizeof(struct reg_global)) {
  546. dev_err(kdev->dev, "bad size %pa for global regs\n", &size);
  547. return -ENODEV;
  548. }
  549. dma->reg_tx_chan = pktdma_get_regs(dma, node, 1, &size);
  550. if (!dma->reg_tx_chan)
  551. return -ENODEV;
  552. max_tx_chan = size / sizeof(struct reg_chan);
  553. dma->reg_rx_chan = pktdma_get_regs(dma, node, 2, &size);
  554. if (!dma->reg_rx_chan)
  555. return -ENODEV;
  556. max_rx_chan = size / sizeof(struct reg_chan);
  557. dma->reg_tx_sched = pktdma_get_regs(dma, node, 3, &size);
  558. if (!dma->reg_tx_sched)
  559. return -ENODEV;
  560. max_tx_sched = size / sizeof(struct reg_tx_sched);
  561. dma->reg_rx_flow = pktdma_get_regs(dma, node, 4, &size);
  562. if (!dma->reg_rx_flow)
  563. return -ENODEV;
  564. max_rx_flow = size / sizeof(struct reg_rx_flow);
  565. dma->rx_priority = DMA_PRIO_DEFAULT;
  566. dma->tx_priority = DMA_PRIO_DEFAULT;
  567. dma->enable_all = (of_get_property(node, "ti,enable-all", NULL) != NULL);
  568. dma->loopback = (of_get_property(node, "ti,loop-back", NULL) != NULL);
  569. ret = of_property_read_u32(node, "ti,rx-retry-timeout", &timeout);
  570. if (ret < 0) {
  571. dev_dbg(kdev->dev, "unspecified rx timeout using value %d\n",
  572. DMA_RX_TIMEOUT_DEFAULT);
  573. timeout = DMA_RX_TIMEOUT_DEFAULT;
  574. }
  575. dma->rx_timeout = timeout;
  576. dma->max_rx_chan = max_rx_chan;
  577. dma->max_rx_flow = max_rx_flow;
  578. dma->max_tx_chan = min(max_tx_chan, max_tx_sched);
  579. atomic_set(&dma->ref_count, 0);
  580. strcpy(dma->name, node->name);
  581. spin_lock_init(&dma->lock);
  582. for (i = 0; i < dma->max_tx_chan; i++) {
  583. if (pktdma_init_chan(dma, DMA_MEM_TO_DEV, i) >= 0)
  584. num_chan++;
  585. }
  586. for (i = 0; i < dma->max_rx_flow; i++) {
  587. if (pktdma_init_chan(dma, DMA_DEV_TO_MEM, i) >= 0)
  588. num_chan++;
  589. }
  590. list_add_tail(&dma->list, &kdev->list);
  591. /*
  592. * For DSP software usecases or userpace transport software, setup all
  593. * the DMA hardware resources.
  594. */
  595. if (dma->enable_all) {
  596. atomic_inc(&dma->ref_count);
  597. knav_dma_hw_init(dma);
  598. dma_hw_enable_all(dma);
  599. }
  600. dev_info(kdev->dev, "DMA %s registered %d logical channels, flows %d, tx chans: %d, rx chans: %d%s\n",
  601. dma->name, num_chan, dma->max_rx_flow,
  602. dma->max_tx_chan, dma->max_rx_chan,
  603. dma->loopback ? ", loopback" : "");
  604. return 0;
  605. }
  606. static int knav_dma_probe(struct platform_device *pdev)
  607. {
  608. struct device *dev = &pdev->dev;
  609. struct device_node *node = pdev->dev.of_node;
  610. struct device_node *child;
  611. int ret = 0;
  612. if (!node) {
  613. dev_err(&pdev->dev, "could not find device info\n");
  614. return -EINVAL;
  615. }
  616. kdev = devm_kzalloc(dev,
  617. sizeof(struct knav_dma_pool_device), GFP_KERNEL);
  618. if (!kdev) {
  619. dev_err(dev, "could not allocate driver mem\n");
  620. return -ENOMEM;
  621. }
  622. kdev->dev = dev;
  623. INIT_LIST_HEAD(&kdev->list);
  624. pm_runtime_enable(kdev->dev);
  625. ret = pm_runtime_get_sync(kdev->dev);
  626. if (ret < 0) {
  627. dev_err(kdev->dev, "unable to enable pktdma, err %d\n", ret);
  628. return ret;
  629. }
  630. /* Initialise all packet dmas */
  631. for_each_child_of_node(node, child) {
  632. ret = dma_init(node, child);
  633. if (ret) {
  634. dev_err(&pdev->dev, "init failed with %d\n", ret);
  635. break;
  636. }
  637. }
  638. if (list_empty(&kdev->list)) {
  639. dev_err(dev, "no valid dma instance\n");
  640. return -ENODEV;
  641. }
  642. debugfs_create_file("knav_dma", S_IFREG | S_IRUGO, NULL, NULL,
  643. &knav_dma_debug_ops);
  644. return ret;
  645. }
  646. static int knav_dma_remove(struct platform_device *pdev)
  647. {
  648. struct knav_dma_device *dma;
  649. list_for_each_entry(dma, &kdev->list, list) {
  650. if (atomic_dec_return(&dma->ref_count) == 0)
  651. knav_dma_hw_destroy(dma);
  652. }
  653. pm_runtime_put_sync(&pdev->dev);
  654. pm_runtime_disable(&pdev->dev);
  655. return 0;
  656. }
  657. static struct of_device_id of_match[] = {
  658. { .compatible = "ti,keystone-navigator-dma", },
  659. {},
  660. };
  661. MODULE_DEVICE_TABLE(of, of_match);
  662. static struct platform_driver knav_dma_driver = {
  663. .probe = knav_dma_probe,
  664. .remove = knav_dma_remove,
  665. .driver = {
  666. .name = "keystone-navigator-dma",
  667. .of_match_table = of_match,
  668. },
  669. };
  670. module_platform_driver(knav_dma_driver);
  671. MODULE_LICENSE("GPL v2");
  672. MODULE_DESCRIPTION("TI Keystone Navigator Packet DMA driver");
  673. MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com>");
  674. MODULE_AUTHOR("Santosh Shilimkar <santosh.shilimkar@ti.com>");