1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2011-2017, The Linux Foundation
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/errno.h>
7*4882a593Smuzhiyun #include "slimbus.h"
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun /**
10*4882a593Smuzhiyun * slim_ctrl_clk_pause() - Called by slimbus controller to enter/exit
11*4882a593Smuzhiyun * 'clock pause'
12*4882a593Smuzhiyun * @ctrl: controller requesting bus to be paused or woken up
13*4882a593Smuzhiyun * @wakeup: Wakeup this controller from clock pause.
14*4882a593Smuzhiyun * @restart: Restart time value per spec used for clock pause. This value
15*4882a593Smuzhiyun * isn't used when controller is to be woken up.
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * Slimbus specification needs this sequence to turn-off clocks for the bus.
18*4882a593Smuzhiyun * The sequence involves sending 3 broadcast messages (reconfiguration
19*4882a593Smuzhiyun * sequence) to inform all devices on the bus.
20*4882a593Smuzhiyun * To exit clock-pause, controller typically wakes up active framer device.
21*4882a593Smuzhiyun * This API executes clock pause reconfiguration sequence if wakeup is false.
22*4882a593Smuzhiyun * If wakeup is true, controller's wakeup is called.
23*4882a593Smuzhiyun * For entering clock-pause, -EBUSY is returned if a message txn in pending.
24*4882a593Smuzhiyun */
slim_ctrl_clk_pause(struct slim_controller * ctrl,bool wakeup,u8 restart)25*4882a593Smuzhiyun int slim_ctrl_clk_pause(struct slim_controller *ctrl, bool wakeup, u8 restart)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun int i, ret = 0;
28*4882a593Smuzhiyun unsigned long flags;
29*4882a593Smuzhiyun struct slim_sched *sched = &ctrl->sched;
30*4882a593Smuzhiyun struct slim_val_inf msg = {0, 0, NULL, NULL};
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun DEFINE_SLIM_BCAST_TXN(txn, SLIM_MSG_MC_BEGIN_RECONFIGURATION,
33*4882a593Smuzhiyun 3, SLIM_LA_MANAGER, &msg);
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun if (wakeup == false && restart > SLIM_CLK_UNSPECIFIED)
36*4882a593Smuzhiyun return -EINVAL;
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun mutex_lock(&sched->m_reconf);
39*4882a593Smuzhiyun if (wakeup) {
40*4882a593Smuzhiyun if (sched->clk_state == SLIM_CLK_ACTIVE) {
41*4882a593Smuzhiyun mutex_unlock(&sched->m_reconf);
42*4882a593Smuzhiyun return 0;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun * Fine-tune calculation based on clock gear,
47*4882a593Smuzhiyun * message-bandwidth after bandwidth management
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun ret = wait_for_completion_timeout(&sched->pause_comp,
50*4882a593Smuzhiyun msecs_to_jiffies(100));
51*4882a593Smuzhiyun if (!ret) {
52*4882a593Smuzhiyun mutex_unlock(&sched->m_reconf);
53*4882a593Smuzhiyun pr_err("Previous clock pause did not finish");
54*4882a593Smuzhiyun return -ETIMEDOUT;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun ret = 0;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun * Slimbus framework will call controller wakeup
60*4882a593Smuzhiyun * Controller should make sure that it sets active framer
61*4882a593Smuzhiyun * out of clock pause
62*4882a593Smuzhiyun */
63*4882a593Smuzhiyun if (sched->clk_state == SLIM_CLK_PAUSED && ctrl->wakeup)
64*4882a593Smuzhiyun ret = ctrl->wakeup(ctrl);
65*4882a593Smuzhiyun if (!ret)
66*4882a593Smuzhiyun sched->clk_state = SLIM_CLK_ACTIVE;
67*4882a593Smuzhiyun mutex_unlock(&sched->m_reconf);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun return ret;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /* already paused */
73*4882a593Smuzhiyun if (ctrl->sched.clk_state == SLIM_CLK_PAUSED) {
74*4882a593Smuzhiyun mutex_unlock(&sched->m_reconf);
75*4882a593Smuzhiyun return 0;
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun spin_lock_irqsave(&ctrl->txn_lock, flags);
79*4882a593Smuzhiyun for (i = 0; i < SLIM_MAX_TIDS; i++) {
80*4882a593Smuzhiyun /* Pending response for a message */
81*4882a593Smuzhiyun if (idr_find(&ctrl->tid_idr, i)) {
82*4882a593Smuzhiyun spin_unlock_irqrestore(&ctrl->txn_lock, flags);
83*4882a593Smuzhiyun mutex_unlock(&sched->m_reconf);
84*4882a593Smuzhiyun return -EBUSY;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun spin_unlock_irqrestore(&ctrl->txn_lock, flags);
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun sched->clk_state = SLIM_CLK_ENTERING_PAUSE;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* clock pause sequence */
92*4882a593Smuzhiyun ret = slim_do_transfer(ctrl, &txn);
93*4882a593Smuzhiyun if (ret)
94*4882a593Smuzhiyun goto clk_pause_ret;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun txn.mc = SLIM_MSG_MC_NEXT_PAUSE_CLOCK;
97*4882a593Smuzhiyun txn.rl = 4;
98*4882a593Smuzhiyun msg.num_bytes = 1;
99*4882a593Smuzhiyun msg.wbuf = &restart;
100*4882a593Smuzhiyun ret = slim_do_transfer(ctrl, &txn);
101*4882a593Smuzhiyun if (ret)
102*4882a593Smuzhiyun goto clk_pause_ret;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun txn.mc = SLIM_MSG_MC_RECONFIGURE_NOW;
105*4882a593Smuzhiyun txn.rl = 3;
106*4882a593Smuzhiyun msg.num_bytes = 1;
107*4882a593Smuzhiyun msg.wbuf = NULL;
108*4882a593Smuzhiyun ret = slim_do_transfer(ctrl, &txn);
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun clk_pause_ret:
111*4882a593Smuzhiyun if (ret) {
112*4882a593Smuzhiyun sched->clk_state = SLIM_CLK_ACTIVE;
113*4882a593Smuzhiyun } else {
114*4882a593Smuzhiyun sched->clk_state = SLIM_CLK_PAUSED;
115*4882a593Smuzhiyun complete(&sched->pause_comp);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun mutex_unlock(&sched->m_reconf);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun return ret;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(slim_ctrl_clk_pause);
122