arm_scpi.c 20 KB

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