knav_dma.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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 device_ready;
  120. bool knav_dma_device_ready(void)
  121. {
  122. return device_ready;
  123. }
  124. EXPORT_SYMBOL_GPL(knav_dma_device_ready);
  125. static bool check_config(struct knav_dma_chan *chan, struct knav_dma_cfg *cfg)
  126. {
  127. if (!memcmp(&chan->cfg, cfg, sizeof(*cfg)))
  128. return true;
  129. else
  130. return false;
  131. }
  132. static int chan_start(struct knav_dma_chan *chan,
  133. struct knav_dma_cfg *cfg)
  134. {
  135. u32 v = 0;
  136. spin_lock(&chan->lock);
  137. if ((chan->direction == DMA_MEM_TO_DEV) && chan->reg_chan) {
  138. if (cfg->u.tx.filt_pswords)
  139. v |= DMA_TX_FILT_PSWORDS;
  140. if (cfg->u.tx.filt_einfo)
  141. v |= DMA_TX_FILT_EINFO;
  142. writel_relaxed(v, &chan->reg_chan->mode);
  143. writel_relaxed(DMA_ENABLE, &chan->reg_chan->control);
  144. }
  145. if (chan->reg_tx_sched)
  146. writel_relaxed(cfg->u.tx.priority, &chan->reg_tx_sched->prio);
  147. if (chan->reg_rx_flow) {
  148. v = 0;
  149. if (cfg->u.rx.einfo_present)
  150. v |= CHAN_HAS_EPIB;
  151. if (cfg->u.rx.psinfo_present)
  152. v |= CHAN_HAS_PSINFO;
  153. if (cfg->u.rx.err_mode == DMA_RETRY)
  154. v |= CHAN_ERR_RETRY;
  155. v |= (cfg->u.rx.desc_type & DESC_TYPE_MASK) << DESC_TYPE_SHIFT;
  156. if (cfg->u.rx.psinfo_at_sop)
  157. v |= CHAN_PSINFO_AT_SOP;
  158. v |= (cfg->u.rx.sop_offset & CHAN_SOP_OFF_MASK)
  159. << CHAN_SOP_OFF_SHIFT;
  160. v |= cfg->u.rx.dst_q & CHAN_QNUM_MASK;
  161. writel_relaxed(v, &chan->reg_rx_flow->control);
  162. writel_relaxed(0, &chan->reg_rx_flow->tags);
  163. writel_relaxed(0, &chan->reg_rx_flow->tag_sel);
  164. v = cfg->u.rx.fdq[0] << 16;
  165. v |= cfg->u.rx.fdq[1] & CHAN_QNUM_MASK;
  166. writel_relaxed(v, &chan->reg_rx_flow->fdq_sel[0]);
  167. v = cfg->u.rx.fdq[2] << 16;
  168. v |= cfg->u.rx.fdq[3] & CHAN_QNUM_MASK;
  169. writel_relaxed(v, &chan->reg_rx_flow->fdq_sel[1]);
  170. writel_relaxed(0, &chan->reg_rx_flow->thresh[0]);
  171. writel_relaxed(0, &chan->reg_rx_flow->thresh[1]);
  172. writel_relaxed(0, &chan->reg_rx_flow->thresh[2]);
  173. }
  174. /* Keep a copy of the cfg */
  175. memcpy(&chan->cfg, cfg, sizeof(*cfg));
  176. spin_unlock(&chan->lock);
  177. return 0;
  178. }
  179. static int chan_teardown(struct knav_dma_chan *chan)
  180. {
  181. unsigned long end, value;
  182. if (!chan->reg_chan)
  183. return 0;
  184. /* indicate teardown */
  185. writel_relaxed(DMA_TEARDOWN, &chan->reg_chan->control);
  186. /* wait for the dma to shut itself down */
  187. end = jiffies + msecs_to_jiffies(DMA_TIMEOUT);
  188. do {
  189. value = readl_relaxed(&chan->reg_chan->control);
  190. if ((value & DMA_ENABLE) == 0)
  191. break;
  192. } while (time_after(end, jiffies));
  193. if (readl_relaxed(&chan->reg_chan->control) & DMA_ENABLE) {
  194. dev_err(kdev->dev, "timeout waiting for teardown\n");
  195. return -ETIMEDOUT;
  196. }
  197. return 0;
  198. }
  199. static void chan_stop(struct knav_dma_chan *chan)
  200. {
  201. spin_lock(&chan->lock);
  202. if (chan->reg_rx_flow) {
  203. /* first detach fdqs, starve out the flow */
  204. writel_relaxed(0, &chan->reg_rx_flow->fdq_sel[0]);
  205. writel_relaxed(0, &chan->reg_rx_flow->fdq_sel[1]);
  206. writel_relaxed(0, &chan->reg_rx_flow->thresh[0]);
  207. writel_relaxed(0, &chan->reg_rx_flow->thresh[1]);
  208. writel_relaxed(0, &chan->reg_rx_flow->thresh[2]);
  209. }
  210. /* teardown the dma channel */
  211. chan_teardown(chan);
  212. /* then disconnect the completion side */
  213. if (chan->reg_rx_flow) {
  214. writel_relaxed(0, &chan->reg_rx_flow->control);
  215. writel_relaxed(0, &chan->reg_rx_flow->tags);
  216. writel_relaxed(0, &chan->reg_rx_flow->tag_sel);
  217. }
  218. memset(&chan->cfg, 0, sizeof(struct knav_dma_cfg));
  219. spin_unlock(&chan->lock);
  220. dev_dbg(kdev->dev, "channel stopped\n");
  221. }
  222. static void dma_hw_enable_all(struct knav_dma_device *dma)
  223. {
  224. int i;
  225. for (i = 0; i < dma->max_tx_chan; i++) {
  226. writel_relaxed(0, &dma->reg_tx_chan[i].mode);
  227. writel_relaxed(DMA_ENABLE, &dma->reg_tx_chan[i].control);
  228. }
  229. }
  230. static void knav_dma_hw_init(struct knav_dma_device *dma)
  231. {
  232. unsigned v;
  233. int i;
  234. spin_lock(&dma->lock);
  235. v = dma->loopback ? DMA_LOOPBACK : 0;
  236. writel_relaxed(v, &dma->reg_global->emulation_control);
  237. v = readl_relaxed(&dma->reg_global->perf_control);
  238. v |= ((dma->rx_timeout & DMA_RX_TIMEOUT_MASK) << DMA_RX_TIMEOUT_SHIFT);
  239. writel_relaxed(v, &dma->reg_global->perf_control);
  240. v = ((dma->tx_priority << DMA_TX_PRIO_SHIFT) |
  241. (dma->rx_priority << DMA_RX_PRIO_SHIFT));
  242. writel_relaxed(v, &dma->reg_global->priority_control);
  243. /* Always enable all Rx channels. Rx paths are managed using flows */
  244. for (i = 0; i < dma->max_rx_chan; i++)
  245. writel_relaxed(DMA_ENABLE, &dma->reg_rx_chan[i].control);
  246. for (i = 0; i < dma->logical_queue_managers; i++)
  247. writel_relaxed(dma->qm_base_address[i],
  248. &dma->reg_global->qm_base_address[i]);
  249. spin_unlock(&dma->lock);
  250. }
  251. static void knav_dma_hw_destroy(struct knav_dma_device *dma)
  252. {
  253. int i;
  254. unsigned v;
  255. spin_lock(&dma->lock);
  256. v = ~DMA_ENABLE & REG_MASK;
  257. for (i = 0; i < dma->max_rx_chan; i++)
  258. writel_relaxed(v, &dma->reg_rx_chan[i].control);
  259. for (i = 0; i < dma->max_tx_chan; i++)
  260. writel_relaxed(v, &dma->reg_tx_chan[i].control);
  261. spin_unlock(&dma->lock);
  262. }
  263. static void dma_debug_show_channels(struct seq_file *s,
  264. struct knav_dma_chan *chan)
  265. {
  266. int i;
  267. seq_printf(s, "\t%s %d:\t",
  268. ((chan->direction == DMA_MEM_TO_DEV) ? "tx chan" : "rx flow"),
  269. chan_number(chan));
  270. if (chan->direction == DMA_MEM_TO_DEV) {
  271. seq_printf(s, "einfo - %d, pswords - %d, priority - %d\n",
  272. chan->cfg.u.tx.filt_einfo,
  273. chan->cfg.u.tx.filt_pswords,
  274. chan->cfg.u.tx.priority);
  275. } else {
  276. seq_printf(s, "einfo - %d, psinfo - %d, desc_type - %d\n",
  277. chan->cfg.u.rx.einfo_present,
  278. chan->cfg.u.rx.psinfo_present,
  279. chan->cfg.u.rx.desc_type);
  280. seq_printf(s, "\t\t\tdst_q: [%d], thresh: %d fdq: ",
  281. chan->cfg.u.rx.dst_q,
  282. chan->cfg.u.rx.thresh);
  283. for (i = 0; i < KNAV_DMA_FDQ_PER_CHAN; i++)
  284. seq_printf(s, "[%d]", chan->cfg.u.rx.fdq[i]);
  285. seq_printf(s, "\n");
  286. }
  287. }
  288. static void dma_debug_show_devices(struct seq_file *s,
  289. struct knav_dma_device *dma)
  290. {
  291. struct knav_dma_chan *chan;
  292. list_for_each_entry(chan, &dma->chan_list, list) {
  293. if (atomic_read(&chan->ref_count))
  294. dma_debug_show_channels(s, chan);
  295. }
  296. }
  297. static int dma_debug_show(struct seq_file *s, void *v)
  298. {
  299. struct knav_dma_device *dma;
  300. list_for_each_entry(dma, &kdev->list, list) {
  301. if (atomic_read(&dma->ref_count)) {
  302. seq_printf(s, "%s : max_tx_chan: (%d), max_rx_flows: (%d)\n",
  303. dma->name, dma->max_tx_chan, dma->max_rx_flow);
  304. dma_debug_show_devices(s, dma);
  305. }
  306. }
  307. return 0;
  308. }
  309. static int knav_dma_debug_open(struct inode *inode, struct file *file)
  310. {
  311. return single_open(file, dma_debug_show, NULL);
  312. }
  313. static const struct file_operations knav_dma_debug_ops = {
  314. .open = knav_dma_debug_open,
  315. .read = seq_read,
  316. .llseek = seq_lseek,
  317. .release = single_release,
  318. };
  319. static int of_channel_match_helper(struct device_node *np, const char *name,
  320. const char **dma_instance)
  321. {
  322. struct of_phandle_args args;
  323. struct device_node *dma_node;
  324. int index;
  325. dma_node = of_parse_phandle(np, "ti,navigator-dmas", 0);
  326. if (!dma_node)
  327. return -ENODEV;
  328. *dma_instance = dma_node->name;
  329. index = of_property_match_string(np, "ti,navigator-dma-names", name);
  330. if (index < 0) {
  331. dev_err(kdev->dev, "No 'ti,navigator-dma-names' property\n");
  332. return -ENODEV;
  333. }
  334. if (of_parse_phandle_with_fixed_args(np, "ti,navigator-dmas",
  335. 1, index, &args)) {
  336. dev_err(kdev->dev, "Missing the phandle args name %s\n", name);
  337. return -ENODEV;
  338. }
  339. if (args.args[0] < 0) {
  340. dev_err(kdev->dev, "Missing args for %s\n", name);
  341. return -ENODEV;
  342. }
  343. return args.args[0];
  344. }
  345. /**
  346. * knav_dma_open_channel() - try to setup an exclusive slave channel
  347. * @dev: pointer to client device structure
  348. * @name: slave channel name
  349. * @config: dma configuration parameters
  350. *
  351. * Returns pointer to appropriate DMA channel on success or error.
  352. */
  353. void *knav_dma_open_channel(struct device *dev, const char *name,
  354. struct knav_dma_cfg *config)
  355. {
  356. struct knav_dma_chan *chan;
  357. struct knav_dma_device *dma;
  358. bool found = false;
  359. int chan_num = -1;
  360. const char *instance;
  361. if (!kdev) {
  362. pr_err("keystone-navigator-dma driver not registered\n");
  363. return (void *)-EINVAL;
  364. }
  365. chan_num = of_channel_match_helper(dev->of_node, name, &instance);
  366. if (chan_num < 0) {
  367. dev_err(kdev->dev, "No DMA instance with name %s\n", name);
  368. return (void *)-EINVAL;
  369. }
  370. dev_dbg(kdev->dev, "initializing %s channel %d from DMA %s\n",
  371. config->direction == DMA_MEM_TO_DEV ? "transmit" :
  372. config->direction == DMA_DEV_TO_MEM ? "receive" :
  373. "unknown", chan_num, instance);
  374. if (config->direction != DMA_MEM_TO_DEV &&
  375. config->direction != DMA_DEV_TO_MEM) {
  376. dev_err(kdev->dev, "bad direction\n");
  377. return (void *)-EINVAL;
  378. }
  379. /* Look for correct dma instance */
  380. list_for_each_entry(dma, &kdev->list, list) {
  381. if (!strcmp(dma->name, instance)) {
  382. found = true;
  383. break;
  384. }
  385. }
  386. if (!found) {
  387. dev_err(kdev->dev, "No DMA instance with name %s\n", instance);
  388. return (void *)-EINVAL;
  389. }
  390. /* Look for correct dma channel from dma instance */
  391. found = false;
  392. list_for_each_entry(chan, &dma->chan_list, list) {
  393. if (config->direction == DMA_MEM_TO_DEV) {
  394. if (chan->channel == chan_num) {
  395. found = true;
  396. break;
  397. }
  398. } else {
  399. if (chan->flow == chan_num) {
  400. found = true;
  401. break;
  402. }
  403. }
  404. }
  405. if (!found) {
  406. dev_err(kdev->dev, "channel %d is not in DMA %s\n",
  407. chan_num, instance);
  408. return (void *)-EINVAL;
  409. }
  410. if (atomic_read(&chan->ref_count) >= 1) {
  411. if (!check_config(chan, config)) {
  412. dev_err(kdev->dev, "channel %d config miss-match\n",
  413. chan_num);
  414. return (void *)-EINVAL;
  415. }
  416. }
  417. if (atomic_inc_return(&chan->dma->ref_count) <= 1)
  418. knav_dma_hw_init(chan->dma);
  419. if (atomic_inc_return(&chan->ref_count) <= 1)
  420. chan_start(chan, config);
  421. dev_dbg(kdev->dev, "channel %d opened from DMA %s\n",
  422. chan_num, instance);
  423. return chan;
  424. }
  425. EXPORT_SYMBOL_GPL(knav_dma_open_channel);
  426. /**
  427. * knav_dma_close_channel() - Destroy a dma channel
  428. *
  429. * channel: dma channel handle
  430. *
  431. */
  432. void knav_dma_close_channel(void *channel)
  433. {
  434. struct knav_dma_chan *chan = channel;
  435. if (!kdev) {
  436. pr_err("keystone-navigator-dma driver not registered\n");
  437. return;
  438. }
  439. if (atomic_dec_return(&chan->ref_count) <= 0)
  440. chan_stop(chan);
  441. if (atomic_dec_return(&chan->dma->ref_count) <= 0)
  442. knav_dma_hw_destroy(chan->dma);
  443. dev_dbg(kdev->dev, "channel %d or flow %d closed from DMA %s\n",
  444. chan->channel, chan->flow, chan->dma->name);
  445. }
  446. EXPORT_SYMBOL_GPL(knav_dma_close_channel);
  447. static void __iomem *pktdma_get_regs(struct knav_dma_device *dma,
  448. struct device_node *node,
  449. unsigned index, resource_size_t *_size)
  450. {
  451. struct device *dev = kdev->dev;
  452. struct resource res;
  453. void __iomem *regs;
  454. int ret;
  455. ret = of_address_to_resource(node, index, &res);
  456. if (ret) {
  457. dev_err(dev, "Can't translate of node(%pOFn) address for index(%d)\n",
  458. node, index);
  459. return ERR_PTR(ret);
  460. }
  461. regs = devm_ioremap_resource(kdev->dev, &res);
  462. if (IS_ERR(regs))
  463. dev_err(dev, "Failed to map register base for index(%d) node(%pOFn)\n",
  464. index, node);
  465. if (_size)
  466. *_size = resource_size(&res);
  467. return regs;
  468. }
  469. static int pktdma_init_rx_chan(struct knav_dma_chan *chan, u32 flow)
  470. {
  471. struct knav_dma_device *dma = chan->dma;
  472. chan->flow = flow;
  473. chan->reg_rx_flow = dma->reg_rx_flow + flow;
  474. chan->channel = DMA_INVALID_ID;
  475. dev_dbg(kdev->dev, "rx flow(%d) (%p)\n", chan->flow, chan->reg_rx_flow);
  476. return 0;
  477. }
  478. static int pktdma_init_tx_chan(struct knav_dma_chan *chan, u32 channel)
  479. {
  480. struct knav_dma_device *dma = chan->dma;
  481. chan->channel = channel;
  482. chan->reg_chan = dma->reg_tx_chan + channel;
  483. chan->reg_tx_sched = dma->reg_tx_sched + channel;
  484. chan->flow = DMA_INVALID_ID;
  485. dev_dbg(kdev->dev, "tx channel(%d) (%p)\n", chan->channel, chan->reg_chan);
  486. return 0;
  487. }
  488. static int pktdma_init_chan(struct knav_dma_device *dma,
  489. enum dma_transfer_direction dir,
  490. unsigned chan_num)
  491. {
  492. struct device *dev = kdev->dev;
  493. struct knav_dma_chan *chan;
  494. int ret = -EINVAL;
  495. chan = devm_kzalloc(dev, sizeof(*chan), GFP_KERNEL);
  496. if (!chan)
  497. return -ENOMEM;
  498. INIT_LIST_HEAD(&chan->list);
  499. chan->dma = dma;
  500. chan->direction = DMA_TRANS_NONE;
  501. atomic_set(&chan->ref_count, 0);
  502. spin_lock_init(&chan->lock);
  503. if (dir == DMA_MEM_TO_DEV) {
  504. chan->direction = dir;
  505. ret = pktdma_init_tx_chan(chan, chan_num);
  506. } else if (dir == DMA_DEV_TO_MEM) {
  507. chan->direction = dir;
  508. ret = pktdma_init_rx_chan(chan, chan_num);
  509. } else {
  510. dev_err(dev, "channel(%d) direction unknown\n", chan_num);
  511. }
  512. list_add_tail(&chan->list, &dma->chan_list);
  513. return ret;
  514. }
  515. static int dma_init(struct device_node *cloud, struct device_node *dma_node)
  516. {
  517. unsigned max_tx_chan, max_rx_chan, max_rx_flow, max_tx_sched;
  518. struct device_node *node = dma_node;
  519. struct knav_dma_device *dma;
  520. int ret, len, num_chan = 0;
  521. resource_size_t size;
  522. u32 timeout;
  523. u32 i;
  524. dma = devm_kzalloc(kdev->dev, sizeof(*dma), GFP_KERNEL);
  525. if (!dma) {
  526. dev_err(kdev->dev, "could not allocate driver mem\n");
  527. return -ENOMEM;
  528. }
  529. INIT_LIST_HEAD(&dma->list);
  530. INIT_LIST_HEAD(&dma->chan_list);
  531. if (!of_find_property(cloud, "ti,navigator-cloud-address", &len)) {
  532. dev_err(kdev->dev, "unspecified navigator cloud addresses\n");
  533. return -ENODEV;
  534. }
  535. dma->logical_queue_managers = len / sizeof(u32);
  536. if (dma->logical_queue_managers > DMA_MAX_QMS) {
  537. dev_warn(kdev->dev, "too many queue mgrs(>%d) rest ignored\n",
  538. dma->logical_queue_managers);
  539. dma->logical_queue_managers = DMA_MAX_QMS;
  540. }
  541. ret = of_property_read_u32_array(cloud, "ti,navigator-cloud-address",
  542. dma->qm_base_address,
  543. dma->logical_queue_managers);
  544. if (ret) {
  545. dev_err(kdev->dev, "invalid navigator cloud addresses\n");
  546. return -ENODEV;
  547. }
  548. dma->reg_global = pktdma_get_regs(dma, node, 0, &size);
  549. if (!dma->reg_global)
  550. return -ENODEV;
  551. if (size < sizeof(struct reg_global)) {
  552. dev_err(kdev->dev, "bad size %pa for global regs\n", &size);
  553. return -ENODEV;
  554. }
  555. dma->reg_tx_chan = pktdma_get_regs(dma, node, 1, &size);
  556. if (!dma->reg_tx_chan)
  557. return -ENODEV;
  558. max_tx_chan = size / sizeof(struct reg_chan);
  559. dma->reg_rx_chan = pktdma_get_regs(dma, node, 2, &size);
  560. if (!dma->reg_rx_chan)
  561. return -ENODEV;
  562. max_rx_chan = size / sizeof(struct reg_chan);
  563. dma->reg_tx_sched = pktdma_get_regs(dma, node, 3, &size);
  564. if (!dma->reg_tx_sched)
  565. return -ENODEV;
  566. max_tx_sched = size / sizeof(struct reg_tx_sched);
  567. dma->reg_rx_flow = pktdma_get_regs(dma, node, 4, &size);
  568. if (!dma->reg_rx_flow)
  569. return -ENODEV;
  570. max_rx_flow = size / sizeof(struct reg_rx_flow);
  571. dma->rx_priority = DMA_PRIO_DEFAULT;
  572. dma->tx_priority = DMA_PRIO_DEFAULT;
  573. dma->enable_all = (of_get_property(node, "ti,enable-all", NULL) != NULL);
  574. dma->loopback = (of_get_property(node, "ti,loop-back", NULL) != NULL);
  575. ret = of_property_read_u32(node, "ti,rx-retry-timeout", &timeout);
  576. if (ret < 0) {
  577. dev_dbg(kdev->dev, "unspecified rx timeout using value %d\n",
  578. DMA_RX_TIMEOUT_DEFAULT);
  579. timeout = DMA_RX_TIMEOUT_DEFAULT;
  580. }
  581. dma->rx_timeout = timeout;
  582. dma->max_rx_chan = max_rx_chan;
  583. dma->max_rx_flow = max_rx_flow;
  584. dma->max_tx_chan = min(max_tx_chan, max_tx_sched);
  585. atomic_set(&dma->ref_count, 0);
  586. strcpy(dma->name, node->name);
  587. spin_lock_init(&dma->lock);
  588. for (i = 0; i < dma->max_tx_chan; i++) {
  589. if (pktdma_init_chan(dma, DMA_MEM_TO_DEV, i) >= 0)
  590. num_chan++;
  591. }
  592. for (i = 0; i < dma->max_rx_flow; i++) {
  593. if (pktdma_init_chan(dma, DMA_DEV_TO_MEM, i) >= 0)
  594. num_chan++;
  595. }
  596. list_add_tail(&dma->list, &kdev->list);
  597. /*
  598. * For DSP software usecases or userpace transport software, setup all
  599. * the DMA hardware resources.
  600. */
  601. if (dma->enable_all) {
  602. atomic_inc(&dma->ref_count);
  603. knav_dma_hw_init(dma);
  604. dma_hw_enable_all(dma);
  605. }
  606. dev_info(kdev->dev, "DMA %s registered %d logical channels, flows %d, tx chans: %d, rx chans: %d%s\n",
  607. dma->name, num_chan, dma->max_rx_flow,
  608. dma->max_tx_chan, dma->max_rx_chan,
  609. dma->loopback ? ", loopback" : "");
  610. return 0;
  611. }
  612. static int knav_dma_probe(struct platform_device *pdev)
  613. {
  614. struct device *dev = &pdev->dev;
  615. struct device_node *node = pdev->dev.of_node;
  616. struct device_node *child;
  617. int ret = 0;
  618. if (!node) {
  619. dev_err(&pdev->dev, "could not find device info\n");
  620. return -EINVAL;
  621. }
  622. kdev = devm_kzalloc(dev,
  623. sizeof(struct knav_dma_pool_device), GFP_KERNEL);
  624. if (!kdev) {
  625. dev_err(dev, "could not allocate driver mem\n");
  626. return -ENOMEM;
  627. }
  628. kdev->dev = dev;
  629. INIT_LIST_HEAD(&kdev->list);
  630. pm_runtime_enable(kdev->dev);
  631. ret = pm_runtime_get_sync(kdev->dev);
  632. if (ret < 0) {
  633. pm_runtime_put_noidle(kdev->dev);
  634. dev_err(kdev->dev, "unable to enable pktdma, err %d\n", ret);
  635. goto err_pm_disable;
  636. }
  637. /* Initialise all packet dmas */
  638. for_each_child_of_node(node, child) {
  639. ret = dma_init(node, child);
  640. if (ret) {
  641. dev_err(&pdev->dev, "init failed with %d\n", ret);
  642. break;
  643. }
  644. }
  645. if (list_empty(&kdev->list)) {
  646. dev_err(dev, "no valid dma instance\n");
  647. ret = -ENODEV;
  648. goto err_put_sync;
  649. }
  650. debugfs_create_file("knav_dma", S_IFREG | S_IRUGO, NULL, NULL,
  651. &knav_dma_debug_ops);
  652. device_ready = true;
  653. return ret;
  654. err_put_sync:
  655. pm_runtime_put_sync(kdev->dev);
  656. err_pm_disable:
  657. pm_runtime_disable(kdev->dev);
  658. return ret;
  659. }
  660. static int knav_dma_remove(struct platform_device *pdev)
  661. {
  662. struct knav_dma_device *dma;
  663. list_for_each_entry(dma, &kdev->list, list) {
  664. if (atomic_dec_return(&dma->ref_count) == 0)
  665. knav_dma_hw_destroy(dma);
  666. }
  667. pm_runtime_put_sync(&pdev->dev);
  668. pm_runtime_disable(&pdev->dev);
  669. return 0;
  670. }
  671. static struct of_device_id of_match[] = {
  672. { .compatible = "ti,keystone-navigator-dma", },
  673. {},
  674. };
  675. MODULE_DEVICE_TABLE(of, of_match);
  676. static struct platform_driver knav_dma_driver = {
  677. .probe = knav_dma_probe,
  678. .remove = knav_dma_remove,
  679. .driver = {
  680. .name = "keystone-navigator-dma",
  681. .of_match_table = of_match,
  682. },
  683. };
  684. module_platform_driver(knav_dma_driver);
  685. MODULE_LICENSE("GPL v2");
  686. MODULE_DESCRIPTION("TI Keystone Navigator Packet DMA driver");
  687. MODULE_AUTHOR("Sandeep Nair <sandeep_n@ti.com>");
  688. MODULE_AUTHOR("Santosh Shilimkar <santosh.shilimkar@ti.com>");