ti_sci.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992
  1. /*
  2. * Texas Instruments System Control Interface Protocol Driver
  3. *
  4. * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
  5. * Nishanth Menon
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  12. * kind, whether express or implied; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #define pr_fmt(fmt) "%s: " fmt, __func__
  17. #include <linux/bitmap.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/export.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mailbox_client.h>
  23. #include <linux/module.h>
  24. #include <linux/of_device.h>
  25. #include <linux/semaphore.h>
  26. #include <linux/slab.h>
  27. #include <linux/soc/ti/ti-msgmgr.h>
  28. #include <linux/soc/ti/ti_sci_protocol.h>
  29. #include <linux/reboot.h>
  30. #include "ti_sci.h"
  31. /* List of all TI SCI devices active in system */
  32. static LIST_HEAD(ti_sci_list);
  33. /* Protection for the entire list */
  34. static DEFINE_MUTEX(ti_sci_list_mutex);
  35. /**
  36. * struct ti_sci_xfer - Structure representing a message flow
  37. * @tx_message: Transmit message
  38. * @rx_len: Receive message length
  39. * @xfer_buf: Preallocated buffer to store receive message
  40. * Since we work with request-ACK protocol, we can
  41. * reuse the same buffer for the rx path as we
  42. * use for the tx path.
  43. * @done: completion event
  44. */
  45. struct ti_sci_xfer {
  46. struct ti_msgmgr_message tx_message;
  47. u8 rx_len;
  48. u8 *xfer_buf;
  49. struct completion done;
  50. };
  51. /**
  52. * struct ti_sci_xfers_info - Structure to manage transfer information
  53. * @sem_xfer_count: Counting Semaphore for managing max simultaneous
  54. * Messages.
  55. * @xfer_block: Preallocated Message array
  56. * @xfer_alloc_table: Bitmap table for allocated messages.
  57. * Index of this bitmap table is also used for message
  58. * sequence identifier.
  59. * @xfer_lock: Protection for message allocation
  60. */
  61. struct ti_sci_xfers_info {
  62. struct semaphore sem_xfer_count;
  63. struct ti_sci_xfer *xfer_block;
  64. unsigned long *xfer_alloc_table;
  65. /* protect transfer allocation */
  66. spinlock_t xfer_lock;
  67. };
  68. /**
  69. * struct ti_sci_desc - Description of SoC integration
  70. * @host_id: Host identifier representing the compute entity
  71. * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
  72. * @max_msgs: Maximum number of messages that can be pending
  73. * simultaneously in the system
  74. * @max_msg_size: Maximum size of data per message that can be handled.
  75. */
  76. struct ti_sci_desc {
  77. u8 host_id;
  78. int max_rx_timeout_ms;
  79. int max_msgs;
  80. int max_msg_size;
  81. };
  82. /**
  83. * struct ti_sci_info - Structure representing a TI SCI instance
  84. * @dev: Device pointer
  85. * @desc: SoC description for this instance
  86. * @nb: Reboot Notifier block
  87. * @d: Debugfs file entry
  88. * @debug_region: Memory region where the debug message are available
  89. * @debug_region_size: Debug region size
  90. * @debug_buffer: Buffer allocated to copy debug messages.
  91. * @handle: Instance of TI SCI handle to send to clients.
  92. * @cl: Mailbox Client
  93. * @chan_tx: Transmit mailbox channel
  94. * @chan_rx: Receive mailbox channel
  95. * @minfo: Message info
  96. * @node: list head
  97. * @users: Number of users of this instance
  98. */
  99. struct ti_sci_info {
  100. struct device *dev;
  101. struct notifier_block nb;
  102. const struct ti_sci_desc *desc;
  103. struct dentry *d;
  104. void __iomem *debug_region;
  105. char *debug_buffer;
  106. size_t debug_region_size;
  107. struct ti_sci_handle handle;
  108. struct mbox_client cl;
  109. struct mbox_chan *chan_tx;
  110. struct mbox_chan *chan_rx;
  111. struct ti_sci_xfers_info minfo;
  112. struct list_head node;
  113. /* protected by ti_sci_list_mutex */
  114. int users;
  115. };
  116. #define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl)
  117. #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
  118. #define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb)
  119. #ifdef CONFIG_DEBUG_FS
  120. /**
  121. * ti_sci_debug_show() - Helper to dump the debug log
  122. * @s: sequence file pointer
  123. * @unused: unused.
  124. *
  125. * Return: 0
  126. */
  127. static int ti_sci_debug_show(struct seq_file *s, void *unused)
  128. {
  129. struct ti_sci_info *info = s->private;
  130. memcpy_fromio(info->debug_buffer, info->debug_region,
  131. info->debug_region_size);
  132. /*
  133. * We don't trust firmware to leave NULL terminated last byte (hence
  134. * we have allocated 1 extra 0 byte). Since we cannot guarantee any
  135. * specific data format for debug messages, We just present the data
  136. * in the buffer as is - we expect the messages to be self explanatory.
  137. */
  138. seq_puts(s, info->debug_buffer);
  139. return 0;
  140. }
  141. /**
  142. * ti_sci_debug_open() - debug file open
  143. * @inode: inode pointer
  144. * @file: file pointer
  145. *
  146. * Return: result of single_open
  147. */
  148. static int ti_sci_debug_open(struct inode *inode, struct file *file)
  149. {
  150. return single_open(file, ti_sci_debug_show, inode->i_private);
  151. }
  152. /* log file operations */
  153. static const struct file_operations ti_sci_debug_fops = {
  154. .open = ti_sci_debug_open,
  155. .read = seq_read,
  156. .llseek = seq_lseek,
  157. .release = single_release,
  158. };
  159. /**
  160. * ti_sci_debugfs_create() - Create log debug file
  161. * @pdev: platform device pointer
  162. * @info: Pointer to SCI entity information
  163. *
  164. * Return: 0 if all went fine, else corresponding error.
  165. */
  166. static int ti_sci_debugfs_create(struct platform_device *pdev,
  167. struct ti_sci_info *info)
  168. {
  169. struct device *dev = &pdev->dev;
  170. struct resource *res;
  171. char debug_name[50] = "ti_sci_debug@";
  172. /* Debug region is optional */
  173. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  174. "debug_messages");
  175. info->debug_region = devm_ioremap_resource(dev, res);
  176. if (IS_ERR(info->debug_region))
  177. return 0;
  178. info->debug_region_size = resource_size(res);
  179. info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
  180. sizeof(char), GFP_KERNEL);
  181. if (!info->debug_buffer)
  182. return -ENOMEM;
  183. /* Setup NULL termination */
  184. info->debug_buffer[info->debug_region_size] = 0;
  185. info->d = debugfs_create_file(strncat(debug_name, dev_name(dev),
  186. sizeof(debug_name)),
  187. 0444, NULL, info, &ti_sci_debug_fops);
  188. if (IS_ERR(info->d))
  189. return PTR_ERR(info->d);
  190. dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n",
  191. info->debug_region, info->debug_region_size, res);
  192. return 0;
  193. }
  194. /**
  195. * ti_sci_debugfs_destroy() - clean up log debug file
  196. * @pdev: platform device pointer
  197. * @info: Pointer to SCI entity information
  198. */
  199. static void ti_sci_debugfs_destroy(struct platform_device *pdev,
  200. struct ti_sci_info *info)
  201. {
  202. if (IS_ERR(info->debug_region))
  203. return;
  204. debugfs_remove(info->d);
  205. }
  206. #else /* CONFIG_DEBUG_FS */
  207. static inline int ti_sci_debugfs_create(struct platform_device *dev,
  208. struct ti_sci_info *info)
  209. {
  210. return 0;
  211. }
  212. static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
  213. struct ti_sci_info *info)
  214. {
  215. }
  216. #endif /* CONFIG_DEBUG_FS */
  217. /**
  218. * ti_sci_dump_header_dbg() - Helper to dump a message header.
  219. * @dev: Device pointer corresponding to the SCI entity
  220. * @hdr: pointer to header.
  221. */
  222. static inline void ti_sci_dump_header_dbg(struct device *dev,
  223. struct ti_sci_msg_hdr *hdr)
  224. {
  225. dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
  226. hdr->type, hdr->host, hdr->seq, hdr->flags);
  227. }
  228. /**
  229. * ti_sci_rx_callback() - mailbox client callback for receive messages
  230. * @cl: client pointer
  231. * @m: mailbox message
  232. *
  233. * Processes one received message to appropriate transfer information and
  234. * signals completion of the transfer.
  235. *
  236. * NOTE: This function will be invoked in IRQ context, hence should be
  237. * as optimal as possible.
  238. */
  239. static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
  240. {
  241. struct ti_sci_info *info = cl_to_ti_sci_info(cl);
  242. struct device *dev = info->dev;
  243. struct ti_sci_xfers_info *minfo = &info->minfo;
  244. struct ti_msgmgr_message *mbox_msg = m;
  245. struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
  246. struct ti_sci_xfer *xfer;
  247. u8 xfer_id;
  248. xfer_id = hdr->seq;
  249. /*
  250. * Are we even expecting this?
  251. * NOTE: barriers were implicit in locks used for modifying the bitmap
  252. */
  253. if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
  254. dev_err(dev, "Message for %d is not expected!\n", xfer_id);
  255. return;
  256. }
  257. xfer = &minfo->xfer_block[xfer_id];
  258. /* Is the message of valid length? */
  259. if (mbox_msg->len > info->desc->max_msg_size) {
  260. dev_err(dev, "Unable to handle %d xfer(max %d)\n",
  261. mbox_msg->len, info->desc->max_msg_size);
  262. ti_sci_dump_header_dbg(dev, hdr);
  263. return;
  264. }
  265. if (mbox_msg->len < xfer->rx_len) {
  266. dev_err(dev, "Recv xfer %d < expected %d length\n",
  267. mbox_msg->len, xfer->rx_len);
  268. ti_sci_dump_header_dbg(dev, hdr);
  269. return;
  270. }
  271. ti_sci_dump_header_dbg(dev, hdr);
  272. /* Take a copy to the rx buffer.. */
  273. memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
  274. complete(&xfer->done);
  275. }
  276. /**
  277. * ti_sci_get_one_xfer() - Allocate one message
  278. * @info: Pointer to SCI entity information
  279. * @msg_type: Message type
  280. * @msg_flags: Flag to set for the message
  281. * @tx_message_size: transmit message size
  282. * @rx_message_size: receive message size
  283. *
  284. * Helper function which is used by various command functions that are
  285. * exposed to clients of this driver for allocating a message traffic event.
  286. *
  287. * This function can sleep depending on pending requests already in the system
  288. * for the SCI entity. Further, this also holds a spinlock to maintain integrity
  289. * of internal data structures.
  290. *
  291. * Return: 0 if all went fine, else corresponding error.
  292. */
  293. static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info,
  294. u16 msg_type, u32 msg_flags,
  295. size_t tx_message_size,
  296. size_t rx_message_size)
  297. {
  298. struct ti_sci_xfers_info *minfo = &info->minfo;
  299. struct ti_sci_xfer *xfer;
  300. struct ti_sci_msg_hdr *hdr;
  301. unsigned long flags;
  302. unsigned long bit_pos;
  303. u8 xfer_id;
  304. int ret;
  305. int timeout;
  306. /* Ensure we have sane transfer sizes */
  307. if (rx_message_size > info->desc->max_msg_size ||
  308. tx_message_size > info->desc->max_msg_size ||
  309. rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
  310. return ERR_PTR(-ERANGE);
  311. /*
  312. * Ensure we have only controlled number of pending messages.
  313. * Ideally, we might just have to wait a single message, be
  314. * conservative and wait 5 times that..
  315. */
  316. timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
  317. ret = down_timeout(&minfo->sem_xfer_count, timeout);
  318. if (ret < 0)
  319. return ERR_PTR(ret);
  320. /* Keep the locked section as small as possible */
  321. spin_lock_irqsave(&minfo->xfer_lock, flags);
  322. bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
  323. info->desc->max_msgs);
  324. set_bit(bit_pos, minfo->xfer_alloc_table);
  325. spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  326. /*
  327. * We already ensured in probe that we can have max messages that can
  328. * fit in hdr.seq - NOTE: this improves access latencies
  329. * to predictable O(1) access, BUT, it opens us to risk if
  330. * remote misbehaves with corrupted message sequence responses.
  331. * If that happens, we are going to be messed up anyways..
  332. */
  333. xfer_id = (u8)bit_pos;
  334. xfer = &minfo->xfer_block[xfer_id];
  335. hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  336. xfer->tx_message.len = tx_message_size;
  337. xfer->rx_len = (u8)rx_message_size;
  338. reinit_completion(&xfer->done);
  339. hdr->seq = xfer_id;
  340. hdr->type = msg_type;
  341. hdr->host = info->desc->host_id;
  342. hdr->flags = msg_flags;
  343. return xfer;
  344. }
  345. /**
  346. * ti_sci_put_one_xfer() - Release a message
  347. * @minfo: transfer info pointer
  348. * @xfer: message that was reserved by ti_sci_get_one_xfer
  349. *
  350. * This holds a spinlock to maintain integrity of internal data structures.
  351. */
  352. static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
  353. struct ti_sci_xfer *xfer)
  354. {
  355. unsigned long flags;
  356. struct ti_sci_msg_hdr *hdr;
  357. u8 xfer_id;
  358. hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
  359. xfer_id = hdr->seq;
  360. /*
  361. * Keep the locked section as small as possible
  362. * NOTE: we might escape with smp_mb and no lock here..
  363. * but just be conservative and symmetric.
  364. */
  365. spin_lock_irqsave(&minfo->xfer_lock, flags);
  366. clear_bit(xfer_id, minfo->xfer_alloc_table);
  367. spin_unlock_irqrestore(&minfo->xfer_lock, flags);
  368. /* Increment the count for the next user to get through */
  369. up(&minfo->sem_xfer_count);
  370. }
  371. /**
  372. * ti_sci_do_xfer() - Do one transfer
  373. * @info: Pointer to SCI entity information
  374. * @xfer: Transfer to initiate and wait for response
  375. *
  376. * Return: -ETIMEDOUT in case of no response, if transmit error,
  377. * return corresponding error, else if all goes well,
  378. * return 0.
  379. */
  380. static inline int ti_sci_do_xfer(struct ti_sci_info *info,
  381. struct ti_sci_xfer *xfer)
  382. {
  383. int ret;
  384. int timeout;
  385. struct device *dev = info->dev;
  386. ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
  387. if (ret < 0)
  388. return ret;
  389. ret = 0;
  390. /* And we wait for the response. */
  391. timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
  392. if (!wait_for_completion_timeout(&xfer->done, timeout)) {
  393. dev_err(dev, "Mbox timedout in resp(caller: %pF)\n",
  394. (void *)_RET_IP_);
  395. ret = -ETIMEDOUT;
  396. }
  397. /*
  398. * NOTE: we might prefer not to need the mailbox ticker to manage the
  399. * transfer queueing since the protocol layer queues things by itself.
  400. * Unfortunately, we have to kick the mailbox framework after we have
  401. * received our message.
  402. */
  403. mbox_client_txdone(info->chan_tx, ret);
  404. return ret;
  405. }
  406. /**
  407. * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
  408. * @info: Pointer to SCI entity information
  409. *
  410. * Updates the SCI information in the internal data structure.
  411. *
  412. * Return: 0 if all went fine, else return appropriate error.
  413. */
  414. static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
  415. {
  416. struct device *dev = info->dev;
  417. struct ti_sci_handle *handle = &info->handle;
  418. struct ti_sci_version_info *ver = &handle->version;
  419. struct ti_sci_msg_resp_version *rev_info;
  420. struct ti_sci_xfer *xfer;
  421. int ret;
  422. /* No need to setup flags since it is expected to respond */
  423. xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
  424. 0x0, sizeof(struct ti_sci_msg_hdr),
  425. sizeof(*rev_info));
  426. if (IS_ERR(xfer)) {
  427. ret = PTR_ERR(xfer);
  428. dev_err(dev, "Message alloc failed(%d)\n", ret);
  429. return ret;
  430. }
  431. rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
  432. ret = ti_sci_do_xfer(info, xfer);
  433. if (ret) {
  434. dev_err(dev, "Mbox send fail %d\n", ret);
  435. goto fail;
  436. }
  437. ver->abi_major = rev_info->abi_major;
  438. ver->abi_minor = rev_info->abi_minor;
  439. ver->firmware_revision = rev_info->firmware_revision;
  440. strncpy(ver->firmware_description, rev_info->firmware_description,
  441. sizeof(ver->firmware_description));
  442. fail:
  443. ti_sci_put_one_xfer(&info->minfo, xfer);
  444. return ret;
  445. }
  446. /**
  447. * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
  448. * @r: pointer to response buffer
  449. *
  450. * Return: true if the response was an ACK, else returns false.
  451. */
  452. static inline bool ti_sci_is_response_ack(void *r)
  453. {
  454. struct ti_sci_msg_hdr *hdr = r;
  455. return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
  456. }
  457. /**
  458. * ti_sci_set_device_state() - Set device state helper
  459. * @handle: pointer to TI SCI handle
  460. * @id: Device identifier
  461. * @flags: flags to setup for the device
  462. * @state: State to move the device to
  463. *
  464. * Return: 0 if all went well, else returns appropriate error value.
  465. */
  466. static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
  467. u32 id, u32 flags, u8 state)
  468. {
  469. struct ti_sci_info *info;
  470. struct ti_sci_msg_req_set_device_state *req;
  471. struct ti_sci_msg_hdr *resp;
  472. struct ti_sci_xfer *xfer;
  473. struct device *dev;
  474. int ret = 0;
  475. if (IS_ERR(handle))
  476. return PTR_ERR(handle);
  477. if (!handle)
  478. return -EINVAL;
  479. info = handle_to_ti_sci_info(handle);
  480. dev = info->dev;
  481. xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
  482. flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  483. sizeof(*req), sizeof(*resp));
  484. if (IS_ERR(xfer)) {
  485. ret = PTR_ERR(xfer);
  486. dev_err(dev, "Message alloc failed(%d)\n", ret);
  487. return ret;
  488. }
  489. req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf;
  490. req->id = id;
  491. req->state = state;
  492. ret = ti_sci_do_xfer(info, xfer);
  493. if (ret) {
  494. dev_err(dev, "Mbox send fail %d\n", ret);
  495. goto fail;
  496. }
  497. resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  498. ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  499. fail:
  500. ti_sci_put_one_xfer(&info->minfo, xfer);
  501. return ret;
  502. }
  503. /**
  504. * ti_sci_get_device_state() - Get device state helper
  505. * @handle: Handle to the device
  506. * @id: Device Identifier
  507. * @clcnt: Pointer to Context Loss Count
  508. * @resets: pointer to resets
  509. * @p_state: pointer to p_state
  510. * @c_state: pointer to c_state
  511. *
  512. * Return: 0 if all went fine, else return appropriate error.
  513. */
  514. static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
  515. u32 id, u32 *clcnt, u32 *resets,
  516. u8 *p_state, u8 *c_state)
  517. {
  518. struct ti_sci_info *info;
  519. struct ti_sci_msg_req_get_device_state *req;
  520. struct ti_sci_msg_resp_get_device_state *resp;
  521. struct ti_sci_xfer *xfer;
  522. struct device *dev;
  523. int ret = 0;
  524. if (IS_ERR(handle))
  525. return PTR_ERR(handle);
  526. if (!handle)
  527. return -EINVAL;
  528. if (!clcnt && !resets && !p_state && !c_state)
  529. return -EINVAL;
  530. info = handle_to_ti_sci_info(handle);
  531. dev = info->dev;
  532. /* Response is expected, so need of any flags */
  533. xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
  534. 0, sizeof(*req), sizeof(*resp));
  535. if (IS_ERR(xfer)) {
  536. ret = PTR_ERR(xfer);
  537. dev_err(dev, "Message alloc failed(%d)\n", ret);
  538. return ret;
  539. }
  540. req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf;
  541. req->id = id;
  542. ret = ti_sci_do_xfer(info, xfer);
  543. if (ret) {
  544. dev_err(dev, "Mbox send fail %d\n", ret);
  545. goto fail;
  546. }
  547. resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf;
  548. if (!ti_sci_is_response_ack(resp)) {
  549. ret = -ENODEV;
  550. goto fail;
  551. }
  552. if (clcnt)
  553. *clcnt = resp->context_loss_count;
  554. if (resets)
  555. *resets = resp->resets;
  556. if (p_state)
  557. *p_state = resp->programmed_state;
  558. if (c_state)
  559. *c_state = resp->current_state;
  560. fail:
  561. ti_sci_put_one_xfer(&info->minfo, xfer);
  562. return ret;
  563. }
  564. /**
  565. * ti_sci_cmd_get_device() - command to request for device managed by TISCI
  566. * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
  567. * @id: Device Identifier
  568. *
  569. * Request for the device - NOTE: the client MUST maintain integrity of
  570. * usage count by balancing get_device with put_device. No refcounting is
  571. * managed by driver for that purpose.
  572. *
  573. * NOTE: The request is for exclusive access for the processor.
  574. *
  575. * Return: 0 if all went fine, else return appropriate error.
  576. */
  577. static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
  578. {
  579. return ti_sci_set_device_state(handle, id,
  580. MSG_FLAG_DEVICE_EXCLUSIVE,
  581. MSG_DEVICE_SW_STATE_ON);
  582. }
  583. /**
  584. * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
  585. * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
  586. * @id: Device Identifier
  587. *
  588. * Request for the device - NOTE: the client MUST maintain integrity of
  589. * usage count by balancing get_device with put_device. No refcounting is
  590. * managed by driver for that purpose.
  591. *
  592. * Return: 0 if all went fine, else return appropriate error.
  593. */
  594. static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
  595. {
  596. return ti_sci_set_device_state(handle, id,
  597. MSG_FLAG_DEVICE_EXCLUSIVE,
  598. MSG_DEVICE_SW_STATE_RETENTION);
  599. }
  600. /**
  601. * ti_sci_cmd_put_device() - command to release a device managed by TISCI
  602. * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
  603. * @id: Device Identifier
  604. *
  605. * Request for the device - NOTE: the client MUST maintain integrity of
  606. * usage count by balancing get_device with put_device. No refcounting is
  607. * managed by driver for that purpose.
  608. *
  609. * Return: 0 if all went fine, else return appropriate error.
  610. */
  611. static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
  612. {
  613. return ti_sci_set_device_state(handle, id,
  614. 0, MSG_DEVICE_SW_STATE_AUTO_OFF);
  615. }
  616. /**
  617. * ti_sci_cmd_dev_is_valid() - Is the device valid
  618. * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
  619. * @id: Device Identifier
  620. *
  621. * Return: 0 if all went fine and the device ID is valid, else return
  622. * appropriate error.
  623. */
  624. static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
  625. {
  626. u8 unused;
  627. /* check the device state which will also tell us if the ID is valid */
  628. return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
  629. }
  630. /**
  631. * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
  632. * @handle: Pointer to TISCI handle
  633. * @id: Device Identifier
  634. * @count: Pointer to Context Loss counter to populate
  635. *
  636. * Return: 0 if all went fine, else return appropriate error.
  637. */
  638. static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
  639. u32 *count)
  640. {
  641. return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
  642. }
  643. /**
  644. * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
  645. * @handle: Pointer to TISCI handle
  646. * @id: Device Identifier
  647. * @r_state: true if requested to be idle
  648. *
  649. * Return: 0 if all went fine, else return appropriate error.
  650. */
  651. static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
  652. bool *r_state)
  653. {
  654. int ret;
  655. u8 state;
  656. if (!r_state)
  657. return -EINVAL;
  658. ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
  659. if (ret)
  660. return ret;
  661. *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
  662. return 0;
  663. }
  664. /**
  665. * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
  666. * @handle: Pointer to TISCI handle
  667. * @id: Device Identifier
  668. * @r_state: true if requested to be stopped
  669. * @curr_state: true if currently stopped.
  670. *
  671. * Return: 0 if all went fine, else return appropriate error.
  672. */
  673. static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
  674. bool *r_state, bool *curr_state)
  675. {
  676. int ret;
  677. u8 p_state, c_state;
  678. if (!r_state && !curr_state)
  679. return -EINVAL;
  680. ret =
  681. ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
  682. if (ret)
  683. return ret;
  684. if (r_state)
  685. *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
  686. if (curr_state)
  687. *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
  688. return 0;
  689. }
  690. /**
  691. * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
  692. * @handle: Pointer to TISCI handle
  693. * @id: Device Identifier
  694. * @r_state: true if requested to be ON
  695. * @curr_state: true if currently ON and active
  696. *
  697. * Return: 0 if all went fine, else return appropriate error.
  698. */
  699. static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
  700. bool *r_state, bool *curr_state)
  701. {
  702. int ret;
  703. u8 p_state, c_state;
  704. if (!r_state && !curr_state)
  705. return -EINVAL;
  706. ret =
  707. ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
  708. if (ret)
  709. return ret;
  710. if (r_state)
  711. *r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
  712. if (curr_state)
  713. *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
  714. return 0;
  715. }
  716. /**
  717. * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
  718. * @handle: Pointer to TISCI handle
  719. * @id: Device Identifier
  720. * @curr_state: true if currently transitioning.
  721. *
  722. * Return: 0 if all went fine, else return appropriate error.
  723. */
  724. static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
  725. bool *curr_state)
  726. {
  727. int ret;
  728. u8 state;
  729. if (!curr_state)
  730. return -EINVAL;
  731. ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
  732. if (ret)
  733. return ret;
  734. *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
  735. return 0;
  736. }
  737. /**
  738. * ti_sci_cmd_set_device_resets() - command to set resets for device managed
  739. * by TISCI
  740. * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
  741. * @id: Device Identifier
  742. * @reset_state: Device specific reset bit field
  743. *
  744. * Return: 0 if all went fine, else return appropriate error.
  745. */
  746. static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
  747. u32 id, u32 reset_state)
  748. {
  749. struct ti_sci_info *info;
  750. struct ti_sci_msg_req_set_device_resets *req;
  751. struct ti_sci_msg_hdr *resp;
  752. struct ti_sci_xfer *xfer;
  753. struct device *dev;
  754. int ret = 0;
  755. if (IS_ERR(handle))
  756. return PTR_ERR(handle);
  757. if (!handle)
  758. return -EINVAL;
  759. info = handle_to_ti_sci_info(handle);
  760. dev = info->dev;
  761. xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
  762. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  763. sizeof(*req), sizeof(*resp));
  764. if (IS_ERR(xfer)) {
  765. ret = PTR_ERR(xfer);
  766. dev_err(dev, "Message alloc failed(%d)\n", ret);
  767. return ret;
  768. }
  769. req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf;
  770. req->id = id;
  771. req->resets = reset_state;
  772. ret = ti_sci_do_xfer(info, xfer);
  773. if (ret) {
  774. dev_err(dev, "Mbox send fail %d\n", ret);
  775. goto fail;
  776. }
  777. resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  778. ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  779. fail:
  780. ti_sci_put_one_xfer(&info->minfo, xfer);
  781. return ret;
  782. }
  783. /**
  784. * ti_sci_cmd_get_device_resets() - Get reset state for device managed
  785. * by TISCI
  786. * @handle: Pointer to TISCI handle
  787. * @id: Device Identifier
  788. * @reset_state: Pointer to reset state to populate
  789. *
  790. * Return: 0 if all went fine, else return appropriate error.
  791. */
  792. static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
  793. u32 id, u32 *reset_state)
  794. {
  795. return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
  796. NULL);
  797. }
  798. /**
  799. * ti_sci_set_clock_state() - Set clock state helper
  800. * @handle: pointer to TI SCI handle
  801. * @dev_id: Device identifier this request is for
  802. * @clk_id: Clock identifier for the device for this request.
  803. * Each device has it's own set of clock inputs. This indexes
  804. * which clock input to modify.
  805. * @flags: Header flags as needed
  806. * @state: State to request for the clock.
  807. *
  808. * Return: 0 if all went well, else returns appropriate error value.
  809. */
  810. static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
  811. u32 dev_id, u8 clk_id,
  812. u32 flags, u8 state)
  813. {
  814. struct ti_sci_info *info;
  815. struct ti_sci_msg_req_set_clock_state *req;
  816. struct ti_sci_msg_hdr *resp;
  817. struct ti_sci_xfer *xfer;
  818. struct device *dev;
  819. int ret = 0;
  820. if (IS_ERR(handle))
  821. return PTR_ERR(handle);
  822. if (!handle)
  823. return -EINVAL;
  824. info = handle_to_ti_sci_info(handle);
  825. dev = info->dev;
  826. xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
  827. flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  828. sizeof(*req), sizeof(*resp));
  829. if (IS_ERR(xfer)) {
  830. ret = PTR_ERR(xfer);
  831. dev_err(dev, "Message alloc failed(%d)\n", ret);
  832. return ret;
  833. }
  834. req = (struct ti_sci_msg_req_set_clock_state *)xfer->xfer_buf;
  835. req->dev_id = dev_id;
  836. req->clk_id = clk_id;
  837. req->request_state = state;
  838. ret = ti_sci_do_xfer(info, xfer);
  839. if (ret) {
  840. dev_err(dev, "Mbox send fail %d\n", ret);
  841. goto fail;
  842. }
  843. resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  844. ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  845. fail:
  846. ti_sci_put_one_xfer(&info->minfo, xfer);
  847. return ret;
  848. }
  849. /**
  850. * ti_sci_cmd_get_clock_state() - Get clock state helper
  851. * @handle: pointer to TI SCI handle
  852. * @dev_id: Device identifier this request is for
  853. * @clk_id: Clock identifier for the device for this request.
  854. * Each device has it's own set of clock inputs. This indexes
  855. * which clock input to modify.
  856. * @programmed_state: State requested for clock to move to
  857. * @current_state: State that the clock is currently in
  858. *
  859. * Return: 0 if all went well, else returns appropriate error value.
  860. */
  861. static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
  862. u32 dev_id, u8 clk_id,
  863. u8 *programmed_state, u8 *current_state)
  864. {
  865. struct ti_sci_info *info;
  866. struct ti_sci_msg_req_get_clock_state *req;
  867. struct ti_sci_msg_resp_get_clock_state *resp;
  868. struct ti_sci_xfer *xfer;
  869. struct device *dev;
  870. int ret = 0;
  871. if (IS_ERR(handle))
  872. return PTR_ERR(handle);
  873. if (!handle)
  874. return -EINVAL;
  875. if (!programmed_state && !current_state)
  876. return -EINVAL;
  877. info = handle_to_ti_sci_info(handle);
  878. dev = info->dev;
  879. xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
  880. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  881. sizeof(*req), sizeof(*resp));
  882. if (IS_ERR(xfer)) {
  883. ret = PTR_ERR(xfer);
  884. dev_err(dev, "Message alloc failed(%d)\n", ret);
  885. return ret;
  886. }
  887. req = (struct ti_sci_msg_req_get_clock_state *)xfer->xfer_buf;
  888. req->dev_id = dev_id;
  889. req->clk_id = clk_id;
  890. ret = ti_sci_do_xfer(info, xfer);
  891. if (ret) {
  892. dev_err(dev, "Mbox send fail %d\n", ret);
  893. goto fail;
  894. }
  895. resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->xfer_buf;
  896. if (!ti_sci_is_response_ack(resp)) {
  897. ret = -ENODEV;
  898. goto fail;
  899. }
  900. if (programmed_state)
  901. *programmed_state = resp->programmed_state;
  902. if (current_state)
  903. *current_state = resp->current_state;
  904. fail:
  905. ti_sci_put_one_xfer(&info->minfo, xfer);
  906. return ret;
  907. }
  908. /**
  909. * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
  910. * @handle: pointer to TI SCI handle
  911. * @dev_id: Device identifier this request is for
  912. * @clk_id: Clock identifier for the device for this request.
  913. * Each device has it's own set of clock inputs. This indexes
  914. * which clock input to modify.
  915. * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
  916. * @can_change_freq: 'true' if frequency change is desired, else 'false'
  917. * @enable_input_term: 'true' if input termination is desired, else 'false'
  918. *
  919. * Return: 0 if all went well, else returns appropriate error value.
  920. */
  921. static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
  922. u8 clk_id, bool needs_ssc, bool can_change_freq,
  923. bool enable_input_term)
  924. {
  925. u32 flags = 0;
  926. flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
  927. flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
  928. flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
  929. return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
  930. MSG_CLOCK_SW_STATE_REQ);
  931. }
  932. /**
  933. * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
  934. * @handle: pointer to TI SCI handle
  935. * @dev_id: Device identifier this request is for
  936. * @clk_id: Clock identifier for the device for this request.
  937. * Each device has it's own set of clock inputs. This indexes
  938. * which clock input to modify.
  939. *
  940. * NOTE: This clock must have been requested by get_clock previously.
  941. *
  942. * Return: 0 if all went well, else returns appropriate error value.
  943. */
  944. static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
  945. u32 dev_id, u8 clk_id)
  946. {
  947. return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
  948. MSG_CLOCK_SW_STATE_UNREQ);
  949. }
  950. /**
  951. * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
  952. * @handle: pointer to TI SCI handle
  953. * @dev_id: Device identifier this request is for
  954. * @clk_id: Clock identifier for the device for this request.
  955. * Each device has it's own set of clock inputs. This indexes
  956. * which clock input to modify.
  957. *
  958. * NOTE: This clock must have been requested by get_clock previously.
  959. *
  960. * Return: 0 if all went well, else returns appropriate error value.
  961. */
  962. static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
  963. u32 dev_id, u8 clk_id)
  964. {
  965. return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
  966. MSG_CLOCK_SW_STATE_AUTO);
  967. }
  968. /**
  969. * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
  970. * @handle: pointer to TI SCI handle
  971. * @dev_id: Device identifier this request is for
  972. * @clk_id: Clock identifier for the device for this request.
  973. * Each device has it's own set of clock inputs. This indexes
  974. * which clock input to modify.
  975. * @req_state: state indicating if the clock is auto managed
  976. *
  977. * Return: 0 if all went well, else returns appropriate error value.
  978. */
  979. static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
  980. u32 dev_id, u8 clk_id, bool *req_state)
  981. {
  982. u8 state = 0;
  983. int ret;
  984. if (!req_state)
  985. return -EINVAL;
  986. ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
  987. if (ret)
  988. return ret;
  989. *req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
  990. return 0;
  991. }
  992. /**
  993. * ti_sci_cmd_clk_is_on() - Is the clock ON
  994. * @handle: pointer to TI SCI handle
  995. * @dev_id: Device identifier this request is for
  996. * @clk_id: Clock identifier for the device for this request.
  997. * Each device has it's own set of clock inputs. This indexes
  998. * which clock input to modify.
  999. * @req_state: state indicating if the clock is managed by us and enabled
  1000. * @curr_state: state indicating if the clock is ready for operation
  1001. *
  1002. * Return: 0 if all went well, else returns appropriate error value.
  1003. */
  1004. static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
  1005. u8 clk_id, bool *req_state, bool *curr_state)
  1006. {
  1007. u8 c_state = 0, r_state = 0;
  1008. int ret;
  1009. if (!req_state && !curr_state)
  1010. return -EINVAL;
  1011. ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
  1012. &r_state, &c_state);
  1013. if (ret)
  1014. return ret;
  1015. if (req_state)
  1016. *req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
  1017. if (curr_state)
  1018. *curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
  1019. return 0;
  1020. }
  1021. /**
  1022. * ti_sci_cmd_clk_is_off() - Is the clock OFF
  1023. * @handle: pointer to TI SCI handle
  1024. * @dev_id: Device identifier this request is for
  1025. * @clk_id: Clock identifier for the device for this request.
  1026. * Each device has it's own set of clock inputs. This indexes
  1027. * which clock input to modify.
  1028. * @req_state: state indicating if the clock is managed by us and disabled
  1029. * @curr_state: state indicating if the clock is NOT ready for operation
  1030. *
  1031. * Return: 0 if all went well, else returns appropriate error value.
  1032. */
  1033. static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
  1034. u8 clk_id, bool *req_state, bool *curr_state)
  1035. {
  1036. u8 c_state = 0, r_state = 0;
  1037. int ret;
  1038. if (!req_state && !curr_state)
  1039. return -EINVAL;
  1040. ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
  1041. &r_state, &c_state);
  1042. if (ret)
  1043. return ret;
  1044. if (req_state)
  1045. *req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
  1046. if (curr_state)
  1047. *curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
  1048. return 0;
  1049. }
  1050. /**
  1051. * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
  1052. * @handle: pointer to TI SCI handle
  1053. * @dev_id: Device identifier this request is for
  1054. * @clk_id: Clock identifier for the device for this request.
  1055. * Each device has it's own set of clock inputs. This indexes
  1056. * which clock input to modify.
  1057. * @parent_id: Parent clock identifier to set
  1058. *
  1059. * Return: 0 if all went well, else returns appropriate error value.
  1060. */
  1061. static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
  1062. u32 dev_id, u8 clk_id, u8 parent_id)
  1063. {
  1064. struct ti_sci_info *info;
  1065. struct ti_sci_msg_req_set_clock_parent *req;
  1066. struct ti_sci_msg_hdr *resp;
  1067. struct ti_sci_xfer *xfer;
  1068. struct device *dev;
  1069. int ret = 0;
  1070. if (IS_ERR(handle))
  1071. return PTR_ERR(handle);
  1072. if (!handle)
  1073. return -EINVAL;
  1074. info = handle_to_ti_sci_info(handle);
  1075. dev = info->dev;
  1076. xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
  1077. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1078. sizeof(*req), sizeof(*resp));
  1079. if (IS_ERR(xfer)) {
  1080. ret = PTR_ERR(xfer);
  1081. dev_err(dev, "Message alloc failed(%d)\n", ret);
  1082. return ret;
  1083. }
  1084. req = (struct ti_sci_msg_req_set_clock_parent *)xfer->xfer_buf;
  1085. req->dev_id = dev_id;
  1086. req->clk_id = clk_id;
  1087. req->parent_id = parent_id;
  1088. ret = ti_sci_do_xfer(info, xfer);
  1089. if (ret) {
  1090. dev_err(dev, "Mbox send fail %d\n", ret);
  1091. goto fail;
  1092. }
  1093. resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  1094. ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  1095. fail:
  1096. ti_sci_put_one_xfer(&info->minfo, xfer);
  1097. return ret;
  1098. }
  1099. /**
  1100. * ti_sci_cmd_clk_get_parent() - Get current parent clock source
  1101. * @handle: pointer to TI SCI handle
  1102. * @dev_id: Device identifier this request is for
  1103. * @clk_id: Clock identifier for the device for this request.
  1104. * Each device has it's own set of clock inputs. This indexes
  1105. * which clock input to modify.
  1106. * @parent_id: Current clock parent
  1107. *
  1108. * Return: 0 if all went well, else returns appropriate error value.
  1109. */
  1110. static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
  1111. u32 dev_id, u8 clk_id, u8 *parent_id)
  1112. {
  1113. struct ti_sci_info *info;
  1114. struct ti_sci_msg_req_get_clock_parent *req;
  1115. struct ti_sci_msg_resp_get_clock_parent *resp;
  1116. struct ti_sci_xfer *xfer;
  1117. struct device *dev;
  1118. int ret = 0;
  1119. if (IS_ERR(handle))
  1120. return PTR_ERR(handle);
  1121. if (!handle || !parent_id)
  1122. return -EINVAL;
  1123. info = handle_to_ti_sci_info(handle);
  1124. dev = info->dev;
  1125. xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
  1126. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1127. sizeof(*req), sizeof(*resp));
  1128. if (IS_ERR(xfer)) {
  1129. ret = PTR_ERR(xfer);
  1130. dev_err(dev, "Message alloc failed(%d)\n", ret);
  1131. return ret;
  1132. }
  1133. req = (struct ti_sci_msg_req_get_clock_parent *)xfer->xfer_buf;
  1134. req->dev_id = dev_id;
  1135. req->clk_id = clk_id;
  1136. ret = ti_sci_do_xfer(info, xfer);
  1137. if (ret) {
  1138. dev_err(dev, "Mbox send fail %d\n", ret);
  1139. goto fail;
  1140. }
  1141. resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->xfer_buf;
  1142. if (!ti_sci_is_response_ack(resp))
  1143. ret = -ENODEV;
  1144. else
  1145. *parent_id = resp->parent_id;
  1146. fail:
  1147. ti_sci_put_one_xfer(&info->minfo, xfer);
  1148. return ret;
  1149. }
  1150. /**
  1151. * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
  1152. * @handle: pointer to TI SCI handle
  1153. * @dev_id: Device identifier this request is for
  1154. * @clk_id: Clock identifier for the device for this request.
  1155. * Each device has it's own set of clock inputs. This indexes
  1156. * which clock input to modify.
  1157. * @num_parents: Returns he number of parents to the current clock.
  1158. *
  1159. * Return: 0 if all went well, else returns appropriate error value.
  1160. */
  1161. static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
  1162. u32 dev_id, u8 clk_id,
  1163. u8 *num_parents)
  1164. {
  1165. struct ti_sci_info *info;
  1166. struct ti_sci_msg_req_get_clock_num_parents *req;
  1167. struct ti_sci_msg_resp_get_clock_num_parents *resp;
  1168. struct ti_sci_xfer *xfer;
  1169. struct device *dev;
  1170. int ret = 0;
  1171. if (IS_ERR(handle))
  1172. return PTR_ERR(handle);
  1173. if (!handle || !num_parents)
  1174. return -EINVAL;
  1175. info = handle_to_ti_sci_info(handle);
  1176. dev = info->dev;
  1177. xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
  1178. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1179. sizeof(*req), sizeof(*resp));
  1180. if (IS_ERR(xfer)) {
  1181. ret = PTR_ERR(xfer);
  1182. dev_err(dev, "Message alloc failed(%d)\n", ret);
  1183. return ret;
  1184. }
  1185. req = (struct ti_sci_msg_req_get_clock_num_parents *)xfer->xfer_buf;
  1186. req->dev_id = dev_id;
  1187. req->clk_id = clk_id;
  1188. ret = ti_sci_do_xfer(info, xfer);
  1189. if (ret) {
  1190. dev_err(dev, "Mbox send fail %d\n", ret);
  1191. goto fail;
  1192. }
  1193. resp = (struct ti_sci_msg_resp_get_clock_num_parents *)xfer->xfer_buf;
  1194. if (!ti_sci_is_response_ack(resp))
  1195. ret = -ENODEV;
  1196. else
  1197. *num_parents = resp->num_parents;
  1198. fail:
  1199. ti_sci_put_one_xfer(&info->minfo, xfer);
  1200. return ret;
  1201. }
  1202. /**
  1203. * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
  1204. * @handle: pointer to TI SCI handle
  1205. * @dev_id: Device identifier this request is for
  1206. * @clk_id: Clock identifier for the device for this request.
  1207. * Each device has it's own set of clock inputs. This indexes
  1208. * which clock input to modify.
  1209. * @min_freq: The minimum allowable frequency in Hz. This is the minimum
  1210. * allowable programmed frequency and does not account for clock
  1211. * tolerances and jitter.
  1212. * @target_freq: The target clock frequency in Hz. A frequency will be
  1213. * processed as close to this target frequency as possible.
  1214. * @max_freq: The maximum allowable frequency in Hz. This is the maximum
  1215. * allowable programmed frequency and does not account for clock
  1216. * tolerances and jitter.
  1217. * @match_freq: Frequency match in Hz response.
  1218. *
  1219. * Return: 0 if all went well, else returns appropriate error value.
  1220. */
  1221. static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
  1222. u32 dev_id, u8 clk_id, u64 min_freq,
  1223. u64 target_freq, u64 max_freq,
  1224. u64 *match_freq)
  1225. {
  1226. struct ti_sci_info *info;
  1227. struct ti_sci_msg_req_query_clock_freq *req;
  1228. struct ti_sci_msg_resp_query_clock_freq *resp;
  1229. struct ti_sci_xfer *xfer;
  1230. struct device *dev;
  1231. int ret = 0;
  1232. if (IS_ERR(handle))
  1233. return PTR_ERR(handle);
  1234. if (!handle || !match_freq)
  1235. return -EINVAL;
  1236. info = handle_to_ti_sci_info(handle);
  1237. dev = info->dev;
  1238. xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
  1239. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1240. sizeof(*req), sizeof(*resp));
  1241. if (IS_ERR(xfer)) {
  1242. ret = PTR_ERR(xfer);
  1243. dev_err(dev, "Message alloc failed(%d)\n", ret);
  1244. return ret;
  1245. }
  1246. req = (struct ti_sci_msg_req_query_clock_freq *)xfer->xfer_buf;
  1247. req->dev_id = dev_id;
  1248. req->clk_id = clk_id;
  1249. req->min_freq_hz = min_freq;
  1250. req->target_freq_hz = target_freq;
  1251. req->max_freq_hz = max_freq;
  1252. ret = ti_sci_do_xfer(info, xfer);
  1253. if (ret) {
  1254. dev_err(dev, "Mbox send fail %d\n", ret);
  1255. goto fail;
  1256. }
  1257. resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->xfer_buf;
  1258. if (!ti_sci_is_response_ack(resp))
  1259. ret = -ENODEV;
  1260. else
  1261. *match_freq = resp->freq_hz;
  1262. fail:
  1263. ti_sci_put_one_xfer(&info->minfo, xfer);
  1264. return ret;
  1265. }
  1266. /**
  1267. * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
  1268. * @handle: pointer to TI SCI handle
  1269. * @dev_id: Device identifier this request is for
  1270. * @clk_id: Clock identifier for the device for this request.
  1271. * Each device has it's own set of clock inputs. This indexes
  1272. * which clock input to modify.
  1273. * @min_freq: The minimum allowable frequency in Hz. This is the minimum
  1274. * allowable programmed frequency and does not account for clock
  1275. * tolerances and jitter.
  1276. * @target_freq: The target clock frequency in Hz. A frequency will be
  1277. * processed as close to this target frequency as possible.
  1278. * @max_freq: The maximum allowable frequency in Hz. This is the maximum
  1279. * allowable programmed frequency and does not account for clock
  1280. * tolerances and jitter.
  1281. *
  1282. * Return: 0 if all went well, else returns appropriate error value.
  1283. */
  1284. static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
  1285. u32 dev_id, u8 clk_id, u64 min_freq,
  1286. u64 target_freq, u64 max_freq)
  1287. {
  1288. struct ti_sci_info *info;
  1289. struct ti_sci_msg_req_set_clock_freq *req;
  1290. struct ti_sci_msg_hdr *resp;
  1291. struct ti_sci_xfer *xfer;
  1292. struct device *dev;
  1293. int ret = 0;
  1294. if (IS_ERR(handle))
  1295. return PTR_ERR(handle);
  1296. if (!handle)
  1297. return -EINVAL;
  1298. info = handle_to_ti_sci_info(handle);
  1299. dev = info->dev;
  1300. xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
  1301. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1302. sizeof(*req), sizeof(*resp));
  1303. if (IS_ERR(xfer)) {
  1304. ret = PTR_ERR(xfer);
  1305. dev_err(dev, "Message alloc failed(%d)\n", ret);
  1306. return ret;
  1307. }
  1308. req = (struct ti_sci_msg_req_set_clock_freq *)xfer->xfer_buf;
  1309. req->dev_id = dev_id;
  1310. req->clk_id = clk_id;
  1311. req->min_freq_hz = min_freq;
  1312. req->target_freq_hz = target_freq;
  1313. req->max_freq_hz = max_freq;
  1314. ret = ti_sci_do_xfer(info, xfer);
  1315. if (ret) {
  1316. dev_err(dev, "Mbox send fail %d\n", ret);
  1317. goto fail;
  1318. }
  1319. resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  1320. ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
  1321. fail:
  1322. ti_sci_put_one_xfer(&info->minfo, xfer);
  1323. return ret;
  1324. }
  1325. /**
  1326. * ti_sci_cmd_clk_get_freq() - Get current frequency
  1327. * @handle: pointer to TI SCI handle
  1328. * @dev_id: Device identifier this request is for
  1329. * @clk_id: Clock identifier for the device for this request.
  1330. * Each device has it's own set of clock inputs. This indexes
  1331. * which clock input to modify.
  1332. * @freq: Currently frequency in Hz
  1333. *
  1334. * Return: 0 if all went well, else returns appropriate error value.
  1335. */
  1336. static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
  1337. u32 dev_id, u8 clk_id, u64 *freq)
  1338. {
  1339. struct ti_sci_info *info;
  1340. struct ti_sci_msg_req_get_clock_freq *req;
  1341. struct ti_sci_msg_resp_get_clock_freq *resp;
  1342. struct ti_sci_xfer *xfer;
  1343. struct device *dev;
  1344. int ret = 0;
  1345. if (IS_ERR(handle))
  1346. return PTR_ERR(handle);
  1347. if (!handle || !freq)
  1348. return -EINVAL;
  1349. info = handle_to_ti_sci_info(handle);
  1350. dev = info->dev;
  1351. xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
  1352. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1353. sizeof(*req), sizeof(*resp));
  1354. if (IS_ERR(xfer)) {
  1355. ret = PTR_ERR(xfer);
  1356. dev_err(dev, "Message alloc failed(%d)\n", ret);
  1357. return ret;
  1358. }
  1359. req = (struct ti_sci_msg_req_get_clock_freq *)xfer->xfer_buf;
  1360. req->dev_id = dev_id;
  1361. req->clk_id = clk_id;
  1362. ret = ti_sci_do_xfer(info, xfer);
  1363. if (ret) {
  1364. dev_err(dev, "Mbox send fail %d\n", ret);
  1365. goto fail;
  1366. }
  1367. resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->xfer_buf;
  1368. if (!ti_sci_is_response_ack(resp))
  1369. ret = -ENODEV;
  1370. else
  1371. *freq = resp->freq_hz;
  1372. fail:
  1373. ti_sci_put_one_xfer(&info->minfo, xfer);
  1374. return ret;
  1375. }
  1376. static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
  1377. {
  1378. struct ti_sci_info *info;
  1379. struct ti_sci_msg_req_reboot *req;
  1380. struct ti_sci_msg_hdr *resp;
  1381. struct ti_sci_xfer *xfer;
  1382. struct device *dev;
  1383. int ret = 0;
  1384. if (IS_ERR(handle))
  1385. return PTR_ERR(handle);
  1386. if (!handle)
  1387. return -EINVAL;
  1388. info = handle_to_ti_sci_info(handle);
  1389. dev = info->dev;
  1390. xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SYS_RESET,
  1391. TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
  1392. sizeof(*req), sizeof(*resp));
  1393. if (IS_ERR(xfer)) {
  1394. ret = PTR_ERR(xfer);
  1395. dev_err(dev, "Message alloc failed(%d)\n", ret);
  1396. return ret;
  1397. }
  1398. req = (struct ti_sci_msg_req_reboot *)xfer->xfer_buf;
  1399. ret = ti_sci_do_xfer(info, xfer);
  1400. if (ret) {
  1401. dev_err(dev, "Mbox send fail %d\n", ret);
  1402. goto fail;
  1403. }
  1404. resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
  1405. if (!ti_sci_is_response_ack(resp))
  1406. ret = -ENODEV;
  1407. else
  1408. ret = 0;
  1409. fail:
  1410. ti_sci_put_one_xfer(&info->minfo, xfer);
  1411. return ret;
  1412. }
  1413. /*
  1414. * ti_sci_setup_ops() - Setup the operations structures
  1415. * @info: pointer to TISCI pointer
  1416. */
  1417. static void ti_sci_setup_ops(struct ti_sci_info *info)
  1418. {
  1419. struct ti_sci_ops *ops = &info->handle.ops;
  1420. struct ti_sci_core_ops *core_ops = &ops->core_ops;
  1421. struct ti_sci_dev_ops *dops = &ops->dev_ops;
  1422. struct ti_sci_clk_ops *cops = &ops->clk_ops;
  1423. core_ops->reboot_device = ti_sci_cmd_core_reboot;
  1424. dops->get_device = ti_sci_cmd_get_device;
  1425. dops->idle_device = ti_sci_cmd_idle_device;
  1426. dops->put_device = ti_sci_cmd_put_device;
  1427. dops->is_valid = ti_sci_cmd_dev_is_valid;
  1428. dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
  1429. dops->is_idle = ti_sci_cmd_dev_is_idle;
  1430. dops->is_stop = ti_sci_cmd_dev_is_stop;
  1431. dops->is_on = ti_sci_cmd_dev_is_on;
  1432. dops->is_transitioning = ti_sci_cmd_dev_is_trans;
  1433. dops->set_device_resets = ti_sci_cmd_set_device_resets;
  1434. dops->get_device_resets = ti_sci_cmd_get_device_resets;
  1435. cops->get_clock = ti_sci_cmd_get_clock;
  1436. cops->idle_clock = ti_sci_cmd_idle_clock;
  1437. cops->put_clock = ti_sci_cmd_put_clock;
  1438. cops->is_auto = ti_sci_cmd_clk_is_auto;
  1439. cops->is_on = ti_sci_cmd_clk_is_on;
  1440. cops->is_off = ti_sci_cmd_clk_is_off;
  1441. cops->set_parent = ti_sci_cmd_clk_set_parent;
  1442. cops->get_parent = ti_sci_cmd_clk_get_parent;
  1443. cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
  1444. cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
  1445. cops->set_freq = ti_sci_cmd_clk_set_freq;
  1446. cops->get_freq = ti_sci_cmd_clk_get_freq;
  1447. }
  1448. /**
  1449. * ti_sci_get_handle() - Get the TI SCI handle for a device
  1450. * @dev: Pointer to device for which we want SCI handle
  1451. *
  1452. * NOTE: The function does not track individual clients of the framework
  1453. * and is expected to be maintained by caller of TI SCI protocol library.
  1454. * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
  1455. * Return: pointer to handle if successful, else:
  1456. * -EPROBE_DEFER if the instance is not ready
  1457. * -ENODEV if the required node handler is missing
  1458. * -EINVAL if invalid conditions are encountered.
  1459. */
  1460. const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
  1461. {
  1462. struct device_node *ti_sci_np;
  1463. struct list_head *p;
  1464. struct ti_sci_handle *handle = NULL;
  1465. struct ti_sci_info *info;
  1466. if (!dev) {
  1467. pr_err("I need a device pointer\n");
  1468. return ERR_PTR(-EINVAL);
  1469. }
  1470. ti_sci_np = of_get_parent(dev->of_node);
  1471. if (!ti_sci_np) {
  1472. dev_err(dev, "No OF information\n");
  1473. return ERR_PTR(-EINVAL);
  1474. }
  1475. mutex_lock(&ti_sci_list_mutex);
  1476. list_for_each(p, &ti_sci_list) {
  1477. info = list_entry(p, struct ti_sci_info, node);
  1478. if (ti_sci_np == info->dev->of_node) {
  1479. handle = &info->handle;
  1480. info->users++;
  1481. break;
  1482. }
  1483. }
  1484. mutex_unlock(&ti_sci_list_mutex);
  1485. of_node_put(ti_sci_np);
  1486. if (!handle)
  1487. return ERR_PTR(-EPROBE_DEFER);
  1488. return handle;
  1489. }
  1490. EXPORT_SYMBOL_GPL(ti_sci_get_handle);
  1491. /**
  1492. * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
  1493. * @handle: Handle acquired by ti_sci_get_handle
  1494. *
  1495. * NOTE: The function does not track individual clients of the framework
  1496. * and is expected to be maintained by caller of TI SCI protocol library.
  1497. * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
  1498. *
  1499. * Return: 0 is successfully released
  1500. * if an error pointer was passed, it returns the error value back,
  1501. * if null was passed, it returns -EINVAL;
  1502. */
  1503. int ti_sci_put_handle(const struct ti_sci_handle *handle)
  1504. {
  1505. struct ti_sci_info *info;
  1506. if (IS_ERR(handle))
  1507. return PTR_ERR(handle);
  1508. if (!handle)
  1509. return -EINVAL;
  1510. info = handle_to_ti_sci_info(handle);
  1511. mutex_lock(&ti_sci_list_mutex);
  1512. if (!WARN_ON(!info->users))
  1513. info->users--;
  1514. mutex_unlock(&ti_sci_list_mutex);
  1515. return 0;
  1516. }
  1517. EXPORT_SYMBOL_GPL(ti_sci_put_handle);
  1518. static void devm_ti_sci_release(struct device *dev, void *res)
  1519. {
  1520. const struct ti_sci_handle **ptr = res;
  1521. const struct ti_sci_handle *handle = *ptr;
  1522. int ret;
  1523. ret = ti_sci_put_handle(handle);
  1524. if (ret)
  1525. dev_err(dev, "failed to put handle %d\n", ret);
  1526. }
  1527. /**
  1528. * devm_ti_sci_get_handle() - Managed get handle
  1529. * @dev: device for which we want SCI handle for.
  1530. *
  1531. * NOTE: This releases the handle once the device resources are
  1532. * no longer needed. MUST NOT BE released with ti_sci_put_handle.
  1533. * The function does not track individual clients of the framework
  1534. * and is expected to be maintained by caller of TI SCI protocol library.
  1535. *
  1536. * Return: 0 if all went fine, else corresponding error.
  1537. */
  1538. const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
  1539. {
  1540. const struct ti_sci_handle **ptr;
  1541. const struct ti_sci_handle *handle;
  1542. ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
  1543. if (!ptr)
  1544. return ERR_PTR(-ENOMEM);
  1545. handle = ti_sci_get_handle(dev);
  1546. if (!IS_ERR(handle)) {
  1547. *ptr = handle;
  1548. devres_add(dev, ptr);
  1549. } else {
  1550. devres_free(ptr);
  1551. }
  1552. return handle;
  1553. }
  1554. EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
  1555. static int tisci_reboot_handler(struct notifier_block *nb, unsigned long mode,
  1556. void *cmd)
  1557. {
  1558. struct ti_sci_info *info = reboot_to_ti_sci_info(nb);
  1559. const struct ti_sci_handle *handle = &info->handle;
  1560. ti_sci_cmd_core_reboot(handle);
  1561. /* call fail OR pass, we should not be here in the first place */
  1562. return NOTIFY_BAD;
  1563. }
  1564. /* Description for K2G */
  1565. static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
  1566. .host_id = 2,
  1567. /* Conservative duration */
  1568. .max_rx_timeout_ms = 1000,
  1569. /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
  1570. .max_msgs = 20,
  1571. .max_msg_size = 64,
  1572. };
  1573. static const struct of_device_id ti_sci_of_match[] = {
  1574. {.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
  1575. { /* Sentinel */ },
  1576. };
  1577. MODULE_DEVICE_TABLE(of, ti_sci_of_match);
  1578. static int ti_sci_probe(struct platform_device *pdev)
  1579. {
  1580. struct device *dev = &pdev->dev;
  1581. const struct of_device_id *of_id;
  1582. const struct ti_sci_desc *desc;
  1583. struct ti_sci_xfer *xfer;
  1584. struct ti_sci_info *info = NULL;
  1585. struct ti_sci_xfers_info *minfo;
  1586. struct mbox_client *cl;
  1587. int ret = -EINVAL;
  1588. int i;
  1589. int reboot = 0;
  1590. of_id = of_match_device(ti_sci_of_match, dev);
  1591. if (!of_id) {
  1592. dev_err(dev, "OF data missing\n");
  1593. return -EINVAL;
  1594. }
  1595. desc = of_id->data;
  1596. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  1597. if (!info)
  1598. return -ENOMEM;
  1599. info->dev = dev;
  1600. info->desc = desc;
  1601. reboot = of_property_read_bool(dev->of_node,
  1602. "ti,system-reboot-controller");
  1603. INIT_LIST_HEAD(&info->node);
  1604. minfo = &info->minfo;
  1605. /*
  1606. * Pre-allocate messages
  1607. * NEVER allocate more than what we can indicate in hdr.seq
  1608. * if we have data description bug, force a fix..
  1609. */
  1610. if (WARN_ON(desc->max_msgs >=
  1611. 1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
  1612. return -EINVAL;
  1613. minfo->xfer_block = devm_kcalloc(dev,
  1614. desc->max_msgs,
  1615. sizeof(*minfo->xfer_block),
  1616. GFP_KERNEL);
  1617. if (!minfo->xfer_block)
  1618. return -ENOMEM;
  1619. minfo->xfer_alloc_table = devm_kzalloc(dev,
  1620. BITS_TO_LONGS(desc->max_msgs)
  1621. * sizeof(unsigned long),
  1622. GFP_KERNEL);
  1623. if (!minfo->xfer_alloc_table)
  1624. return -ENOMEM;
  1625. bitmap_zero(minfo->xfer_alloc_table, desc->max_msgs);
  1626. /* Pre-initialize the buffer pointer to pre-allocated buffers */
  1627. for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
  1628. xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
  1629. GFP_KERNEL);
  1630. if (!xfer->xfer_buf)
  1631. return -ENOMEM;
  1632. xfer->tx_message.buf = xfer->xfer_buf;
  1633. init_completion(&xfer->done);
  1634. }
  1635. ret = ti_sci_debugfs_create(pdev, info);
  1636. if (ret)
  1637. dev_warn(dev, "Failed to create debug file\n");
  1638. platform_set_drvdata(pdev, info);
  1639. cl = &info->cl;
  1640. cl->dev = dev;
  1641. cl->tx_block = false;
  1642. cl->rx_callback = ti_sci_rx_callback;
  1643. cl->knows_txdone = true;
  1644. spin_lock_init(&minfo->xfer_lock);
  1645. sema_init(&minfo->sem_xfer_count, desc->max_msgs);
  1646. info->chan_rx = mbox_request_channel_byname(cl, "rx");
  1647. if (IS_ERR(info->chan_rx)) {
  1648. ret = PTR_ERR(info->chan_rx);
  1649. goto out;
  1650. }
  1651. info->chan_tx = mbox_request_channel_byname(cl, "tx");
  1652. if (IS_ERR(info->chan_tx)) {
  1653. ret = PTR_ERR(info->chan_tx);
  1654. goto out;
  1655. }
  1656. ret = ti_sci_cmd_get_revision(info);
  1657. if (ret) {
  1658. dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
  1659. goto out;
  1660. }
  1661. ti_sci_setup_ops(info);
  1662. if (reboot) {
  1663. info->nb.notifier_call = tisci_reboot_handler;
  1664. info->nb.priority = 128;
  1665. ret = register_restart_handler(&info->nb);
  1666. if (ret) {
  1667. dev_err(dev, "reboot registration fail(%d)\n", ret);
  1668. return ret;
  1669. }
  1670. }
  1671. dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
  1672. info->handle.version.abi_major, info->handle.version.abi_minor,
  1673. info->handle.version.firmware_revision,
  1674. info->handle.version.firmware_description);
  1675. mutex_lock(&ti_sci_list_mutex);
  1676. list_add_tail(&info->node, &ti_sci_list);
  1677. mutex_unlock(&ti_sci_list_mutex);
  1678. return of_platform_populate(dev->of_node, NULL, NULL, dev);
  1679. out:
  1680. if (!IS_ERR(info->chan_tx))
  1681. mbox_free_channel(info->chan_tx);
  1682. if (!IS_ERR(info->chan_rx))
  1683. mbox_free_channel(info->chan_rx);
  1684. debugfs_remove(info->d);
  1685. return ret;
  1686. }
  1687. static int ti_sci_remove(struct platform_device *pdev)
  1688. {
  1689. struct ti_sci_info *info;
  1690. struct device *dev = &pdev->dev;
  1691. int ret = 0;
  1692. of_platform_depopulate(dev);
  1693. info = platform_get_drvdata(pdev);
  1694. if (info->nb.notifier_call)
  1695. unregister_restart_handler(&info->nb);
  1696. mutex_lock(&ti_sci_list_mutex);
  1697. if (info->users)
  1698. ret = -EBUSY;
  1699. else
  1700. list_del(&info->node);
  1701. mutex_unlock(&ti_sci_list_mutex);
  1702. if (!ret) {
  1703. ti_sci_debugfs_destroy(pdev, info);
  1704. /* Safe to free channels since no more users */
  1705. mbox_free_channel(info->chan_tx);
  1706. mbox_free_channel(info->chan_rx);
  1707. }
  1708. return ret;
  1709. }
  1710. static struct platform_driver ti_sci_driver = {
  1711. .probe = ti_sci_probe,
  1712. .remove = ti_sci_remove,
  1713. .driver = {
  1714. .name = "ti-sci",
  1715. .of_match_table = of_match_ptr(ti_sci_of_match),
  1716. },
  1717. };
  1718. module_platform_driver(ti_sci_driver);
  1719. MODULE_LICENSE("GPL v2");
  1720. MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
  1721. MODULE_AUTHOR("Nishanth Menon");
  1722. MODULE_ALIAS("platform:ti-sci");