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