12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985 |
- // SPDX-License-Identifier: GPL-2.0
- /*
- * Texas Instruments System Control Interface Protocol Driver
- *
- * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
- * Nishanth Menon
- */
- #define pr_fmt(fmt) "%s: " fmt, __func__
- #include <linux/bitmap.h>
- #include <linux/debugfs.h>
- #include <linux/export.h>
- #include <linux/io.h>
- #include <linux/kernel.h>
- #include <linux/mailbox_client.h>
- #include <linux/module.h>
- #include <linux/of_device.h>
- #include <linux/semaphore.h>
- #include <linux/slab.h>
- #include <linux/soc/ti/ti-msgmgr.h>
- #include <linux/soc/ti/ti_sci_protocol.h>
- #include <linux/reboot.h>
- #include "ti_sci.h"
- /* List of all TI SCI devices active in system */
- static LIST_HEAD(ti_sci_list);
- /* Protection for the entire list */
- static DEFINE_MUTEX(ti_sci_list_mutex);
- /**
- * struct ti_sci_xfer - Structure representing a message flow
- * @tx_message: Transmit message
- * @rx_len: Receive message length
- * @xfer_buf: Preallocated buffer to store receive message
- * Since we work with request-ACK protocol, we can
- * reuse the same buffer for the rx path as we
- * use for the tx path.
- * @done: completion event
- */
- struct ti_sci_xfer {
- struct ti_msgmgr_message tx_message;
- u8 rx_len;
- u8 *xfer_buf;
- struct completion done;
- };
- /**
- * struct ti_sci_xfers_info - Structure to manage transfer information
- * @sem_xfer_count: Counting Semaphore for managing max simultaneous
- * Messages.
- * @xfer_block: Preallocated Message array
- * @xfer_alloc_table: Bitmap table for allocated messages.
- * Index of this bitmap table is also used for message
- * sequence identifier.
- * @xfer_lock: Protection for message allocation
- */
- struct ti_sci_xfers_info {
- struct semaphore sem_xfer_count;
- struct ti_sci_xfer *xfer_block;
- unsigned long *xfer_alloc_table;
- /* protect transfer allocation */
- spinlock_t xfer_lock;
- };
- /**
- * struct ti_sci_desc - Description of SoC integration
- * @host_id: Host identifier representing the compute entity
- * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
- * @max_msgs: Maximum number of messages that can be pending
- * simultaneously in the system
- * @max_msg_size: Maximum size of data per message that can be handled.
- */
- struct ti_sci_desc {
- u8 host_id;
- int max_rx_timeout_ms;
- int max_msgs;
- int max_msg_size;
- };
- /**
- * struct ti_sci_info - Structure representing a TI SCI instance
- * @dev: Device pointer
- * @desc: SoC description for this instance
- * @nb: Reboot Notifier block
- * @d: Debugfs file entry
- * @debug_region: Memory region where the debug message are available
- * @debug_region_size: Debug region size
- * @debug_buffer: Buffer allocated to copy debug messages.
- * @handle: Instance of TI SCI handle to send to clients.
- * @cl: Mailbox Client
- * @chan_tx: Transmit mailbox channel
- * @chan_rx: Receive mailbox channel
- * @minfo: Message info
- * @node: list head
- * @users: Number of users of this instance
- */
- struct ti_sci_info {
- struct device *dev;
- struct notifier_block nb;
- const struct ti_sci_desc *desc;
- struct dentry *d;
- void __iomem *debug_region;
- char *debug_buffer;
- size_t debug_region_size;
- struct ti_sci_handle handle;
- struct mbox_client cl;
- struct mbox_chan *chan_tx;
- struct mbox_chan *chan_rx;
- struct ti_sci_xfers_info minfo;
- struct list_head node;
- /* protected by ti_sci_list_mutex */
- int users;
- };
- #define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl)
- #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
- #define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb)
- #ifdef CONFIG_DEBUG_FS
- /**
- * ti_sci_debug_show() - Helper to dump the debug log
- * @s: sequence file pointer
- * @unused: unused.
- *
- * Return: 0
- */
- static int ti_sci_debug_show(struct seq_file *s, void *unused)
- {
- struct ti_sci_info *info = s->private;
- memcpy_fromio(info->debug_buffer, info->debug_region,
- info->debug_region_size);
- /*
- * We don't trust firmware to leave NULL terminated last byte (hence
- * we have allocated 1 extra 0 byte). Since we cannot guarantee any
- * specific data format for debug messages, We just present the data
- * in the buffer as is - we expect the messages to be self explanatory.
- */
- seq_puts(s, info->debug_buffer);
- return 0;
- }
- /**
- * ti_sci_debug_open() - debug file open
- * @inode: inode pointer
- * @file: file pointer
- *
- * Return: result of single_open
- */
- static int ti_sci_debug_open(struct inode *inode, struct file *file)
- {
- return single_open(file, ti_sci_debug_show, inode->i_private);
- }
- /* log file operations */
- static const struct file_operations ti_sci_debug_fops = {
- .open = ti_sci_debug_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- /**
- * ti_sci_debugfs_create() - Create log debug file
- * @pdev: platform device pointer
- * @info: Pointer to SCI entity information
- *
- * Return: 0 if all went fine, else corresponding error.
- */
- static int ti_sci_debugfs_create(struct platform_device *pdev,
- struct ti_sci_info *info)
- {
- struct device *dev = &pdev->dev;
- struct resource *res;
- char debug_name[50] = "ti_sci_debug@";
- /* Debug region is optional */
- res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
- "debug_messages");
- info->debug_region = devm_ioremap_resource(dev, res);
- if (IS_ERR(info->debug_region))
- return 0;
- info->debug_region_size = resource_size(res);
- info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
- sizeof(char), GFP_KERNEL);
- if (!info->debug_buffer)
- return -ENOMEM;
- /* Setup NULL termination */
- info->debug_buffer[info->debug_region_size] = 0;
- info->d = debugfs_create_file(strncat(debug_name, dev_name(dev),
- sizeof(debug_name) -
- sizeof("ti_sci_debug@")),
- 0444, NULL, info, &ti_sci_debug_fops);
- if (IS_ERR(info->d))
- return PTR_ERR(info->d);
- dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n",
- info->debug_region, info->debug_region_size, res);
- return 0;
- }
- /**
- * ti_sci_debugfs_destroy() - clean up log debug file
- * @pdev: platform device pointer
- * @info: Pointer to SCI entity information
- */
- static void ti_sci_debugfs_destroy(struct platform_device *pdev,
- struct ti_sci_info *info)
- {
- if (IS_ERR(info->debug_region))
- return;
- debugfs_remove(info->d);
- }
- #else /* CONFIG_DEBUG_FS */
- static inline int ti_sci_debugfs_create(struct platform_device *dev,
- struct ti_sci_info *info)
- {
- return 0;
- }
- static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
- struct ti_sci_info *info)
- {
- }
- #endif /* CONFIG_DEBUG_FS */
- /**
- * ti_sci_dump_header_dbg() - Helper to dump a message header.
- * @dev: Device pointer corresponding to the SCI entity
- * @hdr: pointer to header.
- */
- static inline void ti_sci_dump_header_dbg(struct device *dev,
- struct ti_sci_msg_hdr *hdr)
- {
- dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
- hdr->type, hdr->host, hdr->seq, hdr->flags);
- }
- /**
- * ti_sci_rx_callback() - mailbox client callback for receive messages
- * @cl: client pointer
- * @m: mailbox message
- *
- * Processes one received message to appropriate transfer information and
- * signals completion of the transfer.
- *
- * NOTE: This function will be invoked in IRQ context, hence should be
- * as optimal as possible.
- */
- static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
- {
- struct ti_sci_info *info = cl_to_ti_sci_info(cl);
- struct device *dev = info->dev;
- struct ti_sci_xfers_info *minfo = &info->minfo;
- struct ti_msgmgr_message *mbox_msg = m;
- struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
- struct ti_sci_xfer *xfer;
- u8 xfer_id;
- xfer_id = hdr->seq;
- /*
- * Are we even expecting this?
- * NOTE: barriers were implicit in locks used for modifying the bitmap
- */
- if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
- dev_err(dev, "Message for %d is not expected!\n", xfer_id);
- return;
- }
- xfer = &minfo->xfer_block[xfer_id];
- /* Is the message of valid length? */
- if (mbox_msg->len > info->desc->max_msg_size) {
- dev_err(dev, "Unable to handle %zu xfer(max %d)\n",
- mbox_msg->len, info->desc->max_msg_size);
- ti_sci_dump_header_dbg(dev, hdr);
- return;
- }
- if (mbox_msg->len < xfer->rx_len) {
- dev_err(dev, "Recv xfer %zu < expected %d length\n",
- mbox_msg->len, xfer->rx_len);
- ti_sci_dump_header_dbg(dev, hdr);
- return;
- }
- ti_sci_dump_header_dbg(dev, hdr);
- /* Take a copy to the rx buffer.. */
- memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
- complete(&xfer->done);
- }
- /**
- * ti_sci_get_one_xfer() - Allocate one message
- * @info: Pointer to SCI entity information
- * @msg_type: Message type
- * @msg_flags: Flag to set for the message
- * @tx_message_size: transmit message size
- * @rx_message_size: receive message size
- *
- * Helper function which is used by various command functions that are
- * exposed to clients of this driver for allocating a message traffic event.
- *
- * This function can sleep depending on pending requests already in the system
- * for the SCI entity. Further, this also holds a spinlock to maintain integrity
- * of internal data structures.
- *
- * Return: 0 if all went fine, else corresponding error.
- */
- static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info,
- u16 msg_type, u32 msg_flags,
- size_t tx_message_size,
- size_t rx_message_size)
- {
- struct ti_sci_xfers_info *minfo = &info->minfo;
- struct ti_sci_xfer *xfer;
- struct ti_sci_msg_hdr *hdr;
- unsigned long flags;
- unsigned long bit_pos;
- u8 xfer_id;
- int ret;
- int timeout;
- /* Ensure we have sane transfer sizes */
- if (rx_message_size > info->desc->max_msg_size ||
- tx_message_size > info->desc->max_msg_size ||
- rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
- return ERR_PTR(-ERANGE);
- /*
- * Ensure we have only controlled number of pending messages.
- * Ideally, we might just have to wait a single message, be
- * conservative and wait 5 times that..
- */
- timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
- ret = down_timeout(&minfo->sem_xfer_count, timeout);
- if (ret < 0)
- return ERR_PTR(ret);
- /* Keep the locked section as small as possible */
- spin_lock_irqsave(&minfo->xfer_lock, flags);
- bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
- info->desc->max_msgs);
- set_bit(bit_pos, minfo->xfer_alloc_table);
- spin_unlock_irqrestore(&minfo->xfer_lock, flags);
- /*
- * We already ensured in probe that we can have max messages that can
- * fit in hdr.seq - NOTE: this improves access latencies
- * to predictable O(1) access, BUT, it opens us to risk if
- * remote misbehaves with corrupted message sequence responses.
- * If that happens, we are going to be messed up anyways..
- */
- xfer_id = (u8)bit_pos;
- xfer = &minfo->xfer_block[xfer_id];
- hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
- xfer->tx_message.len = tx_message_size;
- xfer->rx_len = (u8)rx_message_size;
- reinit_completion(&xfer->done);
- hdr->seq = xfer_id;
- hdr->type = msg_type;
- hdr->host = info->desc->host_id;
- hdr->flags = msg_flags;
- return xfer;
- }
- /**
- * ti_sci_put_one_xfer() - Release a message
- * @minfo: transfer info pointer
- * @xfer: message that was reserved by ti_sci_get_one_xfer
- *
- * This holds a spinlock to maintain integrity of internal data structures.
- */
- static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
- struct ti_sci_xfer *xfer)
- {
- unsigned long flags;
- struct ti_sci_msg_hdr *hdr;
- u8 xfer_id;
- hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
- xfer_id = hdr->seq;
- /*
- * Keep the locked section as small as possible
- * NOTE: we might escape with smp_mb and no lock here..
- * but just be conservative and symmetric.
- */
- spin_lock_irqsave(&minfo->xfer_lock, flags);
- clear_bit(xfer_id, minfo->xfer_alloc_table);
- spin_unlock_irqrestore(&minfo->xfer_lock, flags);
- /* Increment the count for the next user to get through */
- up(&minfo->sem_xfer_count);
- }
- /**
- * ti_sci_do_xfer() - Do one transfer
- * @info: Pointer to SCI entity information
- * @xfer: Transfer to initiate and wait for response
- *
- * Return: -ETIMEDOUT in case of no response, if transmit error,
- * return corresponding error, else if all goes well,
- * return 0.
- */
- static inline int ti_sci_do_xfer(struct ti_sci_info *info,
- struct ti_sci_xfer *xfer)
- {
- int ret;
- int timeout;
- struct device *dev = info->dev;
- ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
- if (ret < 0)
- return ret;
- ret = 0;
- /* And we wait for the response. */
- timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
- if (!wait_for_completion_timeout(&xfer->done, timeout)) {
- dev_err(dev, "Mbox timedout in resp(caller: %pS)\n",
- (void *)_RET_IP_);
- ret = -ETIMEDOUT;
- }
- /*
- * NOTE: we might prefer not to need the mailbox ticker to manage the
- * transfer queueing since the protocol layer queues things by itself.
- * Unfortunately, we have to kick the mailbox framework after we have
- * received our message.
- */
- mbox_client_txdone(info->chan_tx, ret);
- return ret;
- }
- /**
- * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
- * @info: Pointer to SCI entity information
- *
- * Updates the SCI information in the internal data structure.
- *
- * Return: 0 if all went fine, else return appropriate error.
- */
- static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
- {
- struct device *dev = info->dev;
- struct ti_sci_handle *handle = &info->handle;
- struct ti_sci_version_info *ver = &handle->version;
- struct ti_sci_msg_resp_version *rev_info;
- struct ti_sci_xfer *xfer;
- int ret;
- xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
- TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
- sizeof(struct ti_sci_msg_hdr),
- sizeof(*rev_info));
- if (IS_ERR(xfer)) {
- ret = PTR_ERR(xfer);
- dev_err(dev, "Message alloc failed(%d)\n", ret);
- return ret;
- }
- rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
- ret = ti_sci_do_xfer(info, xfer);
- if (ret) {
- dev_err(dev, "Mbox send fail %d\n", ret);
- goto fail;
- }
- ver->abi_major = rev_info->abi_major;
- ver->abi_minor = rev_info->abi_minor;
- ver->firmware_revision = rev_info->firmware_revision;
- strncpy(ver->firmware_description, rev_info->firmware_description,
- sizeof(ver->firmware_description));
- fail:
- ti_sci_put_one_xfer(&info->minfo, xfer);
- return ret;
- }
- /**
- * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
- * @r: pointer to response buffer
- *
- * Return: true if the response was an ACK, else returns false.
- */
- static inline bool ti_sci_is_response_ack(void *r)
- {
- struct ti_sci_msg_hdr *hdr = r;
- return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
- }
- /**
- * ti_sci_set_device_state() - Set device state helper
- * @handle: pointer to TI SCI handle
- * @id: Device identifier
- * @flags: flags to setup for the device
- * @state: State to move the device to
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
- u32 id, u32 flags, u8 state)
- {
- struct ti_sci_info *info;
- struct ti_sci_msg_req_set_device_state *req;
- struct ti_sci_msg_hdr *resp;
- struct ti_sci_xfer *xfer;
- struct device *dev;
- int ret = 0;
- if (IS_ERR(handle))
- return PTR_ERR(handle);
- if (!handle)
- return -EINVAL;
- info = handle_to_ti_sci_info(handle);
- dev = info->dev;
- xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
- flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
- sizeof(*req), sizeof(*resp));
- if (IS_ERR(xfer)) {
- ret = PTR_ERR(xfer);
- dev_err(dev, "Message alloc failed(%d)\n", ret);
- return ret;
- }
- req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf;
- req->id = id;
- req->state = state;
- ret = ti_sci_do_xfer(info, xfer);
- if (ret) {
- dev_err(dev, "Mbox send fail %d\n", ret);
- goto fail;
- }
- resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
- ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
- fail:
- ti_sci_put_one_xfer(&info->minfo, xfer);
- return ret;
- }
- /**
- * ti_sci_get_device_state() - Get device state helper
- * @handle: Handle to the device
- * @id: Device Identifier
- * @clcnt: Pointer to Context Loss Count
- * @resets: pointer to resets
- * @p_state: pointer to p_state
- * @c_state: pointer to c_state
- *
- * Return: 0 if all went fine, else return appropriate error.
- */
- static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
- u32 id, u32 *clcnt, u32 *resets,
- u8 *p_state, u8 *c_state)
- {
- struct ti_sci_info *info;
- struct ti_sci_msg_req_get_device_state *req;
- struct ti_sci_msg_resp_get_device_state *resp;
- struct ti_sci_xfer *xfer;
- struct device *dev;
- int ret = 0;
- if (IS_ERR(handle))
- return PTR_ERR(handle);
- if (!handle)
- return -EINVAL;
- if (!clcnt && !resets && !p_state && !c_state)
- return -EINVAL;
- info = handle_to_ti_sci_info(handle);
- dev = info->dev;
- xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
- TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
- sizeof(*req), sizeof(*resp));
- if (IS_ERR(xfer)) {
- ret = PTR_ERR(xfer);
- dev_err(dev, "Message alloc failed(%d)\n", ret);
- return ret;
- }
- req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf;
- req->id = id;
- ret = ti_sci_do_xfer(info, xfer);
- if (ret) {
- dev_err(dev, "Mbox send fail %d\n", ret);
- goto fail;
- }
- resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf;
- if (!ti_sci_is_response_ack(resp)) {
- ret = -ENODEV;
- goto fail;
- }
- if (clcnt)
- *clcnt = resp->context_loss_count;
- if (resets)
- *resets = resp->resets;
- if (p_state)
- *p_state = resp->programmed_state;
- if (c_state)
- *c_state = resp->current_state;
- fail:
- ti_sci_put_one_xfer(&info->minfo, xfer);
- return ret;
- }
- /**
- * ti_sci_cmd_get_device() - command to request for device managed by TISCI
- * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
- * @id: Device Identifier
- *
- * Request for the device - NOTE: the client MUST maintain integrity of
- * usage count by balancing get_device with put_device. No refcounting is
- * managed by driver for that purpose.
- *
- * NOTE: The request is for exclusive access for the processor.
- *
- * Return: 0 if all went fine, else return appropriate error.
- */
- static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
- {
- return ti_sci_set_device_state(handle, id,
- MSG_FLAG_DEVICE_EXCLUSIVE,
- MSG_DEVICE_SW_STATE_ON);
- }
- /**
- * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
- * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
- * @id: Device Identifier
- *
- * Request for the device - NOTE: the client MUST maintain integrity of
- * usage count by balancing get_device with put_device. No refcounting is
- * managed by driver for that purpose.
- *
- * Return: 0 if all went fine, else return appropriate error.
- */
- static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
- {
- return ti_sci_set_device_state(handle, id,
- MSG_FLAG_DEVICE_EXCLUSIVE,
- MSG_DEVICE_SW_STATE_RETENTION);
- }
- /**
- * ti_sci_cmd_put_device() - command to release a device managed by TISCI
- * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
- * @id: Device Identifier
- *
- * Request for the device - NOTE: the client MUST maintain integrity of
- * usage count by balancing get_device with put_device. No refcounting is
- * managed by driver for that purpose.
- *
- * Return: 0 if all went fine, else return appropriate error.
- */
- static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
- {
- return ti_sci_set_device_state(handle, id,
- 0, MSG_DEVICE_SW_STATE_AUTO_OFF);
- }
- /**
- * ti_sci_cmd_dev_is_valid() - Is the device valid
- * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
- * @id: Device Identifier
- *
- * Return: 0 if all went fine and the device ID is valid, else return
- * appropriate error.
- */
- static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
- {
- u8 unused;
- /* check the device state which will also tell us if the ID is valid */
- return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
- }
- /**
- * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
- * @handle: Pointer to TISCI handle
- * @id: Device Identifier
- * @count: Pointer to Context Loss counter to populate
- *
- * Return: 0 if all went fine, else return appropriate error.
- */
- static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
- u32 *count)
- {
- return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
- }
- /**
- * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
- * @handle: Pointer to TISCI handle
- * @id: Device Identifier
- * @r_state: true if requested to be idle
- *
- * Return: 0 if all went fine, else return appropriate error.
- */
- static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
- bool *r_state)
- {
- int ret;
- u8 state;
- if (!r_state)
- return -EINVAL;
- ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
- if (ret)
- return ret;
- *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
- return 0;
- }
- /**
- * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
- * @handle: Pointer to TISCI handle
- * @id: Device Identifier
- * @r_state: true if requested to be stopped
- * @curr_state: true if currently stopped.
- *
- * Return: 0 if all went fine, else return appropriate error.
- */
- static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
- bool *r_state, bool *curr_state)
- {
- int ret;
- u8 p_state, c_state;
- if (!r_state && !curr_state)
- return -EINVAL;
- ret =
- ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
- if (ret)
- return ret;
- if (r_state)
- *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
- if (curr_state)
- *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
- return 0;
- }
- /**
- * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
- * @handle: Pointer to TISCI handle
- * @id: Device Identifier
- * @r_state: true if requested to be ON
- * @curr_state: true if currently ON and active
- *
- * Return: 0 if all went fine, else return appropriate error.
- */
- static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
- bool *r_state, bool *curr_state)
- {
- int ret;
- u8 p_state, c_state;
- if (!r_state && !curr_state)
- return -EINVAL;
- ret =
- ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
- if (ret)
- return ret;
- if (r_state)
- *r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
- if (curr_state)
- *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
- return 0;
- }
- /**
- * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
- * @handle: Pointer to TISCI handle
- * @id: Device Identifier
- * @curr_state: true if currently transitioning.
- *
- * Return: 0 if all went fine, else return appropriate error.
- */
- static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
- bool *curr_state)
- {
- int ret;
- u8 state;
- if (!curr_state)
- return -EINVAL;
- ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
- if (ret)
- return ret;
- *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
- return 0;
- }
- /**
- * ti_sci_cmd_set_device_resets() - command to set resets for device managed
- * by TISCI
- * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
- * @id: Device Identifier
- * @reset_state: Device specific reset bit field
- *
- * Return: 0 if all went fine, else return appropriate error.
- */
- static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
- u32 id, u32 reset_state)
- {
- struct ti_sci_info *info;
- struct ti_sci_msg_req_set_device_resets *req;
- struct ti_sci_msg_hdr *resp;
- struct ti_sci_xfer *xfer;
- struct device *dev;
- int ret = 0;
- if (IS_ERR(handle))
- return PTR_ERR(handle);
- if (!handle)
- return -EINVAL;
- info = handle_to_ti_sci_info(handle);
- dev = info->dev;
- xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
- TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
- sizeof(*req), sizeof(*resp));
- if (IS_ERR(xfer)) {
- ret = PTR_ERR(xfer);
- dev_err(dev, "Message alloc failed(%d)\n", ret);
- return ret;
- }
- req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf;
- req->id = id;
- req->resets = reset_state;
- ret = ti_sci_do_xfer(info, xfer);
- if (ret) {
- dev_err(dev, "Mbox send fail %d\n", ret);
- goto fail;
- }
- resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
- ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
- fail:
- ti_sci_put_one_xfer(&info->minfo, xfer);
- return ret;
- }
- /**
- * ti_sci_cmd_get_device_resets() - Get reset state for device managed
- * by TISCI
- * @handle: Pointer to TISCI handle
- * @id: Device Identifier
- * @reset_state: Pointer to reset state to populate
- *
- * Return: 0 if all went fine, else return appropriate error.
- */
- static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
- u32 id, u32 *reset_state)
- {
- return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
- NULL);
- }
- /**
- * ti_sci_set_clock_state() - Set clock state helper
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- * @flags: Header flags as needed
- * @state: State to request for the clock.
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
- u32 dev_id, u8 clk_id,
- u32 flags, u8 state)
- {
- struct ti_sci_info *info;
- struct ti_sci_msg_req_set_clock_state *req;
- struct ti_sci_msg_hdr *resp;
- struct ti_sci_xfer *xfer;
- struct device *dev;
- int ret = 0;
- if (IS_ERR(handle))
- return PTR_ERR(handle);
- if (!handle)
- return -EINVAL;
- info = handle_to_ti_sci_info(handle);
- dev = info->dev;
- xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
- flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
- sizeof(*req), sizeof(*resp));
- if (IS_ERR(xfer)) {
- ret = PTR_ERR(xfer);
- dev_err(dev, "Message alloc failed(%d)\n", ret);
- return ret;
- }
- req = (struct ti_sci_msg_req_set_clock_state *)xfer->xfer_buf;
- req->dev_id = dev_id;
- req->clk_id = clk_id;
- req->request_state = state;
- ret = ti_sci_do_xfer(info, xfer);
- if (ret) {
- dev_err(dev, "Mbox send fail %d\n", ret);
- goto fail;
- }
- resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
- ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
- fail:
- ti_sci_put_one_xfer(&info->minfo, xfer);
- return ret;
- }
- /**
- * ti_sci_cmd_get_clock_state() - Get clock state helper
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- * @programmed_state: State requested for clock to move to
- * @current_state: State that the clock is currently in
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
- u32 dev_id, u8 clk_id,
- u8 *programmed_state, u8 *current_state)
- {
- struct ti_sci_info *info;
- struct ti_sci_msg_req_get_clock_state *req;
- struct ti_sci_msg_resp_get_clock_state *resp;
- struct ti_sci_xfer *xfer;
- struct device *dev;
- int ret = 0;
- if (IS_ERR(handle))
- return PTR_ERR(handle);
- if (!handle)
- return -EINVAL;
- if (!programmed_state && !current_state)
- return -EINVAL;
- info = handle_to_ti_sci_info(handle);
- dev = info->dev;
- xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
- TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
- sizeof(*req), sizeof(*resp));
- if (IS_ERR(xfer)) {
- ret = PTR_ERR(xfer);
- dev_err(dev, "Message alloc failed(%d)\n", ret);
- return ret;
- }
- req = (struct ti_sci_msg_req_get_clock_state *)xfer->xfer_buf;
- req->dev_id = dev_id;
- req->clk_id = clk_id;
- ret = ti_sci_do_xfer(info, xfer);
- if (ret) {
- dev_err(dev, "Mbox send fail %d\n", ret);
- goto fail;
- }
- resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->xfer_buf;
- if (!ti_sci_is_response_ack(resp)) {
- ret = -ENODEV;
- goto fail;
- }
- if (programmed_state)
- *programmed_state = resp->programmed_state;
- if (current_state)
- *current_state = resp->current_state;
- fail:
- ti_sci_put_one_xfer(&info->minfo, xfer);
- return ret;
- }
- /**
- * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
- * @can_change_freq: 'true' if frequency change is desired, else 'false'
- * @enable_input_term: 'true' if input termination is desired, else 'false'
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
- u8 clk_id, bool needs_ssc, bool can_change_freq,
- bool enable_input_term)
- {
- u32 flags = 0;
- flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
- flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
- flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
- return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
- MSG_CLOCK_SW_STATE_REQ);
- }
- /**
- * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- *
- * NOTE: This clock must have been requested by get_clock previously.
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
- u32 dev_id, u8 clk_id)
- {
- return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
- MSG_CLOCK_SW_STATE_UNREQ);
- }
- /**
- * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- *
- * NOTE: This clock must have been requested by get_clock previously.
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
- u32 dev_id, u8 clk_id)
- {
- return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
- MSG_CLOCK_SW_STATE_AUTO);
- }
- /**
- * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- * @req_state: state indicating if the clock is auto managed
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
- u32 dev_id, u8 clk_id, bool *req_state)
- {
- u8 state = 0;
- int ret;
- if (!req_state)
- return -EINVAL;
- ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
- if (ret)
- return ret;
- *req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
- return 0;
- }
- /**
- * ti_sci_cmd_clk_is_on() - Is the clock ON
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- * @req_state: state indicating if the clock is managed by us and enabled
- * @curr_state: state indicating if the clock is ready for operation
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
- u8 clk_id, bool *req_state, bool *curr_state)
- {
- u8 c_state = 0, r_state = 0;
- int ret;
- if (!req_state && !curr_state)
- return -EINVAL;
- ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
- &r_state, &c_state);
- if (ret)
- return ret;
- if (req_state)
- *req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
- if (curr_state)
- *curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
- return 0;
- }
- /**
- * ti_sci_cmd_clk_is_off() - Is the clock OFF
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- * @req_state: state indicating if the clock is managed by us and disabled
- * @curr_state: state indicating if the clock is NOT ready for operation
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
- u8 clk_id, bool *req_state, bool *curr_state)
- {
- u8 c_state = 0, r_state = 0;
- int ret;
- if (!req_state && !curr_state)
- return -EINVAL;
- ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
- &r_state, &c_state);
- if (ret)
- return ret;
- if (req_state)
- *req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
- if (curr_state)
- *curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
- return 0;
- }
- /**
- * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- * @parent_id: Parent clock identifier to set
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
- u32 dev_id, u8 clk_id, u8 parent_id)
- {
- struct ti_sci_info *info;
- struct ti_sci_msg_req_set_clock_parent *req;
- struct ti_sci_msg_hdr *resp;
- struct ti_sci_xfer *xfer;
- struct device *dev;
- int ret = 0;
- if (IS_ERR(handle))
- return PTR_ERR(handle);
- if (!handle)
- return -EINVAL;
- info = handle_to_ti_sci_info(handle);
- dev = info->dev;
- xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
- TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
- sizeof(*req), sizeof(*resp));
- if (IS_ERR(xfer)) {
- ret = PTR_ERR(xfer);
- dev_err(dev, "Message alloc failed(%d)\n", ret);
- return ret;
- }
- req = (struct ti_sci_msg_req_set_clock_parent *)xfer->xfer_buf;
- req->dev_id = dev_id;
- req->clk_id = clk_id;
- req->parent_id = parent_id;
- ret = ti_sci_do_xfer(info, xfer);
- if (ret) {
- dev_err(dev, "Mbox send fail %d\n", ret);
- goto fail;
- }
- resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
- ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
- fail:
- ti_sci_put_one_xfer(&info->minfo, xfer);
- return ret;
- }
- /**
- * ti_sci_cmd_clk_get_parent() - Get current parent clock source
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- * @parent_id: Current clock parent
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
- u32 dev_id, u8 clk_id, u8 *parent_id)
- {
- struct ti_sci_info *info;
- struct ti_sci_msg_req_get_clock_parent *req;
- struct ti_sci_msg_resp_get_clock_parent *resp;
- struct ti_sci_xfer *xfer;
- struct device *dev;
- int ret = 0;
- if (IS_ERR(handle))
- return PTR_ERR(handle);
- if (!handle || !parent_id)
- return -EINVAL;
- info = handle_to_ti_sci_info(handle);
- dev = info->dev;
- xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
- TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
- sizeof(*req), sizeof(*resp));
- if (IS_ERR(xfer)) {
- ret = PTR_ERR(xfer);
- dev_err(dev, "Message alloc failed(%d)\n", ret);
- return ret;
- }
- req = (struct ti_sci_msg_req_get_clock_parent *)xfer->xfer_buf;
- req->dev_id = dev_id;
- req->clk_id = clk_id;
- ret = ti_sci_do_xfer(info, xfer);
- if (ret) {
- dev_err(dev, "Mbox send fail %d\n", ret);
- goto fail;
- }
- resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->xfer_buf;
- if (!ti_sci_is_response_ack(resp))
- ret = -ENODEV;
- else
- *parent_id = resp->parent_id;
- fail:
- ti_sci_put_one_xfer(&info->minfo, xfer);
- return ret;
- }
- /**
- * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- * @num_parents: Returns he number of parents to the current clock.
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
- u32 dev_id, u8 clk_id,
- u8 *num_parents)
- {
- struct ti_sci_info *info;
- struct ti_sci_msg_req_get_clock_num_parents *req;
- struct ti_sci_msg_resp_get_clock_num_parents *resp;
- struct ti_sci_xfer *xfer;
- struct device *dev;
- int ret = 0;
- if (IS_ERR(handle))
- return PTR_ERR(handle);
- if (!handle || !num_parents)
- return -EINVAL;
- info = handle_to_ti_sci_info(handle);
- dev = info->dev;
- xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
- TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
- sizeof(*req), sizeof(*resp));
- if (IS_ERR(xfer)) {
- ret = PTR_ERR(xfer);
- dev_err(dev, "Message alloc failed(%d)\n", ret);
- return ret;
- }
- req = (struct ti_sci_msg_req_get_clock_num_parents *)xfer->xfer_buf;
- req->dev_id = dev_id;
- req->clk_id = clk_id;
- ret = ti_sci_do_xfer(info, xfer);
- if (ret) {
- dev_err(dev, "Mbox send fail %d\n", ret);
- goto fail;
- }
- resp = (struct ti_sci_msg_resp_get_clock_num_parents *)xfer->xfer_buf;
- if (!ti_sci_is_response_ack(resp))
- ret = -ENODEV;
- else
- *num_parents = resp->num_parents;
- fail:
- ti_sci_put_one_xfer(&info->minfo, xfer);
- return ret;
- }
- /**
- * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- * @min_freq: The minimum allowable frequency in Hz. This is the minimum
- * allowable programmed frequency and does not account for clock
- * tolerances and jitter.
- * @target_freq: The target clock frequency in Hz. A frequency will be
- * processed as close to this target frequency as possible.
- * @max_freq: The maximum allowable frequency in Hz. This is the maximum
- * allowable programmed frequency and does not account for clock
- * tolerances and jitter.
- * @match_freq: Frequency match in Hz response.
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
- u32 dev_id, u8 clk_id, u64 min_freq,
- u64 target_freq, u64 max_freq,
- u64 *match_freq)
- {
- struct ti_sci_info *info;
- struct ti_sci_msg_req_query_clock_freq *req;
- struct ti_sci_msg_resp_query_clock_freq *resp;
- struct ti_sci_xfer *xfer;
- struct device *dev;
- int ret = 0;
- if (IS_ERR(handle))
- return PTR_ERR(handle);
- if (!handle || !match_freq)
- return -EINVAL;
- info = handle_to_ti_sci_info(handle);
- dev = info->dev;
- xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
- TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
- sizeof(*req), sizeof(*resp));
- if (IS_ERR(xfer)) {
- ret = PTR_ERR(xfer);
- dev_err(dev, "Message alloc failed(%d)\n", ret);
- return ret;
- }
- req = (struct ti_sci_msg_req_query_clock_freq *)xfer->xfer_buf;
- req->dev_id = dev_id;
- req->clk_id = clk_id;
- req->min_freq_hz = min_freq;
- req->target_freq_hz = target_freq;
- req->max_freq_hz = max_freq;
- ret = ti_sci_do_xfer(info, xfer);
- if (ret) {
- dev_err(dev, "Mbox send fail %d\n", ret);
- goto fail;
- }
- resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->xfer_buf;
- if (!ti_sci_is_response_ack(resp))
- ret = -ENODEV;
- else
- *match_freq = resp->freq_hz;
- fail:
- ti_sci_put_one_xfer(&info->minfo, xfer);
- return ret;
- }
- /**
- * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- * @min_freq: The minimum allowable frequency in Hz. This is the minimum
- * allowable programmed frequency and does not account for clock
- * tolerances and jitter.
- * @target_freq: The target clock frequency in Hz. A frequency will be
- * processed as close to this target frequency as possible.
- * @max_freq: The maximum allowable frequency in Hz. This is the maximum
- * allowable programmed frequency and does not account for clock
- * tolerances and jitter.
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
- u32 dev_id, u8 clk_id, u64 min_freq,
- u64 target_freq, u64 max_freq)
- {
- struct ti_sci_info *info;
- struct ti_sci_msg_req_set_clock_freq *req;
- struct ti_sci_msg_hdr *resp;
- struct ti_sci_xfer *xfer;
- struct device *dev;
- int ret = 0;
- if (IS_ERR(handle))
- return PTR_ERR(handle);
- if (!handle)
- return -EINVAL;
- info = handle_to_ti_sci_info(handle);
- dev = info->dev;
- xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
- TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
- sizeof(*req), sizeof(*resp));
- if (IS_ERR(xfer)) {
- ret = PTR_ERR(xfer);
- dev_err(dev, "Message alloc failed(%d)\n", ret);
- return ret;
- }
- req = (struct ti_sci_msg_req_set_clock_freq *)xfer->xfer_buf;
- req->dev_id = dev_id;
- req->clk_id = clk_id;
- req->min_freq_hz = min_freq;
- req->target_freq_hz = target_freq;
- req->max_freq_hz = max_freq;
- ret = ti_sci_do_xfer(info, xfer);
- if (ret) {
- dev_err(dev, "Mbox send fail %d\n", ret);
- goto fail;
- }
- resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
- ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
- fail:
- ti_sci_put_one_xfer(&info->minfo, xfer);
- return ret;
- }
- /**
- * ti_sci_cmd_clk_get_freq() - Get current frequency
- * @handle: pointer to TI SCI handle
- * @dev_id: Device identifier this request is for
- * @clk_id: Clock identifier for the device for this request.
- * Each device has it's own set of clock inputs. This indexes
- * which clock input to modify.
- * @freq: Currently frequency in Hz
- *
- * Return: 0 if all went well, else returns appropriate error value.
- */
- static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
- u32 dev_id, u8 clk_id, u64 *freq)
- {
- struct ti_sci_info *info;
- struct ti_sci_msg_req_get_clock_freq *req;
- struct ti_sci_msg_resp_get_clock_freq *resp;
- struct ti_sci_xfer *xfer;
- struct device *dev;
- int ret = 0;
- if (IS_ERR(handle))
- return PTR_ERR(handle);
- if (!handle || !freq)
- return -EINVAL;
- info = handle_to_ti_sci_info(handle);
- dev = info->dev;
- xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
- TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
- sizeof(*req), sizeof(*resp));
- if (IS_ERR(xfer)) {
- ret = PTR_ERR(xfer);
- dev_err(dev, "Message alloc failed(%d)\n", ret);
- return ret;
- }
- req = (struct ti_sci_msg_req_get_clock_freq *)xfer->xfer_buf;
- req->dev_id = dev_id;
- req->clk_id = clk_id;
- ret = ti_sci_do_xfer(info, xfer);
- if (ret) {
- dev_err(dev, "Mbox send fail %d\n", ret);
- goto fail;
- }
- resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->xfer_buf;
- if (!ti_sci_is_response_ack(resp))
- ret = -ENODEV;
- else
- *freq = resp->freq_hz;
- fail:
- ti_sci_put_one_xfer(&info->minfo, xfer);
- return ret;
- }
- static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
- {
- struct ti_sci_info *info;
- struct ti_sci_msg_req_reboot *req;
- struct ti_sci_msg_hdr *resp;
- struct ti_sci_xfer *xfer;
- struct device *dev;
- int ret = 0;
- if (IS_ERR(handle))
- return PTR_ERR(handle);
- if (!handle)
- return -EINVAL;
- info = handle_to_ti_sci_info(handle);
- dev = info->dev;
- xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SYS_RESET,
- TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
- sizeof(*req), sizeof(*resp));
- if (IS_ERR(xfer)) {
- ret = PTR_ERR(xfer);
- dev_err(dev, "Message alloc failed(%d)\n", ret);
- return ret;
- }
- req = (struct ti_sci_msg_req_reboot *)xfer->xfer_buf;
- ret = ti_sci_do_xfer(info, xfer);
- if (ret) {
- dev_err(dev, "Mbox send fail %d\n", ret);
- goto fail;
- }
- resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
- if (!ti_sci_is_response_ack(resp))
- ret = -ENODEV;
- else
- ret = 0;
- fail:
- ti_sci_put_one_xfer(&info->minfo, xfer);
- return ret;
- }
- /*
- * ti_sci_setup_ops() - Setup the operations structures
- * @info: pointer to TISCI pointer
- */
- static void ti_sci_setup_ops(struct ti_sci_info *info)
- {
- struct ti_sci_ops *ops = &info->handle.ops;
- struct ti_sci_core_ops *core_ops = &ops->core_ops;
- struct ti_sci_dev_ops *dops = &ops->dev_ops;
- struct ti_sci_clk_ops *cops = &ops->clk_ops;
- core_ops->reboot_device = ti_sci_cmd_core_reboot;
- dops->get_device = ti_sci_cmd_get_device;
- dops->idle_device = ti_sci_cmd_idle_device;
- dops->put_device = ti_sci_cmd_put_device;
- dops->is_valid = ti_sci_cmd_dev_is_valid;
- dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
- dops->is_idle = ti_sci_cmd_dev_is_idle;
- dops->is_stop = ti_sci_cmd_dev_is_stop;
- dops->is_on = ti_sci_cmd_dev_is_on;
- dops->is_transitioning = ti_sci_cmd_dev_is_trans;
- dops->set_device_resets = ti_sci_cmd_set_device_resets;
- dops->get_device_resets = ti_sci_cmd_get_device_resets;
- cops->get_clock = ti_sci_cmd_get_clock;
- cops->idle_clock = ti_sci_cmd_idle_clock;
- cops->put_clock = ti_sci_cmd_put_clock;
- cops->is_auto = ti_sci_cmd_clk_is_auto;
- cops->is_on = ti_sci_cmd_clk_is_on;
- cops->is_off = ti_sci_cmd_clk_is_off;
- cops->set_parent = ti_sci_cmd_clk_set_parent;
- cops->get_parent = ti_sci_cmd_clk_get_parent;
- cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
- cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
- cops->set_freq = ti_sci_cmd_clk_set_freq;
- cops->get_freq = ti_sci_cmd_clk_get_freq;
- }
- /**
- * ti_sci_get_handle() - Get the TI SCI handle for a device
- * @dev: Pointer to device for which we want SCI handle
- *
- * NOTE: The function does not track individual clients of the framework
- * and is expected to be maintained by caller of TI SCI protocol library.
- * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
- * Return: pointer to handle if successful, else:
- * -EPROBE_DEFER if the instance is not ready
- * -ENODEV if the required node handler is missing
- * -EINVAL if invalid conditions are encountered.
- */
- const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
- {
- struct device_node *ti_sci_np;
- struct list_head *p;
- struct ti_sci_handle *handle = NULL;
- struct ti_sci_info *info;
- if (!dev) {
- pr_err("I need a device pointer\n");
- return ERR_PTR(-EINVAL);
- }
- ti_sci_np = of_get_parent(dev->of_node);
- if (!ti_sci_np) {
- dev_err(dev, "No OF information\n");
- return ERR_PTR(-EINVAL);
- }
- mutex_lock(&ti_sci_list_mutex);
- list_for_each(p, &ti_sci_list) {
- info = list_entry(p, struct ti_sci_info, node);
- if (ti_sci_np == info->dev->of_node) {
- handle = &info->handle;
- info->users++;
- break;
- }
- }
- mutex_unlock(&ti_sci_list_mutex);
- of_node_put(ti_sci_np);
- if (!handle)
- return ERR_PTR(-EPROBE_DEFER);
- return handle;
- }
- EXPORT_SYMBOL_GPL(ti_sci_get_handle);
- /**
- * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
- * @handle: Handle acquired by ti_sci_get_handle
- *
- * NOTE: The function does not track individual clients of the framework
- * and is expected to be maintained by caller of TI SCI protocol library.
- * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
- *
- * Return: 0 is successfully released
- * if an error pointer was passed, it returns the error value back,
- * if null was passed, it returns -EINVAL;
- */
- int ti_sci_put_handle(const struct ti_sci_handle *handle)
- {
- struct ti_sci_info *info;
- if (IS_ERR(handle))
- return PTR_ERR(handle);
- if (!handle)
- return -EINVAL;
- info = handle_to_ti_sci_info(handle);
- mutex_lock(&ti_sci_list_mutex);
- if (!WARN_ON(!info->users))
- info->users--;
- mutex_unlock(&ti_sci_list_mutex);
- return 0;
- }
- EXPORT_SYMBOL_GPL(ti_sci_put_handle);
- static void devm_ti_sci_release(struct device *dev, void *res)
- {
- const struct ti_sci_handle **ptr = res;
- const struct ti_sci_handle *handle = *ptr;
- int ret;
- ret = ti_sci_put_handle(handle);
- if (ret)
- dev_err(dev, "failed to put handle %d\n", ret);
- }
- /**
- * devm_ti_sci_get_handle() - Managed get handle
- * @dev: device for which we want SCI handle for.
- *
- * NOTE: This releases the handle once the device resources are
- * no longer needed. MUST NOT BE released with ti_sci_put_handle.
- * The function does not track individual clients of the framework
- * and is expected to be maintained by caller of TI SCI protocol library.
- *
- * Return: 0 if all went fine, else corresponding error.
- */
- const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
- {
- const struct ti_sci_handle **ptr;
- const struct ti_sci_handle *handle;
- ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
- if (!ptr)
- return ERR_PTR(-ENOMEM);
- handle = ti_sci_get_handle(dev);
- if (!IS_ERR(handle)) {
- *ptr = handle;
- devres_add(dev, ptr);
- } else {
- devres_free(ptr);
- }
- return handle;
- }
- EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
- static int tisci_reboot_handler(struct notifier_block *nb, unsigned long mode,
- void *cmd)
- {
- struct ti_sci_info *info = reboot_to_ti_sci_info(nb);
- const struct ti_sci_handle *handle = &info->handle;
- ti_sci_cmd_core_reboot(handle);
- /* call fail OR pass, we should not be here in the first place */
- return NOTIFY_BAD;
- }
- /* Description for K2G */
- static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
- .host_id = 2,
- /* Conservative duration */
- .max_rx_timeout_ms = 1000,
- /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
- .max_msgs = 20,
- .max_msg_size = 64,
- };
- static const struct of_device_id ti_sci_of_match[] = {
- {.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
- { /* Sentinel */ },
- };
- MODULE_DEVICE_TABLE(of, ti_sci_of_match);
- static int ti_sci_probe(struct platform_device *pdev)
- {
- struct device *dev = &pdev->dev;
- const struct of_device_id *of_id;
- const struct ti_sci_desc *desc;
- struct ti_sci_xfer *xfer;
- struct ti_sci_info *info = NULL;
- struct ti_sci_xfers_info *minfo;
- struct mbox_client *cl;
- int ret = -EINVAL;
- int i;
- int reboot = 0;
- of_id = of_match_device(ti_sci_of_match, dev);
- if (!of_id) {
- dev_err(dev, "OF data missing\n");
- return -EINVAL;
- }
- desc = of_id->data;
- info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
- if (!info)
- return -ENOMEM;
- info->dev = dev;
- info->desc = desc;
- reboot = of_property_read_bool(dev->of_node,
- "ti,system-reboot-controller");
- INIT_LIST_HEAD(&info->node);
- minfo = &info->minfo;
- /*
- * Pre-allocate messages
- * NEVER allocate more than what we can indicate in hdr.seq
- * if we have data description bug, force a fix..
- */
- if (WARN_ON(desc->max_msgs >=
- 1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
- return -EINVAL;
- minfo->xfer_block = devm_kcalloc(dev,
- desc->max_msgs,
- sizeof(*minfo->xfer_block),
- GFP_KERNEL);
- if (!minfo->xfer_block)
- return -ENOMEM;
- minfo->xfer_alloc_table = devm_kcalloc(dev,
- BITS_TO_LONGS(desc->max_msgs),
- sizeof(unsigned long),
- GFP_KERNEL);
- if (!minfo->xfer_alloc_table)
- return -ENOMEM;
- bitmap_zero(minfo->xfer_alloc_table, desc->max_msgs);
- /* Pre-initialize the buffer pointer to pre-allocated buffers */
- for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
- xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
- GFP_KERNEL);
- if (!xfer->xfer_buf)
- return -ENOMEM;
- xfer->tx_message.buf = xfer->xfer_buf;
- init_completion(&xfer->done);
- }
- ret = ti_sci_debugfs_create(pdev, info);
- if (ret)
- dev_warn(dev, "Failed to create debug file\n");
- platform_set_drvdata(pdev, info);
- cl = &info->cl;
- cl->dev = dev;
- cl->tx_block = false;
- cl->rx_callback = ti_sci_rx_callback;
- cl->knows_txdone = true;
- spin_lock_init(&minfo->xfer_lock);
- sema_init(&minfo->sem_xfer_count, desc->max_msgs);
- info->chan_rx = mbox_request_channel_byname(cl, "rx");
- if (IS_ERR(info->chan_rx)) {
- ret = PTR_ERR(info->chan_rx);
- goto out;
- }
- info->chan_tx = mbox_request_channel_byname(cl, "tx");
- if (IS_ERR(info->chan_tx)) {
- ret = PTR_ERR(info->chan_tx);
- goto out;
- }
- ret = ti_sci_cmd_get_revision(info);
- if (ret) {
- dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
- goto out;
- }
- ti_sci_setup_ops(info);
- if (reboot) {
- info->nb.notifier_call = tisci_reboot_handler;
- info->nb.priority = 128;
- ret = register_restart_handler(&info->nb);
- if (ret) {
- dev_err(dev, "reboot registration fail(%d)\n", ret);
- return ret;
- }
- }
- dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
- info->handle.version.abi_major, info->handle.version.abi_minor,
- info->handle.version.firmware_revision,
- info->handle.version.firmware_description);
- mutex_lock(&ti_sci_list_mutex);
- list_add_tail(&info->node, &ti_sci_list);
- mutex_unlock(&ti_sci_list_mutex);
- return of_platform_populate(dev->of_node, NULL, NULL, dev);
- out:
- if (!IS_ERR(info->chan_tx))
- mbox_free_channel(info->chan_tx);
- if (!IS_ERR(info->chan_rx))
- mbox_free_channel(info->chan_rx);
- debugfs_remove(info->d);
- return ret;
- }
- static int ti_sci_remove(struct platform_device *pdev)
- {
- struct ti_sci_info *info;
- struct device *dev = &pdev->dev;
- int ret = 0;
- of_platform_depopulate(dev);
- info = platform_get_drvdata(pdev);
- if (info->nb.notifier_call)
- unregister_restart_handler(&info->nb);
- mutex_lock(&ti_sci_list_mutex);
- if (info->users)
- ret = -EBUSY;
- else
- list_del(&info->node);
- mutex_unlock(&ti_sci_list_mutex);
- if (!ret) {
- ti_sci_debugfs_destroy(pdev, info);
- /* Safe to free channels since no more users */
- mbox_free_channel(info->chan_tx);
- mbox_free_channel(info->chan_rx);
- }
- return ret;
- }
- static struct platform_driver ti_sci_driver = {
- .probe = ti_sci_probe,
- .remove = ti_sci_remove,
- .driver = {
- .name = "ti-sci",
- .of_match_table = of_match_ptr(ti_sci_of_match),
- },
- };
- module_platform_driver(ti_sci_driver);
- MODULE_LICENSE("GPL v2");
- MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
- MODULE_AUTHOR("Nishanth Menon");
- MODULE_ALIAS("platform:ti-sci");
|