arm_scpi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. /*
  2. * System Control and Power Interface (SCPI) Message Protocol driver
  3. *
  4. * SCPI Message Protocol is used between the System Control Processor(SCP)
  5. * and the Application Processors(AP). The Message Handling Unit(MHU)
  6. * provides a mechanism for inter-processor communication between SCP's
  7. * Cortex M3 and AP.
  8. *
  9. * SCP offers control and management of the core/cluster power states,
  10. * various power domain DVFS including the core/cluster, certain system
  11. * clocks configuration, thermal sensors and many others.
  12. *
  13. * Copyright (C) 2015 ARM Ltd.
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms and conditions of the GNU General Public License,
  17. * version 2, as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope it will be useful, but WITHOUT
  20. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  21. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  22. * more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program. If not, see <http://www.gnu.org/licenses/>.
  26. */
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  28. #include <linux/bitmap.h>
  29. #include <linux/device.h>
  30. #include <linux/err.h>
  31. #include <linux/export.h>
  32. #include <linux/io.h>
  33. #include <linux/kernel.h>
  34. #include <linux/list.h>
  35. #include <linux/mailbox_client.h>
  36. #include <linux/module.h>
  37. #include <linux/of_address.h>
  38. #include <linux/of_platform.h>
  39. #include <linux/printk.h>
  40. #include <linux/scpi_protocol.h>
  41. #include <linux/slab.h>
  42. #include <linux/sort.h>
  43. #include <linux/spinlock.h>
  44. #define CMD_ID_SHIFT 0
  45. #define CMD_ID_MASK 0x7f
  46. #define CMD_TOKEN_ID_SHIFT 8
  47. #define CMD_TOKEN_ID_MASK 0xff
  48. #define CMD_DATA_SIZE_SHIFT 16
  49. #define CMD_DATA_SIZE_MASK 0x1ff
  50. #define PACK_SCPI_CMD(cmd_id, tx_sz) \
  51. ((((cmd_id) & CMD_ID_MASK) << CMD_ID_SHIFT) | \
  52. (((tx_sz) & CMD_DATA_SIZE_MASK) << CMD_DATA_SIZE_SHIFT))
  53. #define ADD_SCPI_TOKEN(cmd, token) \
  54. ((cmd) |= (((token) & CMD_TOKEN_ID_MASK) << CMD_TOKEN_ID_SHIFT))
  55. #define CMD_SIZE(cmd) (((cmd) >> CMD_DATA_SIZE_SHIFT) & CMD_DATA_SIZE_MASK)
  56. #define CMD_UNIQ_MASK (CMD_TOKEN_ID_MASK << CMD_TOKEN_ID_SHIFT | CMD_ID_MASK)
  57. #define CMD_XTRACT_UNIQ(cmd) ((cmd) & CMD_UNIQ_MASK)
  58. #define SCPI_SLOT 0
  59. #define MAX_DVFS_DOMAINS 8
  60. #define MAX_DVFS_OPPS 8
  61. #define DVFS_LATENCY(hdr) (le32_to_cpu(hdr) >> 16)
  62. #define DVFS_OPP_COUNT(hdr) ((le32_to_cpu(hdr) >> 8) & 0xff)
  63. #define PROTOCOL_REV_MINOR_BITS 16
  64. #define PROTOCOL_REV_MINOR_MASK ((1U << PROTOCOL_REV_MINOR_BITS) - 1)
  65. #define PROTOCOL_REV_MAJOR(x) ((x) >> PROTOCOL_REV_MINOR_BITS)
  66. #define PROTOCOL_REV_MINOR(x) ((x) & PROTOCOL_REV_MINOR_MASK)
  67. #define FW_REV_MAJOR_BITS 24
  68. #define FW_REV_MINOR_BITS 16
  69. #define FW_REV_PATCH_MASK ((1U << FW_REV_MINOR_BITS) - 1)
  70. #define FW_REV_MINOR_MASK ((1U << FW_REV_MAJOR_BITS) - 1)
  71. #define FW_REV_MAJOR(x) ((x) >> FW_REV_MAJOR_BITS)
  72. #define FW_REV_MINOR(x) (((x) & FW_REV_MINOR_MASK) >> FW_REV_MINOR_BITS)
  73. #define FW_REV_PATCH(x) ((x) & FW_REV_PATCH_MASK)
  74. #define MAX_RX_TIMEOUT (msecs_to_jiffies(30))
  75. enum scpi_error_codes {
  76. SCPI_SUCCESS = 0, /* Success */
  77. SCPI_ERR_PARAM = 1, /* Invalid parameter(s) */
  78. SCPI_ERR_ALIGN = 2, /* Invalid alignment */
  79. SCPI_ERR_SIZE = 3, /* Invalid size */
  80. SCPI_ERR_HANDLER = 4, /* Invalid handler/callback */
  81. SCPI_ERR_ACCESS = 5, /* Invalid access/permission denied */
  82. SCPI_ERR_RANGE = 6, /* Value out of range */
  83. SCPI_ERR_TIMEOUT = 7, /* Timeout has occurred */
  84. SCPI_ERR_NOMEM = 8, /* Invalid memory area or pointer */
  85. SCPI_ERR_PWRSTATE = 9, /* Invalid power state */
  86. SCPI_ERR_SUPPORT = 10, /* Not supported or disabled */
  87. SCPI_ERR_DEVICE = 11, /* Device error */
  88. SCPI_ERR_BUSY = 12, /* Device busy */
  89. SCPI_ERR_MAX
  90. };
  91. enum scpi_std_cmd {
  92. SCPI_CMD_INVALID = 0x00,
  93. SCPI_CMD_SCPI_READY = 0x01,
  94. SCPI_CMD_SCPI_CAPABILITIES = 0x02,
  95. SCPI_CMD_SET_CSS_PWR_STATE = 0x03,
  96. SCPI_CMD_GET_CSS_PWR_STATE = 0x04,
  97. SCPI_CMD_SET_SYS_PWR_STATE = 0x05,
  98. SCPI_CMD_SET_CPU_TIMER = 0x06,
  99. SCPI_CMD_CANCEL_CPU_TIMER = 0x07,
  100. SCPI_CMD_DVFS_CAPABILITIES = 0x08,
  101. SCPI_CMD_GET_DVFS_INFO = 0x09,
  102. SCPI_CMD_SET_DVFS = 0x0a,
  103. SCPI_CMD_GET_DVFS = 0x0b,
  104. SCPI_CMD_GET_DVFS_STAT = 0x0c,
  105. SCPI_CMD_CLOCK_CAPABILITIES = 0x0d,
  106. SCPI_CMD_GET_CLOCK_INFO = 0x0e,
  107. SCPI_CMD_SET_CLOCK_VALUE = 0x0f,
  108. SCPI_CMD_GET_CLOCK_VALUE = 0x10,
  109. SCPI_CMD_PSU_CAPABILITIES = 0x11,
  110. SCPI_CMD_GET_PSU_INFO = 0x12,
  111. SCPI_CMD_SET_PSU = 0x13,
  112. SCPI_CMD_GET_PSU = 0x14,
  113. SCPI_CMD_SENSOR_CAPABILITIES = 0x15,
  114. SCPI_CMD_SENSOR_INFO = 0x16,
  115. SCPI_CMD_SENSOR_VALUE = 0x17,
  116. SCPI_CMD_SENSOR_CFG_PERIODIC = 0x18,
  117. SCPI_CMD_SENSOR_CFG_BOUNDS = 0x19,
  118. SCPI_CMD_SENSOR_ASYNC_VALUE = 0x1a,
  119. SCPI_CMD_SET_DEVICE_PWR_STATE = 0x1b,
  120. SCPI_CMD_GET_DEVICE_PWR_STATE = 0x1c,
  121. SCPI_CMD_COUNT
  122. };
  123. struct scpi_xfer {
  124. u32 slot; /* has to be first element */
  125. u32 cmd;
  126. u32 status;
  127. const void *tx_buf;
  128. void *rx_buf;
  129. unsigned int tx_len;
  130. unsigned int rx_len;
  131. struct list_head node;
  132. struct completion done;
  133. };
  134. struct scpi_chan {
  135. struct mbox_client cl;
  136. struct mbox_chan *chan;
  137. void __iomem *tx_payload;
  138. void __iomem *rx_payload;
  139. struct list_head rx_pending;
  140. struct list_head xfers_list;
  141. struct scpi_xfer *xfers;
  142. spinlock_t rx_lock; /* locking for the rx pending list */
  143. struct mutex xfers_lock;
  144. u8 token;
  145. };
  146. struct scpi_drvinfo {
  147. u32 protocol_version;
  148. u32 firmware_version;
  149. int num_chans;
  150. atomic_t next_chan;
  151. struct scpi_ops *scpi_ops;
  152. struct scpi_chan *channels;
  153. struct scpi_dvfs_info *dvfs[MAX_DVFS_DOMAINS];
  154. };
  155. /*
  156. * The SCP firmware only executes in little-endian mode, so any buffers
  157. * shared through SCPI should have their contents converted to little-endian
  158. */
  159. struct scpi_shared_mem {
  160. __le32 command;
  161. __le32 status;
  162. u8 payload[0];
  163. } __packed;
  164. struct scp_capabilities {
  165. __le32 protocol_version;
  166. __le32 event_version;
  167. __le32 platform_version;
  168. __le32 commands[4];
  169. } __packed;
  170. struct clk_get_info {
  171. __le16 id;
  172. __le16 flags;
  173. __le32 min_rate;
  174. __le32 max_rate;
  175. u8 name[20];
  176. } __packed;
  177. struct clk_get_value {
  178. __le32 rate;
  179. } __packed;
  180. struct clk_set_value {
  181. __le16 id;
  182. __le16 reserved;
  183. __le32 rate;
  184. } __packed;
  185. struct dvfs_info {
  186. __le32 header;
  187. struct {
  188. __le32 freq;
  189. __le32 m_volt;
  190. } opps[MAX_DVFS_OPPS];
  191. } __packed;
  192. struct dvfs_set {
  193. u8 domain;
  194. u8 index;
  195. } __packed;
  196. struct sensor_capabilities {
  197. __le16 sensors;
  198. } __packed;
  199. struct _scpi_sensor_info {
  200. __le16 sensor_id;
  201. u8 class;
  202. u8 trigger_type;
  203. char name[20];
  204. };
  205. struct sensor_value {
  206. __le32 lo_val;
  207. __le32 hi_val;
  208. } __packed;
  209. struct dev_pstate_set {
  210. u16 dev_id;
  211. u8 pstate;
  212. } __packed;
  213. static struct scpi_drvinfo *scpi_info;
  214. static int scpi_linux_errmap[SCPI_ERR_MAX] = {
  215. /* better than switch case as long as return value is continuous */
  216. 0, /* SCPI_SUCCESS */
  217. -EINVAL, /* SCPI_ERR_PARAM */
  218. -ENOEXEC, /* SCPI_ERR_ALIGN */
  219. -EMSGSIZE, /* SCPI_ERR_SIZE */
  220. -EINVAL, /* SCPI_ERR_HANDLER */
  221. -EACCES, /* SCPI_ERR_ACCESS */
  222. -ERANGE, /* SCPI_ERR_RANGE */
  223. -ETIMEDOUT, /* SCPI_ERR_TIMEOUT */
  224. -ENOMEM, /* SCPI_ERR_NOMEM */
  225. -EINVAL, /* SCPI_ERR_PWRSTATE */
  226. -EOPNOTSUPP, /* SCPI_ERR_SUPPORT */
  227. -EIO, /* SCPI_ERR_DEVICE */
  228. -EBUSY, /* SCPI_ERR_BUSY */
  229. };
  230. static inline int scpi_to_linux_errno(int errno)
  231. {
  232. if (errno >= SCPI_SUCCESS && errno < SCPI_ERR_MAX)
  233. return scpi_linux_errmap[errno];
  234. return -EIO;
  235. }
  236. static void scpi_process_cmd(struct scpi_chan *ch, u32 cmd)
  237. {
  238. unsigned long flags;
  239. struct scpi_xfer *t, *match = NULL;
  240. spin_lock_irqsave(&ch->rx_lock, flags);
  241. if (list_empty(&ch->rx_pending)) {
  242. spin_unlock_irqrestore(&ch->rx_lock, flags);
  243. return;
  244. }
  245. list_for_each_entry(t, &ch->rx_pending, node)
  246. if (CMD_XTRACT_UNIQ(t->cmd) == CMD_XTRACT_UNIQ(cmd)) {
  247. list_del(&t->node);
  248. match = t;
  249. break;
  250. }
  251. /* check if wait_for_completion is in progress or timed-out */
  252. if (match && !completion_done(&match->done)) {
  253. struct scpi_shared_mem *mem = ch->rx_payload;
  254. unsigned int len = min(match->rx_len, CMD_SIZE(cmd));
  255. match->status = le32_to_cpu(mem->status);
  256. memcpy_fromio(match->rx_buf, mem->payload, len);
  257. if (match->rx_len > len)
  258. memset(match->rx_buf + len, 0, match->rx_len - len);
  259. complete(&match->done);
  260. }
  261. spin_unlock_irqrestore(&ch->rx_lock, flags);
  262. }
  263. static void scpi_handle_remote_msg(struct mbox_client *c, void *msg)
  264. {
  265. struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
  266. struct scpi_shared_mem *mem = ch->rx_payload;
  267. u32 cmd = le32_to_cpu(mem->command);
  268. scpi_process_cmd(ch, cmd);
  269. }
  270. static void scpi_tx_prepare(struct mbox_client *c, void *msg)
  271. {
  272. unsigned long flags;
  273. struct scpi_xfer *t = msg;
  274. struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
  275. struct scpi_shared_mem *mem = (struct scpi_shared_mem *)ch->tx_payload;
  276. if (t->tx_buf)
  277. memcpy_toio(mem->payload, t->tx_buf, t->tx_len);
  278. if (t->rx_buf) {
  279. if (!(++ch->token))
  280. ++ch->token;
  281. ADD_SCPI_TOKEN(t->cmd, ch->token);
  282. spin_lock_irqsave(&ch->rx_lock, flags);
  283. list_add_tail(&t->node, &ch->rx_pending);
  284. spin_unlock_irqrestore(&ch->rx_lock, flags);
  285. }
  286. mem->command = cpu_to_le32(t->cmd);
  287. }
  288. static struct scpi_xfer *get_scpi_xfer(struct scpi_chan *ch)
  289. {
  290. struct scpi_xfer *t;
  291. mutex_lock(&ch->xfers_lock);
  292. if (list_empty(&ch->xfers_list)) {
  293. mutex_unlock(&ch->xfers_lock);
  294. return NULL;
  295. }
  296. t = list_first_entry(&ch->xfers_list, struct scpi_xfer, node);
  297. list_del(&t->node);
  298. mutex_unlock(&ch->xfers_lock);
  299. return t;
  300. }
  301. static void put_scpi_xfer(struct scpi_xfer *t, struct scpi_chan *ch)
  302. {
  303. mutex_lock(&ch->xfers_lock);
  304. list_add_tail(&t->node, &ch->xfers_list);
  305. mutex_unlock(&ch->xfers_lock);
  306. }
  307. static int scpi_send_message(u8 cmd, void *tx_buf, unsigned int tx_len,
  308. void *rx_buf, unsigned int rx_len)
  309. {
  310. int ret;
  311. u8 chan;
  312. struct scpi_xfer *msg;
  313. struct scpi_chan *scpi_chan;
  314. chan = atomic_inc_return(&scpi_info->next_chan) % scpi_info->num_chans;
  315. scpi_chan = scpi_info->channels + chan;
  316. msg = get_scpi_xfer(scpi_chan);
  317. if (!msg)
  318. return -ENOMEM;
  319. msg->slot = BIT(SCPI_SLOT);
  320. msg->cmd = PACK_SCPI_CMD(cmd, tx_len);
  321. msg->tx_buf = tx_buf;
  322. msg->tx_len = tx_len;
  323. msg->rx_buf = rx_buf;
  324. msg->rx_len = rx_len;
  325. init_completion(&msg->done);
  326. ret = mbox_send_message(scpi_chan->chan, msg);
  327. if (ret < 0 || !rx_buf)
  328. goto out;
  329. if (!wait_for_completion_timeout(&msg->done, MAX_RX_TIMEOUT))
  330. ret = -ETIMEDOUT;
  331. else
  332. /* first status word */
  333. ret = msg->status;
  334. out:
  335. if (ret < 0 && rx_buf) /* remove entry from the list if timed-out */
  336. scpi_process_cmd(scpi_chan, msg->cmd);
  337. put_scpi_xfer(msg, scpi_chan);
  338. /* SCPI error codes > 0, translate them to Linux scale*/
  339. return ret > 0 ? scpi_to_linux_errno(ret) : ret;
  340. }
  341. static u32 scpi_get_version(void)
  342. {
  343. return scpi_info->protocol_version;
  344. }
  345. static int
  346. scpi_clk_get_range(u16 clk_id, unsigned long *min, unsigned long *max)
  347. {
  348. int ret;
  349. struct clk_get_info clk;
  350. __le16 le_clk_id = cpu_to_le16(clk_id);
  351. ret = scpi_send_message(SCPI_CMD_GET_CLOCK_INFO, &le_clk_id,
  352. sizeof(le_clk_id), &clk, sizeof(clk));
  353. if (!ret) {
  354. *min = le32_to_cpu(clk.min_rate);
  355. *max = le32_to_cpu(clk.max_rate);
  356. }
  357. return ret;
  358. }
  359. static unsigned long scpi_clk_get_val(u16 clk_id)
  360. {
  361. int ret;
  362. struct clk_get_value clk;
  363. __le16 le_clk_id = cpu_to_le16(clk_id);
  364. ret = scpi_send_message(SCPI_CMD_GET_CLOCK_VALUE, &le_clk_id,
  365. sizeof(le_clk_id), &clk, sizeof(clk));
  366. return ret ? ret : le32_to_cpu(clk.rate);
  367. }
  368. static int scpi_clk_set_val(u16 clk_id, unsigned long rate)
  369. {
  370. int stat;
  371. struct clk_set_value clk = {
  372. .id = cpu_to_le16(clk_id),
  373. .rate = cpu_to_le32(rate)
  374. };
  375. return scpi_send_message(SCPI_CMD_SET_CLOCK_VALUE, &clk, sizeof(clk),
  376. &stat, sizeof(stat));
  377. }
  378. static int scpi_dvfs_get_idx(u8 domain)
  379. {
  380. int ret;
  381. u8 dvfs_idx;
  382. ret = scpi_send_message(SCPI_CMD_GET_DVFS, &domain, sizeof(domain),
  383. &dvfs_idx, sizeof(dvfs_idx));
  384. return ret ? ret : dvfs_idx;
  385. }
  386. static int scpi_dvfs_set_idx(u8 domain, u8 index)
  387. {
  388. int stat;
  389. struct dvfs_set dvfs = {domain, index};
  390. return scpi_send_message(SCPI_CMD_SET_DVFS, &dvfs, sizeof(dvfs),
  391. &stat, sizeof(stat));
  392. }
  393. static int opp_cmp_func(const void *opp1, const void *opp2)
  394. {
  395. const struct scpi_opp *t1 = opp1, *t2 = opp2;
  396. return t1->freq - t2->freq;
  397. }
  398. static struct scpi_dvfs_info *scpi_dvfs_get_info(u8 domain)
  399. {
  400. struct scpi_dvfs_info *info;
  401. struct scpi_opp *opp;
  402. struct dvfs_info buf;
  403. int ret, i;
  404. if (domain >= MAX_DVFS_DOMAINS)
  405. return ERR_PTR(-EINVAL);
  406. if (scpi_info->dvfs[domain]) /* data already populated */
  407. return scpi_info->dvfs[domain];
  408. ret = scpi_send_message(SCPI_CMD_GET_DVFS_INFO, &domain, sizeof(domain),
  409. &buf, sizeof(buf));
  410. if (ret)
  411. return ERR_PTR(ret);
  412. info = kmalloc(sizeof(*info), GFP_KERNEL);
  413. if (!info)
  414. return ERR_PTR(-ENOMEM);
  415. info->count = DVFS_OPP_COUNT(buf.header);
  416. info->latency = DVFS_LATENCY(buf.header) * 1000; /* uS to nS */
  417. info->opps = kcalloc(info->count, sizeof(*opp), GFP_KERNEL);
  418. if (!info->opps) {
  419. kfree(info);
  420. return ERR_PTR(-ENOMEM);
  421. }
  422. for (i = 0, opp = info->opps; i < info->count; i++, opp++) {
  423. opp->freq = le32_to_cpu(buf.opps[i].freq);
  424. opp->m_volt = le32_to_cpu(buf.opps[i].m_volt);
  425. }
  426. sort(info->opps, info->count, sizeof(*opp), opp_cmp_func, NULL);
  427. scpi_info->dvfs[domain] = info;
  428. return info;
  429. }
  430. static int scpi_sensor_get_capability(u16 *sensors)
  431. {
  432. struct sensor_capabilities cap_buf;
  433. int ret;
  434. ret = scpi_send_message(SCPI_CMD_SENSOR_CAPABILITIES, NULL, 0, &cap_buf,
  435. sizeof(cap_buf));
  436. if (!ret)
  437. *sensors = le16_to_cpu(cap_buf.sensors);
  438. return ret;
  439. }
  440. static int scpi_sensor_get_info(u16 sensor_id, struct scpi_sensor_info *info)
  441. {
  442. __le16 id = cpu_to_le16(sensor_id);
  443. struct _scpi_sensor_info _info;
  444. int ret;
  445. ret = scpi_send_message(SCPI_CMD_SENSOR_INFO, &id, sizeof(id),
  446. &_info, sizeof(_info));
  447. if (!ret) {
  448. memcpy(info, &_info, sizeof(*info));
  449. info->sensor_id = le16_to_cpu(_info.sensor_id);
  450. }
  451. return ret;
  452. }
  453. static int scpi_sensor_get_value(u16 sensor, u64 *val)
  454. {
  455. __le16 id = cpu_to_le16(sensor);
  456. struct sensor_value buf;
  457. int ret;
  458. ret = scpi_send_message(SCPI_CMD_SENSOR_VALUE, &id, sizeof(id),
  459. &buf, sizeof(buf));
  460. if (!ret)
  461. *val = (u64)le32_to_cpu(buf.hi_val) << 32 |
  462. le32_to_cpu(buf.lo_val);
  463. return ret;
  464. }
  465. static int scpi_device_get_power_state(u16 dev_id)
  466. {
  467. int ret;
  468. u8 pstate;
  469. __le16 id = cpu_to_le16(dev_id);
  470. ret = scpi_send_message(SCPI_CMD_GET_DEVICE_PWR_STATE, &id,
  471. sizeof(id), &pstate, sizeof(pstate));
  472. return ret ? ret : pstate;
  473. }
  474. static int scpi_device_set_power_state(u16 dev_id, u8 pstate)
  475. {
  476. int stat;
  477. struct dev_pstate_set dev_set = {
  478. .dev_id = cpu_to_le16(dev_id),
  479. .pstate = pstate,
  480. };
  481. return scpi_send_message(SCPI_CMD_SET_DEVICE_PWR_STATE, &dev_set,
  482. sizeof(dev_set), &stat, sizeof(stat));
  483. }
  484. static struct scpi_ops scpi_ops = {
  485. .get_version = scpi_get_version,
  486. .clk_get_range = scpi_clk_get_range,
  487. .clk_get_val = scpi_clk_get_val,
  488. .clk_set_val = scpi_clk_set_val,
  489. .dvfs_get_idx = scpi_dvfs_get_idx,
  490. .dvfs_set_idx = scpi_dvfs_set_idx,
  491. .dvfs_get_info = scpi_dvfs_get_info,
  492. .sensor_get_capability = scpi_sensor_get_capability,
  493. .sensor_get_info = scpi_sensor_get_info,
  494. .sensor_get_value = scpi_sensor_get_value,
  495. .device_get_power_state = scpi_device_get_power_state,
  496. .device_set_power_state = scpi_device_set_power_state,
  497. };
  498. struct scpi_ops *get_scpi_ops(void)
  499. {
  500. return scpi_info ? scpi_info->scpi_ops : NULL;
  501. }
  502. EXPORT_SYMBOL_GPL(get_scpi_ops);
  503. static int scpi_init_versions(struct scpi_drvinfo *info)
  504. {
  505. int ret;
  506. struct scp_capabilities caps;
  507. ret = scpi_send_message(SCPI_CMD_SCPI_CAPABILITIES, NULL, 0,
  508. &caps, sizeof(caps));
  509. if (!ret) {
  510. info->protocol_version = le32_to_cpu(caps.protocol_version);
  511. info->firmware_version = le32_to_cpu(caps.platform_version);
  512. }
  513. return ret;
  514. }
  515. static ssize_t protocol_version_show(struct device *dev,
  516. struct device_attribute *attr, char *buf)
  517. {
  518. struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
  519. return sprintf(buf, "%d.%d\n",
  520. PROTOCOL_REV_MAJOR(scpi_info->protocol_version),
  521. PROTOCOL_REV_MINOR(scpi_info->protocol_version));
  522. }
  523. static DEVICE_ATTR_RO(protocol_version);
  524. static ssize_t firmware_version_show(struct device *dev,
  525. struct device_attribute *attr, char *buf)
  526. {
  527. struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
  528. return sprintf(buf, "%d.%d.%d\n",
  529. FW_REV_MAJOR(scpi_info->firmware_version),
  530. FW_REV_MINOR(scpi_info->firmware_version),
  531. FW_REV_PATCH(scpi_info->firmware_version));
  532. }
  533. static DEVICE_ATTR_RO(firmware_version);
  534. static struct attribute *versions_attrs[] = {
  535. &dev_attr_firmware_version.attr,
  536. &dev_attr_protocol_version.attr,
  537. NULL,
  538. };
  539. ATTRIBUTE_GROUPS(versions);
  540. static void
  541. scpi_free_channels(struct device *dev, struct scpi_chan *pchan, int count)
  542. {
  543. int i;
  544. for (i = 0; i < count && pchan->chan; i++, pchan++) {
  545. mbox_free_channel(pchan->chan);
  546. devm_kfree(dev, pchan->xfers);
  547. devm_iounmap(dev, pchan->rx_payload);
  548. }
  549. }
  550. static int scpi_remove(struct platform_device *pdev)
  551. {
  552. int i;
  553. struct device *dev = &pdev->dev;
  554. struct scpi_drvinfo *info = platform_get_drvdata(pdev);
  555. scpi_info = NULL; /* stop exporting SCPI ops through get_scpi_ops */
  556. of_platform_depopulate(dev);
  557. sysfs_remove_groups(&dev->kobj, versions_groups);
  558. scpi_free_channels(dev, info->channels, info->num_chans);
  559. platform_set_drvdata(pdev, NULL);
  560. for (i = 0; i < MAX_DVFS_DOMAINS && info->dvfs[i]; i++) {
  561. kfree(info->dvfs[i]->opps);
  562. kfree(info->dvfs[i]);
  563. }
  564. devm_kfree(dev, info->channels);
  565. devm_kfree(dev, info);
  566. return 0;
  567. }
  568. #define MAX_SCPI_XFERS 10
  569. static int scpi_alloc_xfer_list(struct device *dev, struct scpi_chan *ch)
  570. {
  571. int i;
  572. struct scpi_xfer *xfers;
  573. xfers = devm_kzalloc(dev, MAX_SCPI_XFERS * sizeof(*xfers), GFP_KERNEL);
  574. if (!xfers)
  575. return -ENOMEM;
  576. ch->xfers = xfers;
  577. for (i = 0; i < MAX_SCPI_XFERS; i++, xfers++)
  578. list_add_tail(&xfers->node, &ch->xfers_list);
  579. return 0;
  580. }
  581. static int scpi_probe(struct platform_device *pdev)
  582. {
  583. int count, idx, ret;
  584. struct resource res;
  585. struct scpi_chan *scpi_chan;
  586. struct device *dev = &pdev->dev;
  587. struct device_node *np = dev->of_node;
  588. scpi_info = devm_kzalloc(dev, sizeof(*scpi_info), GFP_KERNEL);
  589. if (!scpi_info)
  590. return -ENOMEM;
  591. count = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
  592. if (count < 0) {
  593. dev_err(dev, "no mboxes property in '%s'\n", np->full_name);
  594. return -ENODEV;
  595. }
  596. scpi_chan = devm_kcalloc(dev, count, sizeof(*scpi_chan), GFP_KERNEL);
  597. if (!scpi_chan)
  598. return -ENOMEM;
  599. for (idx = 0; idx < count; idx++) {
  600. resource_size_t size;
  601. struct scpi_chan *pchan = scpi_chan + idx;
  602. struct mbox_client *cl = &pchan->cl;
  603. struct device_node *shmem = of_parse_phandle(np, "shmem", idx);
  604. ret = of_address_to_resource(shmem, 0, &res);
  605. of_node_put(shmem);
  606. if (ret) {
  607. dev_err(dev, "failed to get SCPI payload mem resource\n");
  608. goto err;
  609. }
  610. size = resource_size(&res);
  611. pchan->rx_payload = devm_ioremap(dev, res.start, size);
  612. if (!pchan->rx_payload) {
  613. dev_err(dev, "failed to ioremap SCPI payload\n");
  614. ret = -EADDRNOTAVAIL;
  615. goto err;
  616. }
  617. pchan->tx_payload = pchan->rx_payload + (size >> 1);
  618. cl->dev = dev;
  619. cl->rx_callback = scpi_handle_remote_msg;
  620. cl->tx_prepare = scpi_tx_prepare;
  621. cl->tx_block = true;
  622. cl->tx_tout = 20;
  623. cl->knows_txdone = false; /* controller can't ack */
  624. INIT_LIST_HEAD(&pchan->rx_pending);
  625. INIT_LIST_HEAD(&pchan->xfers_list);
  626. spin_lock_init(&pchan->rx_lock);
  627. mutex_init(&pchan->xfers_lock);
  628. ret = scpi_alloc_xfer_list(dev, pchan);
  629. if (!ret) {
  630. pchan->chan = mbox_request_channel(cl, idx);
  631. if (!IS_ERR(pchan->chan))
  632. continue;
  633. ret = PTR_ERR(pchan->chan);
  634. if (ret != -EPROBE_DEFER)
  635. dev_err(dev, "failed to get channel%d err %d\n",
  636. idx, ret);
  637. }
  638. err:
  639. scpi_free_channels(dev, scpi_chan, idx);
  640. scpi_info = NULL;
  641. return ret;
  642. }
  643. scpi_info->channels = scpi_chan;
  644. scpi_info->num_chans = count;
  645. platform_set_drvdata(pdev, scpi_info);
  646. ret = scpi_init_versions(scpi_info);
  647. if (ret) {
  648. dev_err(dev, "incorrect or no SCP firmware found\n");
  649. scpi_remove(pdev);
  650. return ret;
  651. }
  652. _dev_info(dev, "SCP Protocol %d.%d Firmware %d.%d.%d version\n",
  653. PROTOCOL_REV_MAJOR(scpi_info->protocol_version),
  654. PROTOCOL_REV_MINOR(scpi_info->protocol_version),
  655. FW_REV_MAJOR(scpi_info->firmware_version),
  656. FW_REV_MINOR(scpi_info->firmware_version),
  657. FW_REV_PATCH(scpi_info->firmware_version));
  658. scpi_info->scpi_ops = &scpi_ops;
  659. ret = sysfs_create_groups(&dev->kobj, versions_groups);
  660. if (ret)
  661. dev_err(dev, "unable to create sysfs version group\n");
  662. return of_platform_populate(dev->of_node, NULL, NULL, dev);
  663. }
  664. static const struct of_device_id scpi_of_match[] = {
  665. {.compatible = "arm,scpi"},
  666. {},
  667. };
  668. MODULE_DEVICE_TABLE(of, scpi_of_match);
  669. static struct platform_driver scpi_driver = {
  670. .driver = {
  671. .name = "scpi_protocol",
  672. .of_match_table = scpi_of_match,
  673. },
  674. .probe = scpi_probe,
  675. .remove = scpi_remove,
  676. };
  677. module_platform_driver(scpi_driver);
  678. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  679. MODULE_DESCRIPTION("ARM SCPI mailbox protocol driver");
  680. MODULE_LICENSE("GPL v2");