ti-msgmgr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Texas Instruments' Message Manager Driver
  4. *
  5. * Copyright (C) 2015-2017 Texas Instruments Incorporated - http://www.ti.com/
  6. * Nishanth Menon
  7. */
  8. #define pr_fmt(fmt) "%s: " fmt, __func__
  9. #include <linux/device.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mailbox_controller.h>
  14. #include <linux/module.h>
  15. #include <linux/of_device.h>
  16. #include <linux/of.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/soc/ti/ti-msgmgr.h>
  20. #define Q_DATA_OFFSET(proxy, queue, reg) \
  21. ((0x10000 * (proxy)) + (0x80 * (queue)) + ((reg) * 4))
  22. #define Q_STATE_OFFSET(queue) ((queue) * 0x4)
  23. #define Q_STATE_ENTRY_COUNT_MASK (0xFFF000)
  24. /**
  25. * struct ti_msgmgr_valid_queue_desc - SoC valid queues meant for this processor
  26. * @queue_id: Queue Number for this path
  27. * @proxy_id: Proxy ID representing the processor in SoC
  28. * @is_tx: Is this a receive path?
  29. */
  30. struct ti_msgmgr_valid_queue_desc {
  31. u8 queue_id;
  32. u8 proxy_id;
  33. bool is_tx;
  34. };
  35. /**
  36. * struct ti_msgmgr_desc - Description of message manager integration
  37. * @queue_count: Number of Queues
  38. * @max_message_size: Message size in bytes
  39. * @max_messages: Number of messages
  40. * @data_first_reg: First data register for proxy data region
  41. * @data_last_reg: Last data register for proxy data region
  42. * @status_cnt_mask: Mask for getting the status value
  43. * @tx_polled: Do I need to use polled mechanism for tx
  44. * @tx_poll_timeout_ms: Timeout in ms if polled
  45. * @valid_queues: List of Valid queues that the processor can access
  46. * @data_region_name: Name of the proxy data region
  47. * @status_region_name: Name of the proxy status region
  48. * @num_valid_queues: Number of valid queues
  49. *
  50. * This structure is used in of match data to describe how integration
  51. * for a specific compatible SoC is done.
  52. */
  53. struct ti_msgmgr_desc {
  54. u8 queue_count;
  55. u8 max_message_size;
  56. u8 max_messages;
  57. u8 data_first_reg;
  58. u8 data_last_reg;
  59. u32 status_cnt_mask;
  60. bool tx_polled;
  61. int tx_poll_timeout_ms;
  62. const struct ti_msgmgr_valid_queue_desc *valid_queues;
  63. const char *data_region_name;
  64. const char *status_region_name;
  65. int num_valid_queues;
  66. };
  67. /**
  68. * struct ti_queue_inst - Description of a queue instance
  69. * @name: Queue Name
  70. * @queue_id: Queue Identifier as mapped on SoC
  71. * @proxy_id: Proxy Identifier as mapped on SoC
  72. * @irq: IRQ for Rx Queue
  73. * @is_tx: 'true' if transmit queue, else, 'false'
  74. * @queue_buff_start: First register of Data Buffer
  75. * @queue_buff_end: Last (or confirmation) register of Data buffer
  76. * @queue_state: Queue status register
  77. * @chan: Mailbox channel
  78. * @rx_buff: Receive buffer pointer allocated at probe, max_message_size
  79. */
  80. struct ti_queue_inst {
  81. char name[30];
  82. u8 queue_id;
  83. u8 proxy_id;
  84. int irq;
  85. bool is_tx;
  86. void __iomem *queue_buff_start;
  87. void __iomem *queue_buff_end;
  88. void __iomem *queue_state;
  89. struct mbox_chan *chan;
  90. u32 *rx_buff;
  91. };
  92. /**
  93. * struct ti_msgmgr_inst - Description of a Message Manager Instance
  94. * @dev: device pointer corresponding to the Message Manager instance
  95. * @desc: Description of the SoC integration
  96. * @queue_proxy_region: Queue proxy region where queue buffers are located
  97. * @queue_state_debug_region: Queue status register regions
  98. * @num_valid_queues: Number of valid queues defined for the processor
  99. * Note: other queues are probably reserved for other processors
  100. * in the SoC.
  101. * @qinsts: Array of valid Queue Instances for the Processor
  102. * @mbox: Mailbox Controller
  103. * @chans: Array for channels corresponding to the Queue Instances.
  104. */
  105. struct ti_msgmgr_inst {
  106. struct device *dev;
  107. const struct ti_msgmgr_desc *desc;
  108. void __iomem *queue_proxy_region;
  109. void __iomem *queue_state_debug_region;
  110. u8 num_valid_queues;
  111. struct ti_queue_inst *qinsts;
  112. struct mbox_controller mbox;
  113. struct mbox_chan *chans;
  114. };
  115. /**
  116. * ti_msgmgr_queue_get_num_messages() - Get the number of pending messages
  117. * @d: Description of message manager
  118. * @qinst: Queue instance for which we check the number of pending messages
  119. *
  120. * Return: number of messages pending in the queue (0 == no pending messages)
  121. */
  122. static inline int
  123. ti_msgmgr_queue_get_num_messages(const struct ti_msgmgr_desc *d,
  124. struct ti_queue_inst *qinst)
  125. {
  126. u32 val;
  127. u32 status_cnt_mask = d->status_cnt_mask;
  128. /*
  129. * We cannot use relaxed operation here - update may happen
  130. * real-time.
  131. */
  132. val = readl(qinst->queue_state) & status_cnt_mask;
  133. val >>= __ffs(status_cnt_mask);
  134. return val;
  135. }
  136. /**
  137. * ti_msgmgr_queue_rx_interrupt() - Interrupt handler for receive Queue
  138. * @irq: Interrupt number
  139. * @p: Channel Pointer
  140. *
  141. * Return: -EINVAL if there is no instance
  142. * IRQ_NONE if the interrupt is not ours.
  143. * IRQ_HANDLED if the rx interrupt was successfully handled.
  144. */
  145. static irqreturn_t ti_msgmgr_queue_rx_interrupt(int irq, void *p)
  146. {
  147. struct mbox_chan *chan = p;
  148. struct device *dev = chan->mbox->dev;
  149. struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
  150. struct ti_queue_inst *qinst = chan->con_priv;
  151. const struct ti_msgmgr_desc *desc;
  152. int msg_count, num_words;
  153. struct ti_msgmgr_message message;
  154. void __iomem *data_reg;
  155. u32 *word_data;
  156. if (WARN_ON(!inst)) {
  157. dev_err(dev, "no platform drv data??\n");
  158. return -EINVAL;
  159. }
  160. /* Do I have an invalid interrupt source? */
  161. if (qinst->is_tx) {
  162. dev_err(dev, "Cannot handle rx interrupt on tx channel %s\n",
  163. qinst->name);
  164. return IRQ_NONE;
  165. }
  166. desc = inst->desc;
  167. /* Do I actually have messages to read? */
  168. msg_count = ti_msgmgr_queue_get_num_messages(desc, qinst);
  169. if (!msg_count) {
  170. /* Shared IRQ? */
  171. dev_dbg(dev, "Spurious event - 0 pending data!\n");
  172. return IRQ_NONE;
  173. }
  174. /*
  175. * I have no idea about the protocol being used to communicate with the
  176. * remote producer - 0 could be valid data, so I wont make a judgement
  177. * of how many bytes I should be reading. Let the client figure this
  178. * out.. I just read the full message and pass it on..
  179. */
  180. message.len = desc->max_message_size;
  181. message.buf = (u8 *)qinst->rx_buff;
  182. /*
  183. * NOTE about register access involved here:
  184. * the hardware block is implemented with 32bit access operations and no
  185. * support for data splitting. We don't want the hardware to misbehave
  186. * with sub 32bit access - For example: if the last register read is
  187. * split into byte wise access, it can result in the queue getting
  188. * stuck or indeterminate behavior. An out of order read operation may
  189. * result in weird data results as well.
  190. * Hence, we do not use memcpy_fromio or __ioread32_copy here, instead
  191. * we depend on readl for the purpose.
  192. *
  193. * Also note that the final register read automatically marks the
  194. * queue message as read.
  195. */
  196. for (data_reg = qinst->queue_buff_start, word_data = qinst->rx_buff,
  197. num_words = (desc->max_message_size / sizeof(u32));
  198. num_words; num_words--, data_reg += sizeof(u32), word_data++)
  199. *word_data = readl(data_reg);
  200. /*
  201. * Last register read automatically clears the IRQ if only 1 message
  202. * is pending - so send the data up the stack..
  203. * NOTE: Client is expected to be as optimal as possible, since
  204. * we invoke the handler in IRQ context.
  205. */
  206. mbox_chan_received_data(chan, (void *)&message);
  207. return IRQ_HANDLED;
  208. }
  209. /**
  210. * ti_msgmgr_queue_peek_data() - Peek to see if there are any rx messages.
  211. * @chan: Channel Pointer
  212. *
  213. * Return: 'true' if there is pending rx data, 'false' if there is none.
  214. */
  215. static bool ti_msgmgr_queue_peek_data(struct mbox_chan *chan)
  216. {
  217. struct ti_queue_inst *qinst = chan->con_priv;
  218. struct device *dev = chan->mbox->dev;
  219. struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
  220. int msg_count;
  221. if (qinst->is_tx)
  222. return false;
  223. msg_count = ti_msgmgr_queue_get_num_messages(inst->desc, qinst);
  224. return msg_count ? true : false;
  225. }
  226. /**
  227. * ti_msgmgr_last_tx_done() - See if all the tx messages are sent
  228. * @chan: Channel pointer
  229. *
  230. * Return: 'true' is no pending tx data, 'false' if there are any.
  231. */
  232. static bool ti_msgmgr_last_tx_done(struct mbox_chan *chan)
  233. {
  234. struct ti_queue_inst *qinst = chan->con_priv;
  235. struct device *dev = chan->mbox->dev;
  236. struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
  237. int msg_count;
  238. if (!qinst->is_tx)
  239. return false;
  240. msg_count = ti_msgmgr_queue_get_num_messages(inst->desc, qinst);
  241. /* if we have any messages pending.. */
  242. return msg_count ? false : true;
  243. }
  244. /**
  245. * ti_msgmgr_send_data() - Send data
  246. * @chan: Channel Pointer
  247. * @data: ti_msgmgr_message * Message Pointer
  248. *
  249. * Return: 0 if all goes good, else appropriate error messages.
  250. */
  251. static int ti_msgmgr_send_data(struct mbox_chan *chan, void *data)
  252. {
  253. struct device *dev = chan->mbox->dev;
  254. struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
  255. const struct ti_msgmgr_desc *desc;
  256. struct ti_queue_inst *qinst = chan->con_priv;
  257. int num_words, trail_bytes;
  258. struct ti_msgmgr_message *message = data;
  259. void __iomem *data_reg;
  260. u32 *word_data;
  261. if (WARN_ON(!inst)) {
  262. dev_err(dev, "no platform drv data??\n");
  263. return -EINVAL;
  264. }
  265. desc = inst->desc;
  266. if (desc->max_message_size < message->len) {
  267. dev_err(dev, "Queue %s message length %zu > max %d\n",
  268. qinst->name, message->len, desc->max_message_size);
  269. return -EINVAL;
  270. }
  271. /* NOTE: Constraints similar to rx path exists here as well */
  272. for (data_reg = qinst->queue_buff_start,
  273. num_words = message->len / sizeof(u32),
  274. word_data = (u32 *)message->buf;
  275. num_words; num_words--, data_reg += sizeof(u32), word_data++)
  276. writel(*word_data, data_reg);
  277. trail_bytes = message->len % sizeof(u32);
  278. if (trail_bytes) {
  279. u32 data_trail = *word_data;
  280. /* Ensure all unused data is 0 */
  281. data_trail &= 0xFFFFFFFF >> (8 * (sizeof(u32) - trail_bytes));
  282. writel(data_trail, data_reg);
  283. data_reg++;
  284. }
  285. /*
  286. * 'data_reg' indicates next register to write. If we did not already
  287. * write on tx complete reg(last reg), we must do so for transmit
  288. */
  289. if (data_reg <= qinst->queue_buff_end)
  290. writel(0, qinst->queue_buff_end);
  291. return 0;
  292. }
  293. /**
  294. * ti_msgmgr_queue_rx_irq_req() - RX IRQ request
  295. * @dev: device pointer
  296. * @qinst: Queue instance
  297. * @chan: Channel pointer
  298. */
  299. static int ti_msgmgr_queue_rx_irq_req(struct device *dev,
  300. struct ti_queue_inst *qinst,
  301. struct mbox_chan *chan)
  302. {
  303. int ret = 0;
  304. char of_rx_irq_name[7];
  305. struct device_node *np;
  306. snprintf(of_rx_irq_name, sizeof(of_rx_irq_name),
  307. "rx_%03d", qinst->queue_id);
  308. /* Get the IRQ if not found */
  309. if (qinst->irq < 0) {
  310. np = of_node_get(dev->of_node);
  311. if (!np)
  312. return -ENODATA;
  313. qinst->irq = of_irq_get_byname(np, of_rx_irq_name);
  314. of_node_put(np);
  315. if (qinst->irq < 0) {
  316. dev_err(dev,
  317. "QID %d PID %d:No IRQ[%s]: %d\n",
  318. qinst->queue_id, qinst->proxy_id,
  319. of_rx_irq_name, qinst->irq);
  320. return qinst->irq;
  321. }
  322. }
  323. /* With the expectation that the IRQ might be shared in SoC */
  324. ret = request_irq(qinst->irq, ti_msgmgr_queue_rx_interrupt,
  325. IRQF_SHARED, qinst->name, chan);
  326. if (ret) {
  327. dev_err(dev, "Unable to get IRQ %d on %s(res=%d)\n",
  328. qinst->irq, qinst->name, ret);
  329. }
  330. return ret;
  331. }
  332. /**
  333. * ti_msgmgr_queue_startup() - Startup queue
  334. * @chan: Channel pointer
  335. *
  336. * Return: 0 if all goes good, else return corresponding error message
  337. */
  338. static int ti_msgmgr_queue_startup(struct mbox_chan *chan)
  339. {
  340. struct device *dev = chan->mbox->dev;
  341. struct ti_msgmgr_inst *inst = dev_get_drvdata(dev);
  342. struct ti_queue_inst *qinst = chan->con_priv;
  343. const struct ti_msgmgr_desc *d = inst->desc;
  344. int ret;
  345. if (!qinst->is_tx) {
  346. /* Allocate usage buffer for rx */
  347. qinst->rx_buff = kzalloc(d->max_message_size, GFP_KERNEL);
  348. if (!qinst->rx_buff)
  349. return -ENOMEM;
  350. /* Request IRQ */
  351. ret = ti_msgmgr_queue_rx_irq_req(dev, qinst, chan);
  352. if (ret) {
  353. kfree(qinst->rx_buff);
  354. return ret;
  355. }
  356. }
  357. return 0;
  358. }
  359. /**
  360. * ti_msgmgr_queue_shutdown() - Shutdown the queue
  361. * @chan: Channel pointer
  362. */
  363. static void ti_msgmgr_queue_shutdown(struct mbox_chan *chan)
  364. {
  365. struct ti_queue_inst *qinst = chan->con_priv;
  366. if (!qinst->is_tx) {
  367. free_irq(qinst->irq, chan);
  368. kfree(qinst->rx_buff);
  369. }
  370. }
  371. /**
  372. * ti_msgmgr_of_xlate() - Translation of phandle to queue
  373. * @mbox: Mailbox controller
  374. * @p: phandle pointer
  375. *
  376. * Return: Mailbox channel corresponding to the queue, else return error
  377. * pointer.
  378. */
  379. static struct mbox_chan *ti_msgmgr_of_xlate(struct mbox_controller *mbox,
  380. const struct of_phandle_args *p)
  381. {
  382. struct ti_msgmgr_inst *inst;
  383. int req_qid, req_pid;
  384. struct ti_queue_inst *qinst;
  385. int i;
  386. inst = container_of(mbox, struct ti_msgmgr_inst, mbox);
  387. if (WARN_ON(!inst))
  388. return ERR_PTR(-EINVAL);
  389. /* #mbox-cells is 2 */
  390. if (p->args_count != 2) {
  391. dev_err(inst->dev, "Invalid arguments in dt[%d] instead of 2\n",
  392. p->args_count);
  393. return ERR_PTR(-EINVAL);
  394. }
  395. req_qid = p->args[0];
  396. req_pid = p->args[1];
  397. for (qinst = inst->qinsts, i = 0; i < inst->num_valid_queues;
  398. i++, qinst++) {
  399. if (req_qid == qinst->queue_id && req_pid == qinst->proxy_id)
  400. return qinst->chan;
  401. }
  402. dev_err(inst->dev, "Queue ID %d, Proxy ID %d is wrong on %s\n",
  403. req_qid, req_pid, p->np->name);
  404. return ERR_PTR(-ENOENT);
  405. }
  406. /**
  407. * ti_msgmgr_queue_setup() - Setup data structures for each queue instance
  408. * @idx: index of the queue
  409. * @dev: pointer to the message manager device
  410. * @np: pointer to the of node
  411. * @inst: Queue instance pointer
  412. * @d: Message Manager instance description data
  413. * @qd: Queue description data
  414. * @qinst: Queue instance pointer
  415. * @chan: pointer to mailbox channel
  416. *
  417. * Return: 0 if all went well, else return corresponding error
  418. */
  419. static int ti_msgmgr_queue_setup(int idx, struct device *dev,
  420. struct device_node *np,
  421. struct ti_msgmgr_inst *inst,
  422. const struct ti_msgmgr_desc *d,
  423. const struct ti_msgmgr_valid_queue_desc *qd,
  424. struct ti_queue_inst *qinst,
  425. struct mbox_chan *chan)
  426. {
  427. qinst->proxy_id = qd->proxy_id;
  428. qinst->queue_id = qd->queue_id;
  429. if (qinst->queue_id > d->queue_count) {
  430. dev_err(dev, "Queue Data [idx=%d] queuid %d > %d\n",
  431. idx, qinst->queue_id, d->queue_count);
  432. return -ERANGE;
  433. }
  434. qinst->is_tx = qd->is_tx;
  435. snprintf(qinst->name, sizeof(qinst->name), "%s %s_%03d_%03d",
  436. dev_name(dev), qinst->is_tx ? "tx" : "rx", qinst->queue_id,
  437. qinst->proxy_id);
  438. qinst->queue_buff_start = inst->queue_proxy_region +
  439. Q_DATA_OFFSET(qinst->proxy_id, qinst->queue_id, d->data_first_reg);
  440. qinst->queue_buff_end = inst->queue_proxy_region +
  441. Q_DATA_OFFSET(qinst->proxy_id, qinst->queue_id, d->data_last_reg);
  442. qinst->queue_state = inst->queue_state_debug_region +
  443. Q_STATE_OFFSET(qinst->queue_id);
  444. qinst->chan = chan;
  445. /* Setup an error value for IRQ - Lazy allocation */
  446. qinst->irq = -EINVAL;
  447. chan->con_priv = qinst;
  448. dev_dbg(dev, "[%d] qidx=%d pidx=%d irq=%d q_s=%p q_e = %p\n",
  449. idx, qinst->queue_id, qinst->proxy_id, qinst->irq,
  450. qinst->queue_buff_start, qinst->queue_buff_end);
  451. return 0;
  452. }
  453. /* Queue operations */
  454. static const struct mbox_chan_ops ti_msgmgr_chan_ops = {
  455. .startup = ti_msgmgr_queue_startup,
  456. .shutdown = ti_msgmgr_queue_shutdown,
  457. .peek_data = ti_msgmgr_queue_peek_data,
  458. .last_tx_done = ti_msgmgr_last_tx_done,
  459. .send_data = ti_msgmgr_send_data,
  460. };
  461. /* Keystone K2G SoC integration details */
  462. static const struct ti_msgmgr_valid_queue_desc k2g_valid_queues[] = {
  463. {.queue_id = 0, .proxy_id = 0, .is_tx = true,},
  464. {.queue_id = 1, .proxy_id = 0, .is_tx = true,},
  465. {.queue_id = 2, .proxy_id = 0, .is_tx = true,},
  466. {.queue_id = 3, .proxy_id = 0, .is_tx = true,},
  467. {.queue_id = 5, .proxy_id = 2, .is_tx = false,},
  468. {.queue_id = 56, .proxy_id = 1, .is_tx = true,},
  469. {.queue_id = 57, .proxy_id = 2, .is_tx = false,},
  470. {.queue_id = 58, .proxy_id = 3, .is_tx = true,},
  471. {.queue_id = 59, .proxy_id = 4, .is_tx = true,},
  472. {.queue_id = 60, .proxy_id = 5, .is_tx = true,},
  473. {.queue_id = 61, .proxy_id = 6, .is_tx = true,},
  474. };
  475. static const struct ti_msgmgr_desc k2g_desc = {
  476. .queue_count = 64,
  477. .max_message_size = 64,
  478. .max_messages = 128,
  479. .data_region_name = "queue_proxy_region",
  480. .status_region_name = "queue_state_debug_region",
  481. .data_first_reg = 16,
  482. .data_last_reg = 31,
  483. .status_cnt_mask = Q_STATE_ENTRY_COUNT_MASK,
  484. .tx_polled = false,
  485. .valid_queues = k2g_valid_queues,
  486. .num_valid_queues = ARRAY_SIZE(k2g_valid_queues),
  487. };
  488. static const struct of_device_id ti_msgmgr_of_match[] = {
  489. {.compatible = "ti,k2g-message-manager", .data = &k2g_desc},
  490. { /* Sentinel */ }
  491. };
  492. MODULE_DEVICE_TABLE(of, ti_msgmgr_of_match);
  493. static int ti_msgmgr_probe(struct platform_device *pdev)
  494. {
  495. struct device *dev = &pdev->dev;
  496. const struct of_device_id *of_id;
  497. struct device_node *np;
  498. struct resource *res;
  499. const struct ti_msgmgr_desc *desc;
  500. struct ti_msgmgr_inst *inst;
  501. struct ti_queue_inst *qinst;
  502. struct mbox_controller *mbox;
  503. struct mbox_chan *chans;
  504. int queue_count;
  505. int i;
  506. int ret = -EINVAL;
  507. const struct ti_msgmgr_valid_queue_desc *queue_desc;
  508. if (!dev->of_node) {
  509. dev_err(dev, "no OF information\n");
  510. return -EINVAL;
  511. }
  512. np = dev->of_node;
  513. of_id = of_match_device(ti_msgmgr_of_match, dev);
  514. if (!of_id) {
  515. dev_err(dev, "OF data missing\n");
  516. return -EINVAL;
  517. }
  518. desc = of_id->data;
  519. inst = devm_kzalloc(dev, sizeof(*inst), GFP_KERNEL);
  520. if (!inst)
  521. return -ENOMEM;
  522. inst->dev = dev;
  523. inst->desc = desc;
  524. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  525. desc->data_region_name);
  526. inst->queue_proxy_region = devm_ioremap_resource(dev, res);
  527. if (IS_ERR(inst->queue_proxy_region))
  528. return PTR_ERR(inst->queue_proxy_region);
  529. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  530. desc->status_region_name);
  531. inst->queue_state_debug_region = devm_ioremap_resource(dev, res);
  532. if (IS_ERR(inst->queue_state_debug_region))
  533. return PTR_ERR(inst->queue_state_debug_region);
  534. dev_dbg(dev, "proxy region=%p, queue_state=%p\n",
  535. inst->queue_proxy_region, inst->queue_state_debug_region);
  536. queue_count = desc->num_valid_queues;
  537. if (!queue_count || queue_count > desc->queue_count) {
  538. dev_crit(dev, "Invalid Number of queues %d. Max %d\n",
  539. queue_count, desc->queue_count);
  540. return -ERANGE;
  541. }
  542. inst->num_valid_queues = queue_count;
  543. qinst = devm_kcalloc(dev, queue_count, sizeof(*qinst), GFP_KERNEL);
  544. if (!qinst)
  545. return -ENOMEM;
  546. inst->qinsts = qinst;
  547. chans = devm_kcalloc(dev, queue_count, sizeof(*chans), GFP_KERNEL);
  548. if (!chans)
  549. return -ENOMEM;
  550. inst->chans = chans;
  551. for (i = 0, queue_desc = desc->valid_queues;
  552. i < queue_count; i++, qinst++, chans++, queue_desc++) {
  553. ret = ti_msgmgr_queue_setup(i, dev, np, inst,
  554. desc, queue_desc, qinst, chans);
  555. if (ret)
  556. return ret;
  557. }
  558. mbox = &inst->mbox;
  559. mbox->dev = dev;
  560. mbox->ops = &ti_msgmgr_chan_ops;
  561. mbox->chans = inst->chans;
  562. mbox->num_chans = inst->num_valid_queues;
  563. mbox->txdone_irq = false;
  564. mbox->txdone_poll = desc->tx_polled;
  565. if (desc->tx_polled)
  566. mbox->txpoll_period = desc->tx_poll_timeout_ms;
  567. mbox->of_xlate = ti_msgmgr_of_xlate;
  568. platform_set_drvdata(pdev, inst);
  569. ret = mbox_controller_register(mbox);
  570. if (ret)
  571. dev_err(dev, "Failed to register mbox_controller(%d)\n", ret);
  572. return ret;
  573. }
  574. static int ti_msgmgr_remove(struct platform_device *pdev)
  575. {
  576. struct ti_msgmgr_inst *inst;
  577. inst = platform_get_drvdata(pdev);
  578. mbox_controller_unregister(&inst->mbox);
  579. return 0;
  580. }
  581. static struct platform_driver ti_msgmgr_driver = {
  582. .probe = ti_msgmgr_probe,
  583. .remove = ti_msgmgr_remove,
  584. .driver = {
  585. .name = "ti-msgmgr",
  586. .of_match_table = of_match_ptr(ti_msgmgr_of_match),
  587. },
  588. };
  589. module_platform_driver(ti_msgmgr_driver);
  590. MODULE_LICENSE("GPL v2");
  591. MODULE_DESCRIPTION("TI message manager driver");
  592. MODULE_AUTHOR("Nishanth Menon");
  593. MODULE_ALIAS("platform:ti-msgmgr");