arm_scpi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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_get {
  193. u8 index;
  194. } __packed;
  195. struct dvfs_set {
  196. u8 domain;
  197. u8 index;
  198. } __packed;
  199. struct sensor_capabilities {
  200. __le16 sensors;
  201. } __packed;
  202. struct _scpi_sensor_info {
  203. __le16 sensor_id;
  204. u8 class;
  205. u8 trigger_type;
  206. char name[20];
  207. };
  208. struct sensor_value {
  209. __le32 lo_val;
  210. __le32 hi_val;
  211. } __packed;
  212. static struct scpi_drvinfo *scpi_info;
  213. static int scpi_linux_errmap[SCPI_ERR_MAX] = {
  214. /* better than switch case as long as return value is continuous */
  215. 0, /* SCPI_SUCCESS */
  216. -EINVAL, /* SCPI_ERR_PARAM */
  217. -ENOEXEC, /* SCPI_ERR_ALIGN */
  218. -EMSGSIZE, /* SCPI_ERR_SIZE */
  219. -EINVAL, /* SCPI_ERR_HANDLER */
  220. -EACCES, /* SCPI_ERR_ACCESS */
  221. -ERANGE, /* SCPI_ERR_RANGE */
  222. -ETIMEDOUT, /* SCPI_ERR_TIMEOUT */
  223. -ENOMEM, /* SCPI_ERR_NOMEM */
  224. -EINVAL, /* SCPI_ERR_PWRSTATE */
  225. -EOPNOTSUPP, /* SCPI_ERR_SUPPORT */
  226. -EIO, /* SCPI_ERR_DEVICE */
  227. -EBUSY, /* SCPI_ERR_BUSY */
  228. };
  229. static inline int scpi_to_linux_errno(int errno)
  230. {
  231. if (errno >= SCPI_SUCCESS && errno < SCPI_ERR_MAX)
  232. return scpi_linux_errmap[errno];
  233. return -EIO;
  234. }
  235. static void scpi_process_cmd(struct scpi_chan *ch, u32 cmd)
  236. {
  237. unsigned long flags;
  238. struct scpi_xfer *t, *match = NULL;
  239. spin_lock_irqsave(&ch->rx_lock, flags);
  240. if (list_empty(&ch->rx_pending)) {
  241. spin_unlock_irqrestore(&ch->rx_lock, flags);
  242. return;
  243. }
  244. list_for_each_entry(t, &ch->rx_pending, node)
  245. if (CMD_XTRACT_UNIQ(t->cmd) == CMD_XTRACT_UNIQ(cmd)) {
  246. list_del(&t->node);
  247. match = t;
  248. break;
  249. }
  250. /* check if wait_for_completion is in progress or timed-out */
  251. if (match && !completion_done(&match->done)) {
  252. struct scpi_shared_mem *mem = ch->rx_payload;
  253. unsigned int len = min(match->rx_len, CMD_SIZE(cmd));
  254. match->status = le32_to_cpu(mem->status);
  255. memcpy_fromio(match->rx_buf, mem->payload, len);
  256. if (match->rx_len > len)
  257. memset(match->rx_buf + len, 0, match->rx_len - len);
  258. complete(&match->done);
  259. }
  260. spin_unlock_irqrestore(&ch->rx_lock, flags);
  261. }
  262. static void scpi_handle_remote_msg(struct mbox_client *c, void *msg)
  263. {
  264. struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
  265. struct scpi_shared_mem *mem = ch->rx_payload;
  266. u32 cmd = le32_to_cpu(mem->command);
  267. scpi_process_cmd(ch, cmd);
  268. }
  269. static void scpi_tx_prepare(struct mbox_client *c, void *msg)
  270. {
  271. unsigned long flags;
  272. struct scpi_xfer *t = msg;
  273. struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
  274. struct scpi_shared_mem *mem = (struct scpi_shared_mem *)ch->tx_payload;
  275. if (t->tx_buf)
  276. memcpy_toio(mem->payload, t->tx_buf, t->tx_len);
  277. if (t->rx_buf) {
  278. if (!(++ch->token))
  279. ++ch->token;
  280. ADD_SCPI_TOKEN(t->cmd, ch->token);
  281. spin_lock_irqsave(&ch->rx_lock, flags);
  282. list_add_tail(&t->node, &ch->rx_pending);
  283. spin_unlock_irqrestore(&ch->rx_lock, flags);
  284. }
  285. mem->command = cpu_to_le32(t->cmd);
  286. }
  287. static struct scpi_xfer *get_scpi_xfer(struct scpi_chan *ch)
  288. {
  289. struct scpi_xfer *t;
  290. mutex_lock(&ch->xfers_lock);
  291. if (list_empty(&ch->xfers_list)) {
  292. mutex_unlock(&ch->xfers_lock);
  293. return NULL;
  294. }
  295. t = list_first_entry(&ch->xfers_list, struct scpi_xfer, node);
  296. list_del(&t->node);
  297. mutex_unlock(&ch->xfers_lock);
  298. return t;
  299. }
  300. static void put_scpi_xfer(struct scpi_xfer *t, struct scpi_chan *ch)
  301. {
  302. mutex_lock(&ch->xfers_lock);
  303. list_add_tail(&t->node, &ch->xfers_list);
  304. mutex_unlock(&ch->xfers_lock);
  305. }
  306. static int scpi_send_message(u8 cmd, void *tx_buf, unsigned int tx_len,
  307. void *rx_buf, unsigned int rx_len)
  308. {
  309. int ret;
  310. u8 chan;
  311. struct scpi_xfer *msg;
  312. struct scpi_chan *scpi_chan;
  313. chan = atomic_inc_return(&scpi_info->next_chan) % scpi_info->num_chans;
  314. scpi_chan = scpi_info->channels + chan;
  315. msg = get_scpi_xfer(scpi_chan);
  316. if (!msg)
  317. return -ENOMEM;
  318. msg->slot = BIT(SCPI_SLOT);
  319. msg->cmd = PACK_SCPI_CMD(cmd, tx_len);
  320. msg->tx_buf = tx_buf;
  321. msg->tx_len = tx_len;
  322. msg->rx_buf = rx_buf;
  323. msg->rx_len = rx_len;
  324. init_completion(&msg->done);
  325. ret = mbox_send_message(scpi_chan->chan, msg);
  326. if (ret < 0 || !rx_buf)
  327. goto out;
  328. if (!wait_for_completion_timeout(&msg->done, MAX_RX_TIMEOUT))
  329. ret = -ETIMEDOUT;
  330. else
  331. /* first status word */
  332. ret = msg->status;
  333. out:
  334. if (ret < 0 && rx_buf) /* remove entry from the list if timed-out */
  335. scpi_process_cmd(scpi_chan, msg->cmd);
  336. put_scpi_xfer(msg, scpi_chan);
  337. /* SCPI error codes > 0, translate them to Linux scale*/
  338. return ret > 0 ? scpi_to_linux_errno(ret) : ret;
  339. }
  340. static u32 scpi_get_version(void)
  341. {
  342. return scpi_info->protocol_version;
  343. }
  344. static int
  345. scpi_clk_get_range(u16 clk_id, unsigned long *min, unsigned long *max)
  346. {
  347. int ret;
  348. struct clk_get_info clk;
  349. __le16 le_clk_id = cpu_to_le16(clk_id);
  350. ret = scpi_send_message(SCPI_CMD_GET_CLOCK_INFO, &le_clk_id,
  351. sizeof(le_clk_id), &clk, sizeof(clk));
  352. if (!ret) {
  353. *min = le32_to_cpu(clk.min_rate);
  354. *max = le32_to_cpu(clk.max_rate);
  355. }
  356. return ret;
  357. }
  358. static unsigned long scpi_clk_get_val(u16 clk_id)
  359. {
  360. int ret;
  361. struct clk_get_value clk;
  362. __le16 le_clk_id = cpu_to_le16(clk_id);
  363. ret = scpi_send_message(SCPI_CMD_GET_CLOCK_VALUE, &le_clk_id,
  364. sizeof(le_clk_id), &clk, sizeof(clk));
  365. return ret ? ret : le32_to_cpu(clk.rate);
  366. }
  367. static int scpi_clk_set_val(u16 clk_id, unsigned long rate)
  368. {
  369. int stat;
  370. struct clk_set_value clk = {
  371. .id = cpu_to_le16(clk_id),
  372. .rate = cpu_to_le32(rate)
  373. };
  374. return scpi_send_message(SCPI_CMD_SET_CLOCK_VALUE, &clk, sizeof(clk),
  375. &stat, sizeof(stat));
  376. }
  377. static int scpi_dvfs_get_idx(u8 domain)
  378. {
  379. int ret;
  380. struct dvfs_get dvfs;
  381. ret = scpi_send_message(SCPI_CMD_GET_DVFS, &domain, sizeof(domain),
  382. &dvfs, sizeof(dvfs));
  383. return ret ? ret : dvfs.index;
  384. }
  385. static int scpi_dvfs_set_idx(u8 domain, u8 index)
  386. {
  387. int stat;
  388. struct dvfs_set dvfs = {domain, index};
  389. return scpi_send_message(SCPI_CMD_SET_DVFS, &dvfs, sizeof(dvfs),
  390. &stat, sizeof(stat));
  391. }
  392. static int opp_cmp_func(const void *opp1, const void *opp2)
  393. {
  394. const struct scpi_opp *t1 = opp1, *t2 = opp2;
  395. return t1->freq - t2->freq;
  396. }
  397. static struct scpi_dvfs_info *scpi_dvfs_get_info(u8 domain)
  398. {
  399. struct scpi_dvfs_info *info;
  400. struct scpi_opp *opp;
  401. struct dvfs_info buf;
  402. int ret, i;
  403. if (domain >= MAX_DVFS_DOMAINS)
  404. return ERR_PTR(-EINVAL);
  405. if (scpi_info->dvfs[domain]) /* data already populated */
  406. return scpi_info->dvfs[domain];
  407. ret = scpi_send_message(SCPI_CMD_GET_DVFS_INFO, &domain, sizeof(domain),
  408. &buf, sizeof(buf));
  409. if (ret)
  410. return ERR_PTR(ret);
  411. info = kmalloc(sizeof(*info), GFP_KERNEL);
  412. if (!info)
  413. return ERR_PTR(-ENOMEM);
  414. info->count = DVFS_OPP_COUNT(buf.header);
  415. info->latency = DVFS_LATENCY(buf.header) * 1000; /* uS to nS */
  416. info->opps = kcalloc(info->count, sizeof(*opp), GFP_KERNEL);
  417. if (!info->opps) {
  418. kfree(info);
  419. return ERR_PTR(-ENOMEM);
  420. }
  421. for (i = 0, opp = info->opps; i < info->count; i++, opp++) {
  422. opp->freq = le32_to_cpu(buf.opps[i].freq);
  423. opp->m_volt = le32_to_cpu(buf.opps[i].m_volt);
  424. }
  425. sort(info->opps, info->count, sizeof(*opp), opp_cmp_func, NULL);
  426. scpi_info->dvfs[domain] = info;
  427. return info;
  428. }
  429. static int scpi_sensor_get_capability(u16 *sensors)
  430. {
  431. struct sensor_capabilities cap_buf;
  432. int ret;
  433. ret = scpi_send_message(SCPI_CMD_SENSOR_CAPABILITIES, NULL, 0, &cap_buf,
  434. sizeof(cap_buf));
  435. if (!ret)
  436. *sensors = le16_to_cpu(cap_buf.sensors);
  437. return ret;
  438. }
  439. static int scpi_sensor_get_info(u16 sensor_id, struct scpi_sensor_info *info)
  440. {
  441. __le16 id = cpu_to_le16(sensor_id);
  442. struct _scpi_sensor_info _info;
  443. int ret;
  444. ret = scpi_send_message(SCPI_CMD_SENSOR_INFO, &id, sizeof(id),
  445. &_info, sizeof(_info));
  446. if (!ret) {
  447. memcpy(info, &_info, sizeof(*info));
  448. info->sensor_id = le16_to_cpu(_info.sensor_id);
  449. }
  450. return ret;
  451. }
  452. int scpi_sensor_get_value(u16 sensor, u64 *val)
  453. {
  454. __le16 id = cpu_to_le16(sensor);
  455. struct sensor_value buf;
  456. int ret;
  457. ret = scpi_send_message(SCPI_CMD_SENSOR_VALUE, &id, sizeof(id),
  458. &buf, sizeof(buf));
  459. if (!ret)
  460. *val = (u64)le32_to_cpu(buf.hi_val) << 32 |
  461. le32_to_cpu(buf.lo_val);
  462. return ret;
  463. }
  464. static struct scpi_ops scpi_ops = {
  465. .get_version = scpi_get_version,
  466. .clk_get_range = scpi_clk_get_range,
  467. .clk_get_val = scpi_clk_get_val,
  468. .clk_set_val = scpi_clk_set_val,
  469. .dvfs_get_idx = scpi_dvfs_get_idx,
  470. .dvfs_set_idx = scpi_dvfs_set_idx,
  471. .dvfs_get_info = scpi_dvfs_get_info,
  472. .sensor_get_capability = scpi_sensor_get_capability,
  473. .sensor_get_info = scpi_sensor_get_info,
  474. .sensor_get_value = scpi_sensor_get_value,
  475. };
  476. struct scpi_ops *get_scpi_ops(void)
  477. {
  478. return scpi_info ? scpi_info->scpi_ops : NULL;
  479. }
  480. EXPORT_SYMBOL_GPL(get_scpi_ops);
  481. static int scpi_init_versions(struct scpi_drvinfo *info)
  482. {
  483. int ret;
  484. struct scp_capabilities caps;
  485. ret = scpi_send_message(SCPI_CMD_SCPI_CAPABILITIES, NULL, 0,
  486. &caps, sizeof(caps));
  487. if (!ret) {
  488. info->protocol_version = le32_to_cpu(caps.protocol_version);
  489. info->firmware_version = le32_to_cpu(caps.platform_version);
  490. }
  491. return ret;
  492. }
  493. static ssize_t protocol_version_show(struct device *dev,
  494. struct device_attribute *attr, char *buf)
  495. {
  496. struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
  497. return sprintf(buf, "%d.%d\n",
  498. PROTOCOL_REV_MAJOR(scpi_info->protocol_version),
  499. PROTOCOL_REV_MINOR(scpi_info->protocol_version));
  500. }
  501. static DEVICE_ATTR_RO(protocol_version);
  502. static ssize_t firmware_version_show(struct device *dev,
  503. struct device_attribute *attr, char *buf)
  504. {
  505. struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
  506. return sprintf(buf, "%d.%d.%d\n",
  507. FW_REV_MAJOR(scpi_info->firmware_version),
  508. FW_REV_MINOR(scpi_info->firmware_version),
  509. FW_REV_PATCH(scpi_info->firmware_version));
  510. }
  511. static DEVICE_ATTR_RO(firmware_version);
  512. static struct attribute *versions_attrs[] = {
  513. &dev_attr_firmware_version.attr,
  514. &dev_attr_protocol_version.attr,
  515. NULL,
  516. };
  517. ATTRIBUTE_GROUPS(versions);
  518. static void
  519. scpi_free_channels(struct device *dev, struct scpi_chan *pchan, int count)
  520. {
  521. int i;
  522. for (i = 0; i < count && pchan->chan; i++, pchan++) {
  523. mbox_free_channel(pchan->chan);
  524. devm_kfree(dev, pchan->xfers);
  525. devm_iounmap(dev, pchan->rx_payload);
  526. }
  527. }
  528. static int scpi_remove(struct platform_device *pdev)
  529. {
  530. int i;
  531. struct device *dev = &pdev->dev;
  532. struct scpi_drvinfo *info = platform_get_drvdata(pdev);
  533. scpi_info = NULL; /* stop exporting SCPI ops through get_scpi_ops */
  534. of_platform_depopulate(dev);
  535. sysfs_remove_groups(&dev->kobj, versions_groups);
  536. scpi_free_channels(dev, info->channels, info->num_chans);
  537. platform_set_drvdata(pdev, NULL);
  538. for (i = 0; i < MAX_DVFS_DOMAINS && info->dvfs[i]; i++) {
  539. kfree(info->dvfs[i]->opps);
  540. kfree(info->dvfs[i]);
  541. }
  542. devm_kfree(dev, info->channels);
  543. devm_kfree(dev, info);
  544. return 0;
  545. }
  546. #define MAX_SCPI_XFERS 10
  547. static int scpi_alloc_xfer_list(struct device *dev, struct scpi_chan *ch)
  548. {
  549. int i;
  550. struct scpi_xfer *xfers;
  551. xfers = devm_kzalloc(dev, MAX_SCPI_XFERS * sizeof(*xfers), GFP_KERNEL);
  552. if (!xfers)
  553. return -ENOMEM;
  554. ch->xfers = xfers;
  555. for (i = 0; i < MAX_SCPI_XFERS; i++, xfers++)
  556. list_add_tail(&xfers->node, &ch->xfers_list);
  557. return 0;
  558. }
  559. static int scpi_probe(struct platform_device *pdev)
  560. {
  561. int count, idx, ret;
  562. struct resource res;
  563. struct scpi_chan *scpi_chan;
  564. struct device *dev = &pdev->dev;
  565. struct device_node *np = dev->of_node;
  566. scpi_info = devm_kzalloc(dev, sizeof(*scpi_info), GFP_KERNEL);
  567. if (!scpi_info)
  568. return -ENOMEM;
  569. count = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
  570. if (count < 0) {
  571. dev_err(dev, "no mboxes property in '%s'\n", np->full_name);
  572. return -ENODEV;
  573. }
  574. scpi_chan = devm_kcalloc(dev, count, sizeof(*scpi_chan), GFP_KERNEL);
  575. if (!scpi_chan)
  576. return -ENOMEM;
  577. for (idx = 0; idx < count; idx++) {
  578. resource_size_t size;
  579. struct scpi_chan *pchan = scpi_chan + idx;
  580. struct mbox_client *cl = &pchan->cl;
  581. struct device_node *shmem = of_parse_phandle(np, "shmem", idx);
  582. if (of_address_to_resource(shmem, 0, &res)) {
  583. dev_err(dev, "failed to get SCPI payload mem resource\n");
  584. ret = -EINVAL;
  585. goto err;
  586. }
  587. size = resource_size(&res);
  588. pchan->rx_payload = devm_ioremap(dev, res.start, size);
  589. if (!pchan->rx_payload) {
  590. dev_err(dev, "failed to ioremap SCPI payload\n");
  591. ret = -EADDRNOTAVAIL;
  592. goto err;
  593. }
  594. pchan->tx_payload = pchan->rx_payload + (size >> 1);
  595. cl->dev = dev;
  596. cl->rx_callback = scpi_handle_remote_msg;
  597. cl->tx_prepare = scpi_tx_prepare;
  598. cl->tx_block = true;
  599. cl->tx_tout = 20;
  600. cl->knows_txdone = false; /* controller can't ack */
  601. INIT_LIST_HEAD(&pchan->rx_pending);
  602. INIT_LIST_HEAD(&pchan->xfers_list);
  603. spin_lock_init(&pchan->rx_lock);
  604. mutex_init(&pchan->xfers_lock);
  605. ret = scpi_alloc_xfer_list(dev, pchan);
  606. if (!ret) {
  607. pchan->chan = mbox_request_channel(cl, idx);
  608. if (!IS_ERR(pchan->chan))
  609. continue;
  610. ret = PTR_ERR(pchan->chan);
  611. if (ret != -EPROBE_DEFER)
  612. dev_err(dev, "failed to get channel%d err %d\n",
  613. idx, ret);
  614. }
  615. err:
  616. scpi_free_channels(dev, scpi_chan, idx);
  617. scpi_info = NULL;
  618. return ret;
  619. }
  620. scpi_info->channels = scpi_chan;
  621. scpi_info->num_chans = count;
  622. platform_set_drvdata(pdev, scpi_info);
  623. ret = scpi_init_versions(scpi_info);
  624. if (ret) {
  625. dev_err(dev, "incorrect or no SCP firmware found\n");
  626. scpi_remove(pdev);
  627. return ret;
  628. }
  629. _dev_info(dev, "SCP Protocol %d.%d Firmware %d.%d.%d version\n",
  630. PROTOCOL_REV_MAJOR(scpi_info->protocol_version),
  631. PROTOCOL_REV_MINOR(scpi_info->protocol_version),
  632. FW_REV_MAJOR(scpi_info->firmware_version),
  633. FW_REV_MINOR(scpi_info->firmware_version),
  634. FW_REV_PATCH(scpi_info->firmware_version));
  635. scpi_info->scpi_ops = &scpi_ops;
  636. ret = sysfs_create_groups(&dev->kobj, versions_groups);
  637. if (ret)
  638. dev_err(dev, "unable to create sysfs version group\n");
  639. return of_platform_populate(dev->of_node, NULL, NULL, dev);
  640. }
  641. static const struct of_device_id scpi_of_match[] = {
  642. {.compatible = "arm,scpi"},
  643. {},
  644. };
  645. MODULE_DEVICE_TABLE(of, scpi_of_match);
  646. static struct platform_driver scpi_driver = {
  647. .driver = {
  648. .name = "scpi_protocol",
  649. .of_match_table = scpi_of_match,
  650. },
  651. .probe = scpi_probe,
  652. .remove = scpi_remove,
  653. };
  654. module_platform_driver(scpi_driver);
  655. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  656. MODULE_DESCRIPTION("ARM SCPI mailbox protocol driver");
  657. MODULE_LICENSE("GPL v2");