slimbus.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2011-2017, The Linux Foundation
  4. */
  5. #ifndef _DRIVERS_SLIMBUS_H
  6. #define _DRIVERS_SLIMBUS_H
  7. #include <linux/module.h>
  8. #include <linux/device.h>
  9. #include <linux/mutex.h>
  10. #include <linux/completion.h>
  11. #include <linux/slimbus.h>
  12. /* Standard values per SLIMbus spec needed by controllers and devices */
  13. #define SLIM_CL_PER_SUPERFRAME 6144
  14. #define SLIM_CL_PER_SUPERFRAME_DIV8 (SLIM_CL_PER_SUPERFRAME >> 3)
  15. /* SLIMbus message types. Related to interpretation of message code. */
  16. #define SLIM_MSG_MT_CORE 0x0
  17. #define SLIM_MSG_MT_DEST_REFERRED_USER 0x2
  18. #define SLIM_MSG_MT_SRC_REFERRED_USER 0x6
  19. /*
  20. * SLIM Broadcast header format
  21. * BYTE 0: MT[7:5] RL[4:0]
  22. * BYTE 1: RSVD[7] MC[6:0]
  23. * BYTE 2: RSVD[7:6] DT[5:4] PI[3:0]
  24. */
  25. #define SLIM_MSG_MT_MASK GENMASK(2, 0)
  26. #define SLIM_MSG_MT_SHIFT 5
  27. #define SLIM_MSG_RL_MASK GENMASK(4, 0)
  28. #define SLIM_MSG_RL_SHIFT 0
  29. #define SLIM_MSG_MC_MASK GENMASK(6, 0)
  30. #define SLIM_MSG_MC_SHIFT 0
  31. #define SLIM_MSG_DT_MASK GENMASK(1, 0)
  32. #define SLIM_MSG_DT_SHIFT 4
  33. #define SLIM_HEADER_GET_MT(b) ((b >> SLIM_MSG_MT_SHIFT) & SLIM_MSG_MT_MASK)
  34. #define SLIM_HEADER_GET_RL(b) ((b >> SLIM_MSG_RL_SHIFT) & SLIM_MSG_RL_MASK)
  35. #define SLIM_HEADER_GET_MC(b) ((b >> SLIM_MSG_MC_SHIFT) & SLIM_MSG_MC_MASK)
  36. #define SLIM_HEADER_GET_DT(b) ((b >> SLIM_MSG_DT_SHIFT) & SLIM_MSG_DT_MASK)
  37. /* Device management messages used by this framework */
  38. #define SLIM_MSG_MC_REPORT_PRESENT 0x1
  39. #define SLIM_MSG_MC_ASSIGN_LOGICAL_ADDRESS 0x2
  40. #define SLIM_MSG_MC_REPORT_ABSENT 0xF
  41. /* Data channel management messages */
  42. #define SLIM_MSG_MC_CONNECT_SOURCE 0x10
  43. #define SLIM_MSG_MC_CONNECT_SINK 0x11
  44. #define SLIM_MSG_MC_DISCONNECT_PORT 0x14
  45. #define SLIM_MSG_MC_CHANGE_CONTENT 0x18
  46. /* Clock pause Reconfiguration messages */
  47. #define SLIM_MSG_MC_BEGIN_RECONFIGURATION 0x40
  48. #define SLIM_MSG_MC_NEXT_PAUSE_CLOCK 0x4A
  49. #define SLIM_MSG_MC_NEXT_DEFINE_CHANNEL 0x50
  50. #define SLIM_MSG_MC_NEXT_DEFINE_CONTENT 0x51
  51. #define SLIM_MSG_MC_NEXT_ACTIVATE_CHANNEL 0x54
  52. #define SLIM_MSG_MC_NEXT_DEACTIVATE_CHANNEL 0x55
  53. #define SLIM_MSG_MC_NEXT_REMOVE_CHANNEL 0x58
  54. #define SLIM_MSG_MC_RECONFIGURE_NOW 0x5F
  55. /*
  56. * Clock pause flag to indicate that the reconfig message
  57. * corresponds to clock pause sequence
  58. */
  59. #define SLIM_MSG_CLK_PAUSE_SEQ_FLG (1U << 8)
  60. /* Clock pause values per SLIMbus spec */
  61. #define SLIM_CLK_FAST 0
  62. #define SLIM_CLK_CONST_PHASE 1
  63. #define SLIM_CLK_UNSPECIFIED 2
  64. /* Destination type Values */
  65. #define SLIM_MSG_DEST_LOGICALADDR 0
  66. #define SLIM_MSG_DEST_ENUMADDR 1
  67. #define SLIM_MSG_DEST_BROADCAST 3
  68. /* Standard values per SLIMbus spec needed by controllers and devices */
  69. #define SLIM_MAX_CLK_GEAR 10
  70. #define SLIM_MIN_CLK_GEAR 1
  71. #define SLIM_SLOT_LEN_BITS 4
  72. /* Indicate that the frequency of the flow and the bus frequency are locked */
  73. #define SLIM_CHANNEL_CONTENT_FL BIT(7)
  74. /* Standard values per SLIMbus spec needed by controllers and devices */
  75. #define SLIM_CL_PER_SUPERFRAME 6144
  76. #define SLIM_SLOTS_PER_SUPERFRAME (SLIM_CL_PER_SUPERFRAME >> 2)
  77. #define SLIM_SL_PER_SUPERFRAME (SLIM_CL_PER_SUPERFRAME >> 2)
  78. /* Manager's logical address is set to 0xFF per spec */
  79. #define SLIM_LA_MANAGER 0xFF
  80. #define SLIM_MAX_TIDS 256
  81. /**
  82. * struct slim_framer - Represents SLIMbus framer.
  83. * Every controller may have multiple framers. There is 1 active framer device
  84. * responsible for clocking the bus.
  85. * Manager is responsible for framer hand-over.
  86. * @dev: Driver model representation of the device.
  87. * @e_addr: Enumeration address of the framer.
  88. * @rootfreq: Root Frequency at which the framer can run. This is maximum
  89. * frequency ('clock gear 10') at which the bus can operate.
  90. * @superfreq: Superframes per root frequency. Every frame is 6144 bits.
  91. */
  92. struct slim_framer {
  93. struct device dev;
  94. struct slim_eaddr e_addr;
  95. int rootfreq;
  96. int superfreq;
  97. };
  98. #define to_slim_framer(d) container_of(d, struct slim_framer, dev)
  99. /**
  100. * struct slim_msg_txn - Message to be sent by the controller.
  101. * This structure has packet header,
  102. * payload and buffer to be filled (if any)
  103. * @rl: Header field. remaining length.
  104. * @mt: Header field. Message type.
  105. * @mc: Header field. LSB is message code for type mt.
  106. * @dt: Header field. Destination type.
  107. * @ec: Element code. Used for elemental access APIs.
  108. * @tid: Transaction ID. Used for messages expecting response.
  109. * (relevant for message-codes involving read operation)
  110. * @la: Logical address of the device this message is going to.
  111. * (Not used when destination type is broadcast.)
  112. * @msg: Elemental access message to be read/written
  113. * @comp: completion if read/write is synchronous, used internally
  114. * for tid based transactions.
  115. */
  116. struct slim_msg_txn {
  117. u8 rl;
  118. u8 mt;
  119. u8 mc;
  120. u8 dt;
  121. u16 ec;
  122. u8 tid;
  123. u8 la;
  124. struct slim_val_inf *msg;
  125. struct completion *comp;
  126. };
  127. /* Frequently used message transaction structures */
  128. #define DEFINE_SLIM_LDEST_TXN(name, mc, rl, la, msg) \
  129. struct slim_msg_txn name = { rl, 0, mc, SLIM_MSG_DEST_LOGICALADDR, 0,\
  130. 0, la, msg, }
  131. #define DEFINE_SLIM_BCAST_TXN(name, mc, rl, la, msg) \
  132. struct slim_msg_txn name = { rl, 0, mc, SLIM_MSG_DEST_BROADCAST, 0,\
  133. 0, la, msg, }
  134. #define DEFINE_SLIM_EDEST_TXN(name, mc, rl, la, msg) \
  135. struct slim_msg_txn name = { rl, 0, mc, SLIM_MSG_DEST_ENUMADDR, 0,\
  136. 0, la, msg, }
  137. /**
  138. * enum slim_clk_state: SLIMbus controller's clock state used internally for
  139. * maintaining current clock state.
  140. * @SLIM_CLK_ACTIVE: SLIMbus clock is active
  141. * @SLIM_CLK_ENTERING_PAUSE: SLIMbus clock pause sequence is being sent on the
  142. * bus. If this succeeds, state changes to SLIM_CLK_PAUSED. If the
  143. * transition fails, state changes back to SLIM_CLK_ACTIVE
  144. * @SLIM_CLK_PAUSED: SLIMbus controller clock has paused.
  145. */
  146. enum slim_clk_state {
  147. SLIM_CLK_ACTIVE,
  148. SLIM_CLK_ENTERING_PAUSE,
  149. SLIM_CLK_PAUSED,
  150. };
  151. /**
  152. * struct slim_sched: Framework uses this structure internally for scheduling.
  153. * @clk_state: Controller's clock state from enum slim_clk_state
  154. * @pause_comp: Signals completion of clock pause sequence. This is useful when
  155. * client tries to call SLIMbus transaction when controller is entering
  156. * clock pause.
  157. * @m_reconf: This mutex is held until current reconfiguration (data channel
  158. * scheduling, message bandwidth reservation) is done. Message APIs can
  159. * use the bus concurrently when this mutex is held since elemental access
  160. * messages can be sent on the bus when reconfiguration is in progress.
  161. */
  162. struct slim_sched {
  163. enum slim_clk_state clk_state;
  164. struct completion pause_comp;
  165. struct mutex m_reconf;
  166. };
  167. /**
  168. * enum slim_port_direction: SLIMbus port direction
  169. *
  170. * @SLIM_PORT_SINK: SLIMbus port is a sink
  171. * @SLIM_PORT_SOURCE: SLIMbus port is a source
  172. */
  173. enum slim_port_direction {
  174. SLIM_PORT_SINK = 0,
  175. SLIM_PORT_SOURCE,
  176. };
  177. /**
  178. * enum slim_port_state: SLIMbus Port/Endpoint state machine
  179. * according to SLIMbus Spec 2.0
  180. * @SLIM_PORT_DISCONNECTED: SLIMbus port is disconnected
  181. * entered from Unconfigure/configured state after
  182. * DISCONNECT_PORT or REMOVE_CHANNEL core command
  183. * @SLIM_PORT_UNCONFIGURED: SLIMbus port is in unconfigured state.
  184. * entered from disconnect state after CONNECT_SOURCE/SINK core command
  185. * @SLIM_PORT_CONFIGURED: SLIMbus port is in configured state.
  186. * entered from unconfigured state after DEFINE_CHANNEL, DEFINE_CONTENT
  187. * and ACTIVATE_CHANNEL core commands. Ready for data transmission.
  188. */
  189. enum slim_port_state {
  190. SLIM_PORT_DISCONNECTED = 0,
  191. SLIM_PORT_UNCONFIGURED,
  192. SLIM_PORT_CONFIGURED,
  193. };
  194. /**
  195. * enum slim_channel_state: SLIMbus channel state machine used by core.
  196. * @SLIM_CH_STATE_DISCONNECTED: SLIMbus channel is disconnected
  197. * @SLIM_CH_STATE_ALLOCATED: SLIMbus channel is allocated
  198. * @SLIM_CH_STATE_ASSOCIATED: SLIMbus channel is associated with port
  199. * @SLIM_CH_STATE_DEFINED: SLIMbus channel parameters are defined
  200. * @SLIM_CH_STATE_CONTENT_DEFINED: SLIMbus channel content is defined
  201. * @SLIM_CH_STATE_ACTIVE: SLIMbus channel is active and ready for data
  202. * @SLIM_CH_STATE_REMOVED: SLIMbus channel is inactive and removed
  203. */
  204. enum slim_channel_state {
  205. SLIM_CH_STATE_DISCONNECTED = 0,
  206. SLIM_CH_STATE_ALLOCATED,
  207. SLIM_CH_STATE_ASSOCIATED,
  208. SLIM_CH_STATE_DEFINED,
  209. SLIM_CH_STATE_CONTENT_DEFINED,
  210. SLIM_CH_STATE_ACTIVE,
  211. SLIM_CH_STATE_REMOVED,
  212. };
  213. /**
  214. * enum slim_ch_data_fmt: SLIMbus channel data Type identifiers according to
  215. * Table 60 of SLIMbus Spec 1.01.01
  216. * @SLIM_CH_DATA_FMT_NOT_DEFINED: Undefined
  217. * @SLIM_CH_DATA_FMT_LPCM_AUDIO: LPCM audio
  218. * @SLIM_CH_DATA_FMT_IEC61937_COMP_AUDIO: IEC61937 Compressed audio
  219. * @SLIM_CH_DATA_FMT_PACKED_PDM_AUDIO: Packed PDM audio
  220. */
  221. enum slim_ch_data_fmt {
  222. SLIM_CH_DATA_FMT_NOT_DEFINED = 0,
  223. SLIM_CH_DATA_FMT_LPCM_AUDIO = 1,
  224. SLIM_CH_DATA_FMT_IEC61937_COMP_AUDIO = 2,
  225. SLIM_CH_DATA_FMT_PACKED_PDM_AUDIO = 3,
  226. };
  227. /**
  228. * enum slim_ch_aux_fmt: SLIMbus channel Aux Field format IDs according to
  229. * Table 63 of SLIMbus Spec 2.0
  230. * @SLIM_CH_AUX_FMT_NOT_APPLICABLE: Undefined
  231. * @SLIM_CH_AUX_FMT_ZCUV_TUNNEL_IEC60958: ZCUV for tunneling IEC60958
  232. * @SLIM_CH_AUX_FMT_USER_DEFINED: User defined
  233. */
  234. enum slim_ch_aux_bit_fmt {
  235. SLIM_CH_AUX_FMT_NOT_APPLICABLE = 0,
  236. SLIM_CH_AUX_FMT_ZCUV_TUNNEL_IEC60958 = 1,
  237. SLIM_CH_AUX_FMT_USER_DEFINED = 0xF,
  238. };
  239. /**
  240. * struct slim_channel - SLIMbus channel, used for state machine
  241. *
  242. * @id: ID of channel
  243. * @prrate: Presense rate of channel from Table 66 of SLIMbus 2.0 Specs
  244. * @seg_dist: segment distribution code from Table 20 of SLIMbus 2.0 Specs
  245. * @data_fmt: Data format of channel.
  246. * @aux_fmt: Aux format for this channel.
  247. * @state: channel state machine
  248. */
  249. struct slim_channel {
  250. int id;
  251. int prrate;
  252. int seg_dist;
  253. enum slim_ch_data_fmt data_fmt;
  254. enum slim_ch_aux_bit_fmt aux_fmt;
  255. enum slim_channel_state state;
  256. };
  257. /**
  258. * struct slim_port - SLIMbus port
  259. *
  260. * @id: Port id
  261. * @direction: Port direction, Source or Sink.
  262. * @state: state machine of port.
  263. * @ch: channel associated with this port.
  264. */
  265. struct slim_port {
  266. int id;
  267. enum slim_port_direction direction;
  268. enum slim_port_state state;
  269. struct slim_channel ch;
  270. };
  271. /**
  272. * enum slim_transport_protocol: SLIMbus Transport protocol list from
  273. * Table 47 of SLIMbus 2.0 specs.
  274. * @SLIM_PROTO_ISO: Isochronous Protocol, no flow control as data rate match
  275. * channel rate flow control embedded in the data.
  276. * @SLIM_PROTO_PUSH: Pushed Protocol, includes flow control, Used to carry
  277. * data whose rate is equal to, or lower than the channel rate.
  278. * @SLIM_PROTO_PULL: Pulled Protocol, similar usage as pushed protocol
  279. * but pull is a unicast.
  280. * @SLIM_PROTO_LOCKED: Locked Protocol
  281. * @SLIM_PROTO_ASYNC_SMPLX: Asynchronous Protocol-Simplex
  282. * @SLIM_PROTO_ASYNC_HALF_DUP: Asynchronous Protocol-Half-duplex
  283. * @SLIM_PROTO_EXT_SMPLX: Extended Asynchronous Protocol-Simplex
  284. * @SLIM_PROTO_EXT_HALF_DUP: Extended Asynchronous Protocol-Half-duplex
  285. */
  286. enum slim_transport_protocol {
  287. SLIM_PROTO_ISO = 0,
  288. SLIM_PROTO_PUSH,
  289. SLIM_PROTO_PULL,
  290. SLIM_PROTO_LOCKED,
  291. SLIM_PROTO_ASYNC_SMPLX,
  292. SLIM_PROTO_ASYNC_HALF_DUP,
  293. SLIM_PROTO_EXT_SMPLX,
  294. SLIM_PROTO_EXT_HALF_DUP,
  295. };
  296. /**
  297. * struct slim_stream_runtime - SLIMbus stream runtime instance
  298. *
  299. * @name: Name of the stream
  300. * @dev: SLIM Device instance associated with this stream
  301. * @direction: direction of stream
  302. * @prot: Transport protocol used in this stream
  303. * @rate: Data rate of samples *
  304. * @bps: bits per sample
  305. * @ratem: rate multipler which is super frame rate/data rate
  306. * @num_ports: number of ports
  307. * @ports: pointer to instance of ports
  308. * @node: list head for stream associated with slim device.
  309. */
  310. struct slim_stream_runtime {
  311. const char *name;
  312. struct slim_device *dev;
  313. int direction;
  314. enum slim_transport_protocol prot;
  315. unsigned int rate;
  316. unsigned int bps;
  317. unsigned int ratem;
  318. int num_ports;
  319. struct slim_port *ports;
  320. struct list_head node;
  321. };
  322. /**
  323. * struct slim_controller - Controls every instance of SLIMbus
  324. * (similar to 'master' on SPI)
  325. * @dev: Device interface to this driver
  326. * @id: Board-specific number identifier for this controller/bus
  327. * @name: Name for this controller
  328. * @min_cg: Minimum clock gear supported by this controller (default value: 1)
  329. * @max_cg: Maximum clock gear supported by this controller (default value: 10)
  330. * @clkgear: Current clock gear in which this bus is running
  331. * @laddr_ida: logical address id allocator
  332. * @a_framer: Active framer which is clocking the bus managed by this controller
  333. * @lock: Mutex protecting controller data structures
  334. * @devices: Slim device list
  335. * @tid_idr: tid id allocator
  336. * @txn_lock: Lock to protect table of transactions
  337. * @sched: scheduler structure used by the controller
  338. * @xfer_msg: Transfer a message on this controller (this can be a broadcast
  339. * control/status message like data channel setup, or a unicast message
  340. * like value element read/write.
  341. * @set_laddr: Setup logical address at laddr for the slave with elemental
  342. * address e_addr. Drivers implementing controller will be expected to
  343. * send unicast message to this device with its logical address.
  344. * @get_laddr: It is possible that controller needs to set fixed logical
  345. * address table and get_laddr can be used in that case so that controller
  346. * can do this assignment. Use case is when the master is on the remote
  347. * processor side, who is resposible for allocating laddr.
  348. * @wakeup: This function pointer implements controller-specific procedure
  349. * to wake it up from clock-pause. Framework will call this to bring
  350. * the controller out of clock pause.
  351. * @enable_stream: This function pointer implements controller-specific procedure
  352. * to enable a stream.
  353. * @disable_stream: This function pointer implements controller-specific procedure
  354. * to disable stream.
  355. *
  356. * 'Manager device' is responsible for device management, bandwidth
  357. * allocation, channel setup, and port associations per channel.
  358. * Device management means Logical address assignment/removal based on
  359. * enumeration (report-present, report-absent) of a device.
  360. * Bandwidth allocation is done dynamically by the manager based on active
  361. * channels on the bus, message-bandwidth requests made by SLIMbus devices.
  362. * Based on current bandwidth usage, manager chooses a frequency to run
  363. * the bus at (in steps of 'clock-gear', 1 through 10, each clock gear
  364. * representing twice the frequency than the previous gear).
  365. * Manager is also responsible for entering (and exiting) low-power-mode
  366. * (known as 'clock pause').
  367. * Manager can do handover of framer if there are multiple framers on the
  368. * bus and a certain usecase warrants using certain framer to avoid keeping
  369. * previous framer being powered-on.
  370. *
  371. * Controller here performs duties of the manager device, and 'interface
  372. * device'. Interface device is responsible for monitoring the bus and
  373. * reporting information such as loss-of-synchronization, data
  374. * slot-collision.
  375. */
  376. struct slim_controller {
  377. struct device *dev;
  378. unsigned int id;
  379. char name[SLIMBUS_NAME_SIZE];
  380. int min_cg;
  381. int max_cg;
  382. int clkgear;
  383. struct ida laddr_ida;
  384. struct slim_framer *a_framer;
  385. struct mutex lock;
  386. struct list_head devices;
  387. struct idr tid_idr;
  388. spinlock_t txn_lock;
  389. struct slim_sched sched;
  390. int (*xfer_msg)(struct slim_controller *ctrl,
  391. struct slim_msg_txn *tx);
  392. int (*set_laddr)(struct slim_controller *ctrl,
  393. struct slim_eaddr *ea, u8 laddr);
  394. int (*get_laddr)(struct slim_controller *ctrl,
  395. struct slim_eaddr *ea, u8 *laddr);
  396. int (*enable_stream)(struct slim_stream_runtime *rt);
  397. int (*disable_stream)(struct slim_stream_runtime *rt);
  398. int (*wakeup)(struct slim_controller *ctrl);
  399. };
  400. int slim_device_report_present(struct slim_controller *ctrl,
  401. struct slim_eaddr *e_addr, u8 *laddr);
  402. void slim_report_absent(struct slim_device *sbdev);
  403. int slim_register_controller(struct slim_controller *ctrl);
  404. int slim_unregister_controller(struct slim_controller *ctrl);
  405. void slim_msg_response(struct slim_controller *ctrl, u8 *reply, u8 tid, u8 l);
  406. int slim_do_transfer(struct slim_controller *ctrl, struct slim_msg_txn *txn);
  407. int slim_ctrl_clk_pause(struct slim_controller *ctrl, bool wakeup, u8 restart);
  408. int slim_alloc_txn_tid(struct slim_controller *ctrl, struct slim_msg_txn *txn);
  409. void slim_free_txn_tid(struct slim_controller *ctrl, struct slim_msg_txn *txn);
  410. static inline bool slim_tid_txn(u8 mt, u8 mc)
  411. {
  412. return (mt == SLIM_MSG_MT_CORE &&
  413. (mc == SLIM_MSG_MC_REQUEST_INFORMATION ||
  414. mc == SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION ||
  415. mc == SLIM_MSG_MC_REQUEST_VALUE ||
  416. mc == SLIM_MSG_MC_REQUEST_CLEAR_INFORMATION));
  417. }
  418. static inline bool slim_ec_txn(u8 mt, u8 mc)
  419. {
  420. return (mt == SLIM_MSG_MT_CORE &&
  421. ((mc >= SLIM_MSG_MC_REQUEST_INFORMATION &&
  422. mc <= SLIM_MSG_MC_REPORT_INFORMATION) ||
  423. (mc >= SLIM_MSG_MC_REQUEST_VALUE &&
  424. mc <= SLIM_MSG_MC_CHANGE_VALUE)));
  425. }
  426. #endif /* _LINUX_SLIMBUS_H */