arm_mhuv3.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ARM Message Handling Unit Version 3 (MHUv3) driver.
  4. *
  5. * Copyright (C) 2024 ARM Ltd.
  6. *
  7. * Based on ARM MHUv2 driver.
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/bitops.h>
  11. #include <linux/bits.h>
  12. #include <linux/cleanup.h>
  13. #include <linux/device.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/mailbox_controller.h>
  16. #include <linux/module.h>
  17. #include <linux/of_address.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/sizes.h>
  21. #include <linux/slab.h>
  22. #include <linux/types.h>
  23. /* ====== MHUv3 Registers ====== */
  24. /* Maximum number of Doorbell channel windows */
  25. #define MHUV3_DBCW_MAX 128
  26. /* Number of DBCH combined interrupt status registers */
  27. #define MHUV3_DBCH_CMB_INT_ST_REG_CNT 4
  28. /* Number of FFCH combined interrupt status registers */
  29. #define MHUV3_FFCH_CMB_INT_ST_REG_CNT 2
  30. #define MHUV3_FLAG_BITS 32
  31. /* Not a typo ... */
  32. #define MHUV3_MAJOR_VERSION 2
  33. enum {
  34. MHUV3_MBOX_CELL_TYPE,
  35. MHUV3_MBOX_CELL_CHWN,
  36. MHUV3_MBOX_CELL_PARAM,
  37. MHUV3_MBOX_CELLS
  38. };
  39. /* Padding bitfields/fields represents hole in the regs MMIO */
  40. /* CTRL_Page */
  41. struct blk_id {
  42. #define id GENMASK(3, 0)
  43. u32 val;
  44. } __packed;
  45. struct feat_spt0 {
  46. #define dbe_spt GENMASK(3, 0)
  47. #define fe_spt GENMASK(7, 4)
  48. #define fce_spt GENMASK(11, 8)
  49. u32 val;
  50. } __packed;
  51. struct feat_spt1 {
  52. #define auto_op_spt GENMASK(3, 0)
  53. u32 val;
  54. } __packed;
  55. struct dbch_cfg0 {
  56. #define num_dbch GENMASK(7, 0)
  57. u32 val;
  58. } __packed;
  59. struct ffch_cfg0 {
  60. #define num_ffch GENMASK(7, 0)
  61. #define x8ba_spt BIT(8)
  62. #define x16ba_spt BIT(9)
  63. #define x32ba_spt BIT(10)
  64. #define x64ba_spt BIT(11)
  65. #define ffch_depth GENMASK(25, 16)
  66. u32 val;
  67. } __packed;
  68. struct fch_cfg0 {
  69. #define num_fch GENMASK(9, 0)
  70. #define fcgi_spt BIT(10) // MBX-only
  71. #define num_fcg GENMASK(15, 11)
  72. #define num_fch_per_grp GENMASK(20, 16)
  73. #define fch_ws GENMASK(28, 21)
  74. u32 val;
  75. } __packed;
  76. struct ctrl {
  77. #define op_req BIT(0)
  78. #define ch_op_mask BIT(1)
  79. u32 val;
  80. } __packed;
  81. struct fch_ctrl {
  82. #define _int_en BIT(2)
  83. u32 val;
  84. } __packed;
  85. struct iidr {
  86. #define implementer GENMASK(11, 0)
  87. #define revision GENMASK(15, 12)
  88. #define variant GENMASK(19, 16)
  89. #define product_id GENMASK(31, 20)
  90. u32 val;
  91. } __packed;
  92. struct aidr {
  93. #define arch_minor_rev GENMASK(3, 0)
  94. #define arch_major_rev GENMASK(7, 4)
  95. u32 val;
  96. } __packed;
  97. struct ctrl_page {
  98. struct blk_id blk_id;
  99. u8 pad[12];
  100. struct feat_spt0 feat_spt0;
  101. struct feat_spt1 feat_spt1;
  102. u8 pad1[8];
  103. struct dbch_cfg0 dbch_cfg0;
  104. u8 pad2[12];
  105. struct ffch_cfg0 ffch_cfg0;
  106. u8 pad3[12];
  107. struct fch_cfg0 fch_cfg0;
  108. u8 pad4[188];
  109. struct ctrl x_ctrl;
  110. /*-- MBX-only registers --*/
  111. u8 pad5[60];
  112. struct fch_ctrl fch_ctrl;
  113. u32 fcg_int_en;
  114. u8 pad6[696];
  115. /*-- End of MBX-only ---- */
  116. u32 dbch_int_st[MHUV3_DBCH_CMB_INT_ST_REG_CNT];
  117. u32 ffch_int_st[MHUV3_FFCH_CMB_INT_ST_REG_CNT];
  118. /*-- MBX-only registers --*/
  119. u8 pad7[88];
  120. u32 fcg_int_st;
  121. u8 pad8[12];
  122. u32 fcg_grp_int_st[32];
  123. u8 pad9[2760];
  124. /*-- End of MBX-only ---- */
  125. struct iidr iidr;
  126. struct aidr aidr;
  127. u32 imp_def_id[12];
  128. } __packed;
  129. /* DBCW_Page */
  130. struct xbcw_ctrl {
  131. #define comb_en BIT(0)
  132. u32 val;
  133. } __packed;
  134. struct pdbcw_int {
  135. #define tfr_ack BIT(0)
  136. u32 val;
  137. } __packed;
  138. struct pdbcw_page {
  139. u32 st;
  140. u8 pad[8];
  141. u32 set;
  142. struct pdbcw_int int_st;
  143. struct pdbcw_int int_clr;
  144. struct pdbcw_int int_en;
  145. struct xbcw_ctrl ctrl;
  146. } __packed;
  147. struct mdbcw_page {
  148. u32 st;
  149. u32 st_msk;
  150. u32 clr;
  151. u8 pad[4];
  152. u32 msk_st;
  153. u32 msk_set;
  154. u32 msk_clr;
  155. struct xbcw_ctrl ctrl;
  156. } __packed;
  157. struct dummy_page {
  158. u8 pad[SZ_4K];
  159. } __packed;
  160. struct mhu3_pbx_frame_reg {
  161. struct ctrl_page ctrl;
  162. struct pdbcw_page dbcw[MHUV3_DBCW_MAX];
  163. struct dummy_page ffcw;
  164. struct dummy_page fcw;
  165. u8 pad[SZ_4K * 11];
  166. struct dummy_page impdef;
  167. } __packed;
  168. struct mhu3_mbx_frame_reg {
  169. struct ctrl_page ctrl;
  170. struct mdbcw_page dbcw[MHUV3_DBCW_MAX];
  171. struct dummy_page ffcw;
  172. struct dummy_page fcw;
  173. u8 pad[SZ_4K * 11];
  174. struct dummy_page impdef;
  175. } __packed;
  176. /* Macro for reading a bitmask within a physically mapped packed struct */
  177. #define readl_relaxed_bitmask(_regptr, _bitmask) \
  178. ({ \
  179. unsigned long _rval; \
  180. _rval = readl_relaxed(_regptr); \
  181. FIELD_GET(_bitmask, _rval); \
  182. })
  183. /* Macro for writing a bitmask within a physically mapped packed struct */
  184. #define writel_relaxed_bitmask(_value, _regptr, _bitmask) \
  185. ({ \
  186. unsigned long _rval; \
  187. typeof(_regptr) _rptr = _regptr; \
  188. typeof(_bitmask) _bmask = _bitmask; \
  189. _rval = readl_relaxed(_rptr); \
  190. _rval &= ~(_bmask); \
  191. _rval |= FIELD_PREP((unsigned long long)_bmask, _value);\
  192. writel_relaxed(_rval, _rptr); \
  193. })
  194. /* ====== MHUv3 data structures ====== */
  195. enum mhuv3_frame {
  196. PBX_FRAME,
  197. MBX_FRAME,
  198. };
  199. static char *mhuv3_str[] = {
  200. "PBX",
  201. "MBX"
  202. };
  203. enum mhuv3_extension_type {
  204. DBE_EXT,
  205. FCE_EXT,
  206. FE_EXT,
  207. NUM_EXT
  208. };
  209. static char *mhuv3_ext_str[] = {
  210. "DBE",
  211. "FCE",
  212. "FE"
  213. };
  214. struct mhuv3;
  215. /**
  216. * struct mhuv3_protocol_ops - MHUv3 operations
  217. *
  218. * @rx_startup: Receiver startup callback.
  219. * @rx_shutdown: Receiver shutdown callback.
  220. * @read_data: Read available Sender in-band LE data (if any).
  221. * @rx_complete: Acknowledge data reception to the Sender. Any out-of-band data
  222. * has to have been already retrieved before calling this.
  223. * @tx_startup: Sender startup callback.
  224. * @tx_shutdown: Sender shutdown callback.
  225. * @last_tx_done: Report back to the Sender if the last transfer has completed.
  226. * @send_data: Send data to the receiver.
  227. *
  228. * Each supported transport protocol provides its own implementation of
  229. * these operations.
  230. */
  231. struct mhuv3_protocol_ops {
  232. int (*rx_startup)(struct mhuv3 *mhu, struct mbox_chan *chan);
  233. void (*rx_shutdown)(struct mhuv3 *mhu, struct mbox_chan *chan);
  234. void *(*read_data)(struct mhuv3 *mhu, struct mbox_chan *chan);
  235. void (*rx_complete)(struct mhuv3 *mhu, struct mbox_chan *chan);
  236. void (*tx_startup)(struct mhuv3 *mhu, struct mbox_chan *chan);
  237. void (*tx_shutdown)(struct mhuv3 *mhu, struct mbox_chan *chan);
  238. int (*last_tx_done)(struct mhuv3 *mhu, struct mbox_chan *chan);
  239. int (*send_data)(struct mhuv3 *mhu, struct mbox_chan *chan, void *arg);
  240. };
  241. /**
  242. * struct mhuv3_mbox_chan_priv - MHUv3 channel private information
  243. *
  244. * @ch_idx: Channel window index associated to this mailbox channel.
  245. * @doorbell: Doorbell bit number within the @ch_idx window.
  246. * Only relevant to Doorbell transport.
  247. * @ops: Transport protocol specific operations for this channel.
  248. *
  249. * Transport specific data attached to mmailbox channel priv data.
  250. */
  251. struct mhuv3_mbox_chan_priv {
  252. u32 ch_idx;
  253. u32 doorbell;
  254. const struct mhuv3_protocol_ops *ops;
  255. };
  256. /**
  257. * struct mhuv3_extension - MHUv3 extension descriptor
  258. *
  259. * @type: Type of extension
  260. * @num_chans: Max number of channels found for this extension.
  261. * @base_ch_idx: First channel number assigned to this extension, picked from
  262. * the set of all mailbox channels descriptors created.
  263. * @mbox_of_xlate: Extension specific helper to parse DT and lookup associated
  264. * channel from the related 'mboxes' property.
  265. * @combined_irq_setup: Extension specific helper to setup the combined irq.
  266. * @channels_init: Extension specific helper to initialize channels.
  267. * @chan_from_comb_irq_get: Extension specific helper to lookup which channel
  268. * triggered the combined irq.
  269. * @pending_db: Array of per-channel pending doorbells.
  270. * @pending_lock: Protect access to pending_db.
  271. */
  272. struct mhuv3_extension {
  273. enum mhuv3_extension_type type;
  274. unsigned int num_chans;
  275. unsigned int base_ch_idx;
  276. struct mbox_chan *(*mbox_of_xlate)(struct mhuv3 *mhu,
  277. unsigned int channel,
  278. unsigned int param);
  279. void (*combined_irq_setup)(struct mhuv3 *mhu);
  280. int (*channels_init)(struct mhuv3 *mhu);
  281. struct mbox_chan *(*chan_from_comb_irq_get)(struct mhuv3 *mhu);
  282. u32 pending_db[MHUV3_DBCW_MAX];
  283. /* Protect access to pending_db */
  284. spinlock_t pending_lock;
  285. };
  286. /**
  287. * struct mhuv3 - MHUv3 mailbox controller data
  288. *
  289. * @frame: Frame type: MBX_FRAME or PBX_FRAME.
  290. * @auto_op_full: Flag to indicate if the MHU supports AutoOp full mode.
  291. * @major: MHUv3 controller architectural major version.
  292. * @minor: MHUv3 controller architectural minor version.
  293. * @implem: MHUv3 controller IIDR implementer.
  294. * @rev: MHUv3 controller IIDR revision.
  295. * @var: MHUv3 controller IIDR variant.
  296. * @prod_id: MHUv3 controller IIDR product_id.
  297. * @num_chans: The total number of channnels discovered across all extensions.
  298. * @cmb_irq: Combined IRQ number if any found defined.
  299. * @ctrl: A reference to the MHUv3 control page for this block.
  300. * @pbx: Base address of the PBX register mapping region.
  301. * @mbx: Base address of the MBX register mapping region.
  302. * @ext: Array holding descriptors for any found implemented extension.
  303. * @mbox: Mailbox controller belonging to the MHU frame.
  304. */
  305. struct mhuv3 {
  306. enum mhuv3_frame frame;
  307. bool auto_op_full;
  308. unsigned int major;
  309. unsigned int minor;
  310. unsigned int implem;
  311. unsigned int rev;
  312. unsigned int var;
  313. unsigned int prod_id;
  314. unsigned int num_chans;
  315. int cmb_irq;
  316. struct ctrl_page __iomem *ctrl;
  317. union {
  318. struct mhu3_pbx_frame_reg __iomem *pbx;
  319. struct mhu3_mbx_frame_reg __iomem *mbx;
  320. };
  321. struct mhuv3_extension *ext[NUM_EXT];
  322. struct mbox_controller mbox;
  323. };
  324. #define mhu_from_mbox(_mbox) container_of(_mbox, struct mhuv3, mbox)
  325. typedef int (*mhuv3_extension_initializer)(struct mhuv3 *mhu);
  326. /* =================== Doorbell transport protocol operations =============== */
  327. static void mhuv3_doorbell_tx_startup(struct mhuv3 *mhu, struct mbox_chan *chan)
  328. {
  329. struct mhuv3_mbox_chan_priv *priv = chan->con_priv;
  330. /* Enable Transfer Acknowledgment events */
  331. writel_relaxed_bitmask(0x1, &mhu->pbx->dbcw[priv->ch_idx].int_en, tfr_ack);
  332. }
  333. static void mhuv3_doorbell_tx_shutdown(struct mhuv3 *mhu, struct mbox_chan *chan)
  334. {
  335. struct mhuv3_mbox_chan_priv *priv = chan->con_priv;
  336. struct mhuv3_extension *e = mhu->ext[DBE_EXT];
  337. unsigned long flags;
  338. /* Disable Channel Transfer Ack events */
  339. writel_relaxed_bitmask(0x0, &mhu->pbx->dbcw[priv->ch_idx].int_en, tfr_ack);
  340. /* Clear Channel Transfer Ack and pending doorbells */
  341. writel_relaxed_bitmask(0x1, &mhu->pbx->dbcw[priv->ch_idx].int_clr, tfr_ack);
  342. spin_lock_irqsave(&e->pending_lock, flags);
  343. e->pending_db[priv->ch_idx] = 0;
  344. spin_unlock_irqrestore(&e->pending_lock, flags);
  345. }
  346. static int mhuv3_doorbell_rx_startup(struct mhuv3 *mhu, struct mbox_chan *chan)
  347. {
  348. struct mhuv3_mbox_chan_priv *priv = chan->con_priv;
  349. /* Unmask Channel Transfer events */
  350. writel_relaxed(BIT(priv->doorbell), &mhu->mbx->dbcw[priv->ch_idx].msk_clr);
  351. return 0;
  352. }
  353. static void mhuv3_doorbell_rx_shutdown(struct mhuv3 *mhu,
  354. struct mbox_chan *chan)
  355. {
  356. struct mhuv3_mbox_chan_priv *priv = chan->con_priv;
  357. /* Mask Channel Transfer events */
  358. writel_relaxed(BIT(priv->doorbell), &mhu->mbx->dbcw[priv->ch_idx].msk_set);
  359. }
  360. static void mhuv3_doorbell_rx_complete(struct mhuv3 *mhu, struct mbox_chan *chan)
  361. {
  362. struct mhuv3_mbox_chan_priv *priv = chan->con_priv;
  363. /* Clearing the pending transfer generates the Channel Transfer Ack */
  364. writel_relaxed(BIT(priv->doorbell), &mhu->mbx->dbcw[priv->ch_idx].clr);
  365. }
  366. static int mhuv3_doorbell_last_tx_done(struct mhuv3 *mhu,
  367. struct mbox_chan *chan)
  368. {
  369. struct mhuv3_mbox_chan_priv *priv = chan->con_priv;
  370. int done;
  371. done = !(readl_relaxed(&mhu->pbx->dbcw[priv->ch_idx].st) &
  372. BIT(priv->doorbell));
  373. if (done) {
  374. struct mhuv3_extension *e = mhu->ext[DBE_EXT];
  375. unsigned long flags;
  376. /* Take care to clear the pending doorbell also when polling */
  377. spin_lock_irqsave(&e->pending_lock, flags);
  378. e->pending_db[priv->ch_idx] &= ~BIT(priv->doorbell);
  379. spin_unlock_irqrestore(&e->pending_lock, flags);
  380. }
  381. return done;
  382. }
  383. static int mhuv3_doorbell_send_data(struct mhuv3 *mhu, struct mbox_chan *chan,
  384. void *arg)
  385. {
  386. struct mhuv3_mbox_chan_priv *priv = chan->con_priv;
  387. struct mhuv3_extension *e = mhu->ext[DBE_EXT];
  388. scoped_guard(spinlock_irqsave, &e->pending_lock) {
  389. /* Only one in-flight Transfer is allowed per-doorbell */
  390. if (e->pending_db[priv->ch_idx] & BIT(priv->doorbell))
  391. return -EBUSY;
  392. e->pending_db[priv->ch_idx] |= BIT(priv->doorbell);
  393. }
  394. writel_relaxed(BIT(priv->doorbell), &mhu->pbx->dbcw[priv->ch_idx].set);
  395. return 0;
  396. }
  397. static const struct mhuv3_protocol_ops mhuv3_doorbell_ops = {
  398. .tx_startup = mhuv3_doorbell_tx_startup,
  399. .tx_shutdown = mhuv3_doorbell_tx_shutdown,
  400. .rx_startup = mhuv3_doorbell_rx_startup,
  401. .rx_shutdown = mhuv3_doorbell_rx_shutdown,
  402. .rx_complete = mhuv3_doorbell_rx_complete,
  403. .last_tx_done = mhuv3_doorbell_last_tx_done,
  404. .send_data = mhuv3_doorbell_send_data,
  405. };
  406. /* Sender and receiver mailbox ops */
  407. static bool mhuv3_sender_last_tx_done(struct mbox_chan *chan)
  408. {
  409. struct mhuv3_mbox_chan_priv *priv = chan->con_priv;
  410. struct mhuv3 *mhu = mhu_from_mbox(chan->mbox);
  411. return priv->ops->last_tx_done(mhu, chan);
  412. }
  413. static int mhuv3_sender_send_data(struct mbox_chan *chan, void *data)
  414. {
  415. struct mhuv3_mbox_chan_priv *priv = chan->con_priv;
  416. struct mhuv3 *mhu = mhu_from_mbox(chan->mbox);
  417. if (!priv->ops->last_tx_done(mhu, chan))
  418. return -EBUSY;
  419. return priv->ops->send_data(mhu, chan, data);
  420. }
  421. static int mhuv3_sender_startup(struct mbox_chan *chan)
  422. {
  423. struct mhuv3_mbox_chan_priv *priv = chan->con_priv;
  424. struct mhuv3 *mhu = mhu_from_mbox(chan->mbox);
  425. if (priv->ops->tx_startup)
  426. priv->ops->tx_startup(mhu, chan);
  427. return 0;
  428. }
  429. static void mhuv3_sender_shutdown(struct mbox_chan *chan)
  430. {
  431. struct mhuv3_mbox_chan_priv *priv = chan->con_priv;
  432. struct mhuv3 *mhu = mhu_from_mbox(chan->mbox);
  433. if (priv->ops->tx_shutdown)
  434. priv->ops->tx_shutdown(mhu, chan);
  435. }
  436. static const struct mbox_chan_ops mhuv3_sender_ops = {
  437. .send_data = mhuv3_sender_send_data,
  438. .startup = mhuv3_sender_startup,
  439. .shutdown = mhuv3_sender_shutdown,
  440. .last_tx_done = mhuv3_sender_last_tx_done,
  441. };
  442. static int mhuv3_receiver_startup(struct mbox_chan *chan)
  443. {
  444. struct mhuv3_mbox_chan_priv *priv = chan->con_priv;
  445. struct mhuv3 *mhu = mhu_from_mbox(chan->mbox);
  446. return priv->ops->rx_startup(mhu, chan);
  447. }
  448. static void mhuv3_receiver_shutdown(struct mbox_chan *chan)
  449. {
  450. struct mhuv3_mbox_chan_priv *priv = chan->con_priv;
  451. struct mhuv3 *mhu = mhu_from_mbox(chan->mbox);
  452. priv->ops->rx_shutdown(mhu, chan);
  453. }
  454. static int mhuv3_receiver_send_data(struct mbox_chan *chan, void *data)
  455. {
  456. dev_err(chan->mbox->dev,
  457. "Trying to transmit on a MBX MHUv3 frame\n");
  458. return -EIO;
  459. }
  460. static bool mhuv3_receiver_last_tx_done(struct mbox_chan *chan)
  461. {
  462. dev_err(chan->mbox->dev, "Trying to Tx poll on a MBX MHUv3 frame\n");
  463. return true;
  464. }
  465. static const struct mbox_chan_ops mhuv3_receiver_ops = {
  466. .send_data = mhuv3_receiver_send_data,
  467. .startup = mhuv3_receiver_startup,
  468. .shutdown = mhuv3_receiver_shutdown,
  469. .last_tx_done = mhuv3_receiver_last_tx_done,
  470. };
  471. static struct mbox_chan *mhuv3_dbe_mbox_of_xlate(struct mhuv3 *mhu,
  472. unsigned int channel,
  473. unsigned int doorbell)
  474. {
  475. struct mhuv3_extension *e = mhu->ext[DBE_EXT];
  476. struct mbox_controller *mbox = &mhu->mbox;
  477. struct mbox_chan *chans = mbox->chans;
  478. if (channel >= e->num_chans || doorbell >= MHUV3_FLAG_BITS) {
  479. dev_err(mbox->dev, "Couldn't xlate to a valid channel (%d: %d)\n",
  480. channel, doorbell);
  481. return ERR_PTR(-ENODEV);
  482. }
  483. return &chans[e->base_ch_idx + channel * MHUV3_FLAG_BITS + doorbell];
  484. }
  485. static void mhuv3_dbe_combined_irq_setup(struct mhuv3 *mhu)
  486. {
  487. struct mhuv3_extension *e = mhu->ext[DBE_EXT];
  488. int i;
  489. if (mhu->frame == PBX_FRAME) {
  490. struct pdbcw_page __iomem *dbcw = mhu->pbx->dbcw;
  491. for (i = 0; i < e->num_chans; i++) {
  492. writel_relaxed_bitmask(0x1, &dbcw[i].int_clr, tfr_ack);
  493. writel_relaxed_bitmask(0x0, &dbcw[i].int_en, tfr_ack);
  494. writel_relaxed_bitmask(0x1, &dbcw[i].ctrl, comb_en);
  495. }
  496. } else {
  497. struct mdbcw_page __iomem *dbcw = mhu->mbx->dbcw;
  498. for (i = 0; i < e->num_chans; i++) {
  499. writel_relaxed(0xFFFFFFFF, &dbcw[i].clr);
  500. writel_relaxed(0xFFFFFFFF, &dbcw[i].msk_set);
  501. writel_relaxed_bitmask(0x1, &dbcw[i].ctrl, comb_en);
  502. }
  503. }
  504. }
  505. static int mhuv3_dbe_channels_init(struct mhuv3 *mhu)
  506. {
  507. struct mhuv3_extension *e = mhu->ext[DBE_EXT];
  508. struct mbox_controller *mbox = &mhu->mbox;
  509. struct mbox_chan *chans;
  510. int i;
  511. chans = mbox->chans + mbox->num_chans;
  512. e->base_ch_idx = mbox->num_chans;
  513. for (i = 0; i < e->num_chans; i++) {
  514. struct mhuv3_mbox_chan_priv *priv;
  515. int k;
  516. for (k = 0; k < MHUV3_FLAG_BITS; k++) {
  517. priv = devm_kmalloc(mbox->dev, sizeof(*priv), GFP_KERNEL);
  518. if (!priv)
  519. return -ENOMEM;
  520. priv->ch_idx = i;
  521. priv->ops = &mhuv3_doorbell_ops;
  522. priv->doorbell = k;
  523. chans++->con_priv = priv;
  524. mbox->num_chans++;
  525. }
  526. }
  527. spin_lock_init(&e->pending_lock);
  528. return 0;
  529. }
  530. static bool mhuv3_dbe_doorbell_lookup(struct mhuv3 *mhu, unsigned int channel,
  531. unsigned int *db)
  532. {
  533. struct mhuv3_extension *e = mhu->ext[DBE_EXT];
  534. struct device *dev = mhu->mbox.dev;
  535. u32 st;
  536. if (mhu->frame == PBX_FRAME) {
  537. u32 active_dbs, fired_dbs;
  538. st = readl_relaxed_bitmask(&mhu->pbx->dbcw[channel].int_st,
  539. tfr_ack);
  540. if (!st)
  541. goto err_spurious;
  542. active_dbs = readl_relaxed(&mhu->pbx->dbcw[channel].st);
  543. scoped_guard(spinlock_irqsave, &e->pending_lock) {
  544. fired_dbs = e->pending_db[channel] & ~active_dbs;
  545. if (!fired_dbs)
  546. goto err_spurious;
  547. *db = __ffs(fired_dbs);
  548. e->pending_db[channel] &= ~BIT(*db);
  549. }
  550. fired_dbs &= ~BIT(*db);
  551. /* Clear TFR Ack if no more doorbells pending */
  552. if (!fired_dbs)
  553. writel_relaxed_bitmask(0x1,
  554. &mhu->pbx->dbcw[channel].int_clr,
  555. tfr_ack);
  556. } else {
  557. st = readl_relaxed(&mhu->mbx->dbcw[channel].st_msk);
  558. if (!st)
  559. goto err_spurious;
  560. *db = __ffs(st);
  561. }
  562. return true;
  563. err_spurious:
  564. dev_warn(dev, "Spurious IRQ on %s channel:%d\n",
  565. mhuv3_str[mhu->frame], channel);
  566. return false;
  567. }
  568. static struct mbox_chan *mhuv3_dbe_chan_from_comb_irq_get(struct mhuv3 *mhu)
  569. {
  570. struct mhuv3_extension *e = mhu->ext[DBE_EXT];
  571. struct device *dev = mhu->mbox.dev;
  572. int i;
  573. for (i = 0; i < MHUV3_DBCH_CMB_INT_ST_REG_CNT; i++) {
  574. unsigned int channel, db;
  575. u32 cmb_st;
  576. cmb_st = readl_relaxed(&mhu->ctrl->dbch_int_st[i]);
  577. if (!cmb_st)
  578. continue;
  579. channel = i * MHUV3_FLAG_BITS + __ffs(cmb_st);
  580. if (channel >= e->num_chans) {
  581. dev_err(dev, "Invalid %s channel:%d\n",
  582. mhuv3_str[mhu->frame], channel);
  583. return ERR_PTR(-EIO);
  584. }
  585. if (!mhuv3_dbe_doorbell_lookup(mhu, channel, &db))
  586. continue;
  587. dev_dbg(dev, "Found %s ch[%d]/db[%d]\n",
  588. mhuv3_str[mhu->frame], channel, db);
  589. return &mhu->mbox.chans[channel * MHUV3_FLAG_BITS + db];
  590. }
  591. return ERR_PTR(-EIO);
  592. }
  593. static int mhuv3_dbe_init(struct mhuv3 *mhu)
  594. {
  595. struct device *dev = mhu->mbox.dev;
  596. struct mhuv3_extension *e;
  597. if (!readl_relaxed_bitmask(&mhu->ctrl->feat_spt0, dbe_spt))
  598. return 0;
  599. dev_dbg(dev, "%s: Initializing DBE Extension.\n", mhuv3_str[mhu->frame]);
  600. e = devm_kzalloc(dev, sizeof(*e), GFP_KERNEL);
  601. if (!e)
  602. return -ENOMEM;
  603. e->type = DBE_EXT;
  604. /* Note that, by the spec, the number of channels is (num_dbch + 1) */
  605. e->num_chans =
  606. readl_relaxed_bitmask(&mhu->ctrl->dbch_cfg0, num_dbch) + 1;
  607. e->mbox_of_xlate = mhuv3_dbe_mbox_of_xlate;
  608. e->combined_irq_setup = mhuv3_dbe_combined_irq_setup;
  609. e->channels_init = mhuv3_dbe_channels_init;
  610. e->chan_from_comb_irq_get = mhuv3_dbe_chan_from_comb_irq_get;
  611. mhu->num_chans += e->num_chans * MHUV3_FLAG_BITS;
  612. mhu->ext[DBE_EXT] = e;
  613. dev_dbg(dev, "%s: found %d DBE channels.\n",
  614. mhuv3_str[mhu->frame], e->num_chans);
  615. return 0;
  616. }
  617. static int mhuv3_fce_init(struct mhuv3 *mhu)
  618. {
  619. struct device *dev = mhu->mbox.dev;
  620. if (!readl_relaxed_bitmask(&mhu->ctrl->feat_spt0, fce_spt))
  621. return 0;
  622. dev_dbg(dev, "%s: FCE Extension not supported by driver.\n",
  623. mhuv3_str[mhu->frame]);
  624. return 0;
  625. }
  626. static int mhuv3_fe_init(struct mhuv3 *mhu)
  627. {
  628. struct device *dev = mhu->mbox.dev;
  629. if (!readl_relaxed_bitmask(&mhu->ctrl->feat_spt0, fe_spt))
  630. return 0;
  631. dev_dbg(dev, "%s: FE Extension not supported by driver.\n",
  632. mhuv3_str[mhu->frame]);
  633. return 0;
  634. }
  635. static mhuv3_extension_initializer mhuv3_extension_init[NUM_EXT] = {
  636. mhuv3_dbe_init,
  637. mhuv3_fce_init,
  638. mhuv3_fe_init,
  639. };
  640. static int mhuv3_initialize_channels(struct device *dev, struct mhuv3 *mhu)
  641. {
  642. struct mbox_controller *mbox = &mhu->mbox;
  643. int i, ret = 0;
  644. mbox->chans = devm_kcalloc(dev, mhu->num_chans,
  645. sizeof(*mbox->chans), GFP_KERNEL);
  646. if (!mbox->chans)
  647. return dev_err_probe(dev, -ENOMEM,
  648. "Failed to initialize channels\n");
  649. for (i = 0; i < NUM_EXT && !ret; i++)
  650. if (mhu->ext[i])
  651. ret = mhu->ext[i]->channels_init(mhu);
  652. return ret;
  653. }
  654. static struct mbox_chan *mhuv3_mbox_of_xlate(struct mbox_controller *mbox,
  655. const struct of_phandle_args *pa)
  656. {
  657. struct mhuv3 *mhu = mhu_from_mbox(mbox);
  658. unsigned int type, channel, param;
  659. if (pa->args_count != MHUV3_MBOX_CELLS)
  660. return ERR_PTR(-EINVAL);
  661. type = pa->args[MHUV3_MBOX_CELL_TYPE];
  662. if (type >= NUM_EXT)
  663. return ERR_PTR(-EINVAL);
  664. channel = pa->args[MHUV3_MBOX_CELL_CHWN];
  665. param = pa->args[MHUV3_MBOX_CELL_PARAM];
  666. return mhu->ext[type]->mbox_of_xlate(mhu, channel, param);
  667. }
  668. static void mhu_frame_cleanup_actions(void *data)
  669. {
  670. struct mhuv3 *mhu = data;
  671. writel_relaxed_bitmask(0x0, &mhu->ctrl->x_ctrl, op_req);
  672. }
  673. static int mhuv3_frame_init(struct mhuv3 *mhu, void __iomem *regs)
  674. {
  675. struct device *dev = mhu->mbox.dev;
  676. int i;
  677. mhu->ctrl = regs;
  678. mhu->frame = readl_relaxed_bitmask(&mhu->ctrl->blk_id, id);
  679. if (mhu->frame > MBX_FRAME)
  680. return dev_err_probe(dev, -EINVAL,
  681. "Invalid Frame type- %d\n", mhu->frame);
  682. mhu->major = readl_relaxed_bitmask(&mhu->ctrl->aidr, arch_major_rev);
  683. mhu->minor = readl_relaxed_bitmask(&mhu->ctrl->aidr, arch_minor_rev);
  684. mhu->implem = readl_relaxed_bitmask(&mhu->ctrl->iidr, implementer);
  685. mhu->rev = readl_relaxed_bitmask(&mhu->ctrl->iidr, revision);
  686. mhu->var = readl_relaxed_bitmask(&mhu->ctrl->iidr, variant);
  687. mhu->prod_id = readl_relaxed_bitmask(&mhu->ctrl->iidr, product_id);
  688. if (mhu->major != MHUV3_MAJOR_VERSION)
  689. return dev_err_probe(dev, -EINVAL,
  690. "Unsupported MHU %s block - major:%d minor:%d\n",
  691. mhuv3_str[mhu->frame], mhu->major,
  692. mhu->minor);
  693. mhu->auto_op_full =
  694. !!readl_relaxed_bitmask(&mhu->ctrl->feat_spt1, auto_op_spt);
  695. /* Request the PBX/MBX to remain operational */
  696. if (mhu->auto_op_full) {
  697. writel_relaxed_bitmask(0x1, &mhu->ctrl->x_ctrl, op_req);
  698. devm_add_action_or_reset(dev, mhu_frame_cleanup_actions, mhu);
  699. }
  700. dev_dbg(dev,
  701. "Found MHU %s block - major:%d minor:%d\n implem:0x%X rev:0x%X var:0x%X prod_id:0x%X",
  702. mhuv3_str[mhu->frame], mhu->major, mhu->minor,
  703. mhu->implem, mhu->rev, mhu->var, mhu->prod_id);
  704. if (mhu->frame == PBX_FRAME)
  705. mhu->pbx = regs;
  706. else
  707. mhu->mbx = regs;
  708. for (i = 0; i < NUM_EXT; i++) {
  709. int ret;
  710. /*
  711. * Note that extensions initialization fails only when such
  712. * extension initialization routine fails and the extensions
  713. * was found to be supported in hardware and in software.
  714. */
  715. ret = mhuv3_extension_init[i](mhu);
  716. if (ret)
  717. return dev_err_probe(dev, ret,
  718. "Failed to initialize %s %s\n",
  719. mhuv3_str[mhu->frame],
  720. mhuv3_ext_str[i]);
  721. }
  722. return 0;
  723. }
  724. static irqreturn_t mhuv3_pbx_comb_interrupt(int irq, void *arg)
  725. {
  726. unsigned int i, found = 0;
  727. struct mhuv3 *mhu = arg;
  728. struct mbox_chan *chan;
  729. struct device *dev;
  730. int ret = IRQ_NONE;
  731. dev = mhu->mbox.dev;
  732. for (i = 0; i < NUM_EXT; i++) {
  733. struct mhuv3_mbox_chan_priv *priv;
  734. /* FCE does not participate to the PBX combined */
  735. if (i == FCE_EXT || !mhu->ext[i])
  736. continue;
  737. chan = mhu->ext[i]->chan_from_comb_irq_get(mhu);
  738. if (IS_ERR(chan))
  739. continue;
  740. found++;
  741. priv = chan->con_priv;
  742. if (!chan->cl) {
  743. dev_warn(dev, "TX Ack on UNBOUND channel (%u)\n",
  744. priv->ch_idx);
  745. continue;
  746. }
  747. mbox_chan_txdone(chan, 0);
  748. ret = IRQ_HANDLED;
  749. }
  750. if (found == 0)
  751. dev_warn_once(dev, "Failed to find channel for the TX interrupt\n");
  752. return ret;
  753. }
  754. static irqreturn_t mhuv3_mbx_comb_interrupt(int irq, void *arg)
  755. {
  756. unsigned int i, found = 0;
  757. struct mhuv3 *mhu = arg;
  758. struct mbox_chan *chan;
  759. struct device *dev;
  760. int ret = IRQ_NONE;
  761. dev = mhu->mbox.dev;
  762. for (i = 0; i < NUM_EXT; i++) {
  763. struct mhuv3_mbox_chan_priv *priv;
  764. void *data __free(kfree) = NULL;
  765. if (!mhu->ext[i])
  766. continue;
  767. /* Process any extension which could be source of the IRQ */
  768. chan = mhu->ext[i]->chan_from_comb_irq_get(mhu);
  769. if (IS_ERR(chan))
  770. continue;
  771. found++;
  772. /* From here on we need to call rx_complete even on error */
  773. priv = chan->con_priv;
  774. if (!chan->cl) {
  775. dev_warn(dev, "RX Data on UNBOUND channel (%u)\n",
  776. priv->ch_idx);
  777. goto rx_ack;
  778. }
  779. /* Read optional in-band LE data first. */
  780. if (priv->ops->read_data) {
  781. data = priv->ops->read_data(mhu, chan);
  782. if (IS_ERR(data)) {
  783. dev_err(dev,
  784. "Failed to read in-band data. err:%ld\n",
  785. PTR_ERR(no_free_ptr(data)));
  786. goto rx_ack;
  787. }
  788. }
  789. mbox_chan_received_data(chan, data);
  790. ret = IRQ_HANDLED;
  791. /*
  792. * Acknowledge transfer after any possible optional
  793. * out-of-band data has also been retrieved via
  794. * mbox_chan_received_data().
  795. */
  796. rx_ack:
  797. if (priv->ops->rx_complete)
  798. priv->ops->rx_complete(mhu, chan);
  799. }
  800. if (found == 0)
  801. dev_warn_once(dev, "Failed to find channel for the RX interrupt\n");
  802. return ret;
  803. }
  804. static int mhuv3_setup_pbx(struct mhuv3 *mhu)
  805. {
  806. struct device *dev = mhu->mbox.dev;
  807. mhu->mbox.ops = &mhuv3_sender_ops;
  808. if (mhu->cmb_irq > 0) {
  809. int ret, i;
  810. ret = devm_request_threaded_irq(dev, mhu->cmb_irq, NULL,
  811. mhuv3_pbx_comb_interrupt,
  812. IRQF_ONESHOT, "mhuv3-pbx", mhu);
  813. if (ret)
  814. return dev_err_probe(dev, ret,
  815. "Failed to request PBX IRQ\n");
  816. mhu->mbox.txdone_irq = true;
  817. mhu->mbox.txdone_poll = false;
  818. for (i = 0; i < NUM_EXT; i++)
  819. if (mhu->ext[i])
  820. mhu->ext[i]->combined_irq_setup(mhu);
  821. dev_dbg(dev, "MHUv3 PBX IRQs initialized.\n");
  822. return 0;
  823. }
  824. dev_info(dev, "Using PBX in Tx polling mode.\n");
  825. mhu->mbox.txdone_irq = false;
  826. mhu->mbox.txdone_poll = true;
  827. mhu->mbox.txpoll_period = 1;
  828. return 0;
  829. }
  830. static int mhuv3_setup_mbx(struct mhuv3 *mhu)
  831. {
  832. struct device *dev = mhu->mbox.dev;
  833. int ret, i;
  834. mhu->mbox.ops = &mhuv3_receiver_ops;
  835. if (mhu->cmb_irq <= 0)
  836. return dev_err_probe(dev, -EINVAL,
  837. "MBX combined IRQ is missing !\n");
  838. ret = devm_request_threaded_irq(dev, mhu->cmb_irq, NULL,
  839. mhuv3_mbx_comb_interrupt, IRQF_ONESHOT,
  840. "mhuv3-mbx", mhu);
  841. if (ret)
  842. return dev_err_probe(dev, ret, "Failed to request MBX IRQ\n");
  843. for (i = 0; i < NUM_EXT; i++)
  844. if (mhu->ext[i])
  845. mhu->ext[i]->combined_irq_setup(mhu);
  846. dev_dbg(dev, "MHUv3 MBX IRQs initialized.\n");
  847. return ret;
  848. }
  849. static int mhuv3_irqs_init(struct mhuv3 *mhu, struct platform_device *pdev)
  850. {
  851. dev_dbg(mhu->mbox.dev, "Initializing %s block.\n",
  852. mhuv3_str[mhu->frame]);
  853. if (mhu->frame == PBX_FRAME) {
  854. mhu->cmb_irq =
  855. platform_get_irq_byname_optional(pdev, "combined");
  856. return mhuv3_setup_pbx(mhu);
  857. }
  858. mhu->cmb_irq = platform_get_irq_byname(pdev, "combined");
  859. return mhuv3_setup_mbx(mhu);
  860. }
  861. static int mhuv3_probe(struct platform_device *pdev)
  862. {
  863. struct device *dev = &pdev->dev;
  864. void __iomem *regs;
  865. struct mhuv3 *mhu;
  866. int ret;
  867. mhu = devm_kzalloc(dev, sizeof(*mhu), GFP_KERNEL);
  868. if (!mhu)
  869. return -ENOMEM;
  870. regs = devm_platform_ioremap_resource(pdev, 0);
  871. if (IS_ERR(regs))
  872. return PTR_ERR(regs);
  873. mhu->mbox.dev = dev;
  874. ret = mhuv3_frame_init(mhu, regs);
  875. if (ret)
  876. return ret;
  877. ret = mhuv3_irqs_init(mhu, pdev);
  878. if (ret)
  879. return ret;
  880. mhu->mbox.of_xlate = mhuv3_mbox_of_xlate;
  881. ret = mhuv3_initialize_channels(dev, mhu);
  882. if (ret)
  883. return ret;
  884. ret = devm_mbox_controller_register(dev, &mhu->mbox);
  885. if (ret)
  886. return dev_err_probe(dev, ret,
  887. "Failed to register ARM MHUv3 driver\n");
  888. return ret;
  889. }
  890. static const struct of_device_id mhuv3_of_match[] = {
  891. { .compatible = "arm,mhuv3", .data = NULL },
  892. {}
  893. };
  894. MODULE_DEVICE_TABLE(of, mhuv3_of_match);
  895. static struct platform_driver mhuv3_driver = {
  896. .driver = {
  897. .name = "arm-mhuv3-mailbox",
  898. .of_match_table = mhuv3_of_match,
  899. },
  900. .probe = mhuv3_probe,
  901. };
  902. module_platform_driver(mhuv3_driver);
  903. MODULE_LICENSE("GPL");
  904. MODULE_DESCRIPTION("ARM MHUv3 Driver");
  905. MODULE_AUTHOR("Cristian Marussi <cristian.marussi@arm.com>");