slimbus.h 16 KB

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