bt-bmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2015-2016, IBM Corporation.
  4. */
  5. #include <linux/atomic.h>
  6. #include <linux/bt-bmc.h>
  7. #include <linux/errno.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/io.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/miscdevice.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/poll.h>
  16. #include <linux/regmap.h>
  17. #include <linux/sched.h>
  18. #include <linux/timer.h>
  19. /*
  20. * This is a BMC device used to communicate to the host
  21. */
  22. #define DEVICE_NAME "ipmi-bt-host"
  23. #define BT_IO_BASE 0xe4
  24. #define BT_IRQ 10
  25. #define BT_CR0 0x0
  26. #define BT_CR0_IO_BASE 16
  27. #define BT_CR0_IRQ 12
  28. #define BT_CR0_EN_CLR_SLV_RDP 0x8
  29. #define BT_CR0_EN_CLR_SLV_WRP 0x4
  30. #define BT_CR0_ENABLE_IBT 0x1
  31. #define BT_CR1 0x4
  32. #define BT_CR1_IRQ_H2B 0x01
  33. #define BT_CR1_IRQ_HBUSY 0x40
  34. #define BT_CR2 0x8
  35. #define BT_CR2_IRQ_H2B 0x01
  36. #define BT_CR2_IRQ_HBUSY 0x40
  37. #define BT_CR3 0xc
  38. #define BT_CTRL 0x10
  39. #define BT_CTRL_B_BUSY 0x80
  40. #define BT_CTRL_H_BUSY 0x40
  41. #define BT_CTRL_OEM0 0x20
  42. #define BT_CTRL_SMS_ATN 0x10
  43. #define BT_CTRL_B2H_ATN 0x08
  44. #define BT_CTRL_H2B_ATN 0x04
  45. #define BT_CTRL_CLR_RD_PTR 0x02
  46. #define BT_CTRL_CLR_WR_PTR 0x01
  47. #define BT_BMC2HOST 0x14
  48. #define BT_INTMASK 0x18
  49. #define BT_INTMASK_B2H_IRQEN 0x01
  50. #define BT_INTMASK_B2H_IRQ 0x02
  51. #define BT_INTMASK_BMC_HWRST 0x80
  52. #define BT_BMC_BUFFER_SIZE 256
  53. struct bt_bmc {
  54. struct device dev;
  55. struct miscdevice miscdev;
  56. struct regmap *map;
  57. int offset;
  58. int irq;
  59. wait_queue_head_t queue;
  60. struct timer_list poll_timer;
  61. struct mutex mutex;
  62. };
  63. static atomic_t open_count = ATOMIC_INIT(0);
  64. static const struct regmap_config bt_regmap_cfg = {
  65. .reg_bits = 32,
  66. .val_bits = 32,
  67. .reg_stride = 4,
  68. };
  69. static u8 bt_inb(struct bt_bmc *bt_bmc, int reg)
  70. {
  71. uint32_t val = 0;
  72. int rc;
  73. rc = regmap_read(bt_bmc->map, bt_bmc->offset + reg, &val);
  74. WARN(rc != 0, "regmap_read() failed: %d\n", rc);
  75. return rc == 0 ? (u8) val : 0;
  76. }
  77. static void bt_outb(struct bt_bmc *bt_bmc, u8 data, int reg)
  78. {
  79. int rc;
  80. rc = regmap_write(bt_bmc->map, bt_bmc->offset + reg, data);
  81. WARN(rc != 0, "regmap_write() failed: %d\n", rc);
  82. }
  83. static void clr_rd_ptr(struct bt_bmc *bt_bmc)
  84. {
  85. bt_outb(bt_bmc, BT_CTRL_CLR_RD_PTR, BT_CTRL);
  86. }
  87. static void clr_wr_ptr(struct bt_bmc *bt_bmc)
  88. {
  89. bt_outb(bt_bmc, BT_CTRL_CLR_WR_PTR, BT_CTRL);
  90. }
  91. static void clr_h2b_atn(struct bt_bmc *bt_bmc)
  92. {
  93. bt_outb(bt_bmc, BT_CTRL_H2B_ATN, BT_CTRL);
  94. }
  95. static void set_b_busy(struct bt_bmc *bt_bmc)
  96. {
  97. if (!(bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_B_BUSY))
  98. bt_outb(bt_bmc, BT_CTRL_B_BUSY, BT_CTRL);
  99. }
  100. static void clr_b_busy(struct bt_bmc *bt_bmc)
  101. {
  102. if (bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_B_BUSY)
  103. bt_outb(bt_bmc, BT_CTRL_B_BUSY, BT_CTRL);
  104. }
  105. static void set_b2h_atn(struct bt_bmc *bt_bmc)
  106. {
  107. bt_outb(bt_bmc, BT_CTRL_B2H_ATN, BT_CTRL);
  108. }
  109. static u8 bt_read(struct bt_bmc *bt_bmc)
  110. {
  111. return bt_inb(bt_bmc, BT_BMC2HOST);
  112. }
  113. static ssize_t bt_readn(struct bt_bmc *bt_bmc, u8 *buf, size_t n)
  114. {
  115. int i;
  116. for (i = 0; i < n; i++)
  117. buf[i] = bt_read(bt_bmc);
  118. return n;
  119. }
  120. static void bt_write(struct bt_bmc *bt_bmc, u8 c)
  121. {
  122. bt_outb(bt_bmc, c, BT_BMC2HOST);
  123. }
  124. static ssize_t bt_writen(struct bt_bmc *bt_bmc, u8 *buf, size_t n)
  125. {
  126. int i;
  127. for (i = 0; i < n; i++)
  128. bt_write(bt_bmc, buf[i]);
  129. return n;
  130. }
  131. static void set_sms_atn(struct bt_bmc *bt_bmc)
  132. {
  133. bt_outb(bt_bmc, BT_CTRL_SMS_ATN, BT_CTRL);
  134. }
  135. static struct bt_bmc *file_bt_bmc(struct file *file)
  136. {
  137. return container_of(file->private_data, struct bt_bmc, miscdev);
  138. }
  139. static int bt_bmc_open(struct inode *inode, struct file *file)
  140. {
  141. struct bt_bmc *bt_bmc = file_bt_bmc(file);
  142. if (atomic_inc_return(&open_count) == 1) {
  143. clr_b_busy(bt_bmc);
  144. return 0;
  145. }
  146. atomic_dec(&open_count);
  147. return -EBUSY;
  148. }
  149. /*
  150. * The BT (Block Transfer) interface means that entire messages are
  151. * buffered by the host before a notification is sent to the BMC that
  152. * there is data to be read. The first byte is the length and the
  153. * message data follows. The read operation just tries to capture the
  154. * whole before returning it to userspace.
  155. *
  156. * BT Message format :
  157. *
  158. * Byte 1 Byte 2 Byte 3 Byte 4 Byte 5:N
  159. * Length NetFn/LUN Seq Cmd Data
  160. *
  161. */
  162. static ssize_t bt_bmc_read(struct file *file, char __user *buf,
  163. size_t count, loff_t *ppos)
  164. {
  165. struct bt_bmc *bt_bmc = file_bt_bmc(file);
  166. u8 len;
  167. int len_byte = 1;
  168. u8 kbuffer[BT_BMC_BUFFER_SIZE];
  169. ssize_t ret = 0;
  170. ssize_t nread;
  171. WARN_ON(*ppos);
  172. if (wait_event_interruptible(bt_bmc->queue,
  173. bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))
  174. return -ERESTARTSYS;
  175. mutex_lock(&bt_bmc->mutex);
  176. if (unlikely(!(bt_inb(bt_bmc, BT_CTRL) & BT_CTRL_H2B_ATN))) {
  177. ret = -EIO;
  178. goto out_unlock;
  179. }
  180. set_b_busy(bt_bmc);
  181. clr_h2b_atn(bt_bmc);
  182. clr_rd_ptr(bt_bmc);
  183. /*
  184. * The BT frames start with the message length, which does not
  185. * include the length byte.
  186. */
  187. kbuffer[0] = bt_read(bt_bmc);
  188. len = kbuffer[0];
  189. /* We pass the length back to userspace as well */
  190. if (len + 1 > count)
  191. len = count - 1;
  192. while (len) {
  193. nread = min_t(ssize_t, len, sizeof(kbuffer) - len_byte);
  194. bt_readn(bt_bmc, kbuffer + len_byte, nread);
  195. if (copy_to_user(buf, kbuffer, nread + len_byte)) {
  196. ret = -EFAULT;
  197. break;
  198. }
  199. len -= nread;
  200. buf += nread + len_byte;
  201. ret += nread + len_byte;
  202. len_byte = 0;
  203. }
  204. clr_b_busy(bt_bmc);
  205. out_unlock:
  206. mutex_unlock(&bt_bmc->mutex);
  207. return ret;
  208. }
  209. /*
  210. * BT Message response format :
  211. *
  212. * Byte 1 Byte 2 Byte 3 Byte 4 Byte 5 Byte 6:N
  213. * Length NetFn/LUN Seq Cmd Code Data
  214. */
  215. static ssize_t bt_bmc_write(struct file *file, const char __user *buf,
  216. size_t count, loff_t *ppos)
  217. {
  218. struct bt_bmc *bt_bmc = file_bt_bmc(file);
  219. u8 kbuffer[BT_BMC_BUFFER_SIZE];
  220. ssize_t ret = 0;
  221. ssize_t nwritten;
  222. /*
  223. * send a minimum response size
  224. */
  225. if (count < 5)
  226. return -EINVAL;
  227. WARN_ON(*ppos);
  228. /*
  229. * There's no interrupt for clearing bmc busy so we have to
  230. * poll
  231. */
  232. if (wait_event_interruptible(bt_bmc->queue,
  233. !(bt_inb(bt_bmc, BT_CTRL) &
  234. (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))))
  235. return -ERESTARTSYS;
  236. mutex_lock(&bt_bmc->mutex);
  237. if (unlikely(bt_inb(bt_bmc, BT_CTRL) &
  238. (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN))) {
  239. ret = -EIO;
  240. goto out_unlock;
  241. }
  242. clr_wr_ptr(bt_bmc);
  243. while (count) {
  244. nwritten = min_t(ssize_t, count, sizeof(kbuffer));
  245. if (copy_from_user(&kbuffer, buf, nwritten)) {
  246. ret = -EFAULT;
  247. break;
  248. }
  249. bt_writen(bt_bmc, kbuffer, nwritten);
  250. count -= nwritten;
  251. buf += nwritten;
  252. ret += nwritten;
  253. }
  254. set_b2h_atn(bt_bmc);
  255. out_unlock:
  256. mutex_unlock(&bt_bmc->mutex);
  257. return ret;
  258. }
  259. static long bt_bmc_ioctl(struct file *file, unsigned int cmd,
  260. unsigned long param)
  261. {
  262. struct bt_bmc *bt_bmc = file_bt_bmc(file);
  263. switch (cmd) {
  264. case BT_BMC_IOCTL_SMS_ATN:
  265. set_sms_atn(bt_bmc);
  266. return 0;
  267. }
  268. return -EINVAL;
  269. }
  270. static int bt_bmc_release(struct inode *inode, struct file *file)
  271. {
  272. struct bt_bmc *bt_bmc = file_bt_bmc(file);
  273. atomic_dec(&open_count);
  274. set_b_busy(bt_bmc);
  275. return 0;
  276. }
  277. static __poll_t bt_bmc_poll(struct file *file, poll_table *wait)
  278. {
  279. struct bt_bmc *bt_bmc = file_bt_bmc(file);
  280. __poll_t mask = 0;
  281. u8 ctrl;
  282. poll_wait(file, &bt_bmc->queue, wait);
  283. ctrl = bt_inb(bt_bmc, BT_CTRL);
  284. if (ctrl & BT_CTRL_H2B_ATN)
  285. mask |= EPOLLIN;
  286. if (!(ctrl & (BT_CTRL_H_BUSY | BT_CTRL_B2H_ATN)))
  287. mask |= EPOLLOUT;
  288. return mask;
  289. }
  290. static const struct file_operations bt_bmc_fops = {
  291. .owner = THIS_MODULE,
  292. .open = bt_bmc_open,
  293. .read = bt_bmc_read,
  294. .write = bt_bmc_write,
  295. .release = bt_bmc_release,
  296. .poll = bt_bmc_poll,
  297. .unlocked_ioctl = bt_bmc_ioctl,
  298. };
  299. static void poll_timer(struct timer_list *t)
  300. {
  301. struct bt_bmc *bt_bmc = from_timer(bt_bmc, t, poll_timer);
  302. bt_bmc->poll_timer.expires += msecs_to_jiffies(500);
  303. wake_up(&bt_bmc->queue);
  304. add_timer(&bt_bmc->poll_timer);
  305. }
  306. static irqreturn_t bt_bmc_irq(int irq, void *arg)
  307. {
  308. struct bt_bmc *bt_bmc = arg;
  309. u32 reg;
  310. int rc;
  311. rc = regmap_read(bt_bmc->map, bt_bmc->offset + BT_CR2, &reg);
  312. if (rc)
  313. return IRQ_NONE;
  314. reg &= BT_CR2_IRQ_H2B | BT_CR2_IRQ_HBUSY;
  315. if (!reg)
  316. return IRQ_NONE;
  317. /* ack pending IRQs */
  318. regmap_write(bt_bmc->map, bt_bmc->offset + BT_CR2, reg);
  319. wake_up(&bt_bmc->queue);
  320. return IRQ_HANDLED;
  321. }
  322. static int bt_bmc_config_irq(struct bt_bmc *bt_bmc,
  323. struct platform_device *pdev)
  324. {
  325. struct device *dev = &pdev->dev;
  326. int rc;
  327. bt_bmc->irq = platform_get_irq(pdev, 0);
  328. if (!bt_bmc->irq)
  329. return -ENODEV;
  330. rc = devm_request_irq(dev, bt_bmc->irq, bt_bmc_irq, IRQF_SHARED,
  331. DEVICE_NAME, bt_bmc);
  332. if (rc < 0) {
  333. dev_warn(dev, "Unable to request IRQ %d\n", bt_bmc->irq);
  334. bt_bmc->irq = 0;
  335. return rc;
  336. }
  337. /*
  338. * Configure IRQs on the bmc clearing the H2B and HBUSY bits;
  339. * H2B will be asserted when the bmc has data for us; HBUSY
  340. * will be cleared (along with B2H) when we can write the next
  341. * message to the BT buffer
  342. */
  343. rc = regmap_update_bits(bt_bmc->map, bt_bmc->offset + BT_CR1,
  344. (BT_CR1_IRQ_H2B | BT_CR1_IRQ_HBUSY),
  345. (BT_CR1_IRQ_H2B | BT_CR1_IRQ_HBUSY));
  346. return rc;
  347. }
  348. static int bt_bmc_probe(struct platform_device *pdev)
  349. {
  350. struct bt_bmc *bt_bmc;
  351. struct device *dev;
  352. int rc;
  353. if (!pdev || !pdev->dev.of_node)
  354. return -ENODEV;
  355. dev = &pdev->dev;
  356. dev_info(dev, "Found bt bmc device\n");
  357. bt_bmc = devm_kzalloc(dev, sizeof(*bt_bmc), GFP_KERNEL);
  358. if (!bt_bmc)
  359. return -ENOMEM;
  360. dev_set_drvdata(&pdev->dev, bt_bmc);
  361. bt_bmc->map = syscon_node_to_regmap(pdev->dev.parent->of_node);
  362. if (IS_ERR(bt_bmc->map)) {
  363. struct resource *res;
  364. void __iomem *base;
  365. /*
  366. * Assume it's not the MFD-based devicetree description, in
  367. * which case generate a regmap ourselves
  368. */
  369. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  370. base = devm_ioremap_resource(&pdev->dev, res);
  371. if (IS_ERR(base))
  372. return PTR_ERR(base);
  373. bt_bmc->map = devm_regmap_init_mmio(dev, base, &bt_regmap_cfg);
  374. bt_bmc->offset = 0;
  375. } else {
  376. rc = of_property_read_u32(dev->of_node, "reg", &bt_bmc->offset);
  377. if (rc)
  378. return rc;
  379. }
  380. mutex_init(&bt_bmc->mutex);
  381. init_waitqueue_head(&bt_bmc->queue);
  382. bt_bmc->miscdev.minor = MISC_DYNAMIC_MINOR,
  383. bt_bmc->miscdev.name = DEVICE_NAME,
  384. bt_bmc->miscdev.fops = &bt_bmc_fops,
  385. bt_bmc->miscdev.parent = dev;
  386. rc = misc_register(&bt_bmc->miscdev);
  387. if (rc) {
  388. dev_err(dev, "Unable to register misc device\n");
  389. return rc;
  390. }
  391. bt_bmc_config_irq(bt_bmc, pdev);
  392. if (bt_bmc->irq) {
  393. dev_info(dev, "Using IRQ %d\n", bt_bmc->irq);
  394. } else {
  395. dev_info(dev, "No IRQ; using timer\n");
  396. timer_setup(&bt_bmc->poll_timer, poll_timer, 0);
  397. bt_bmc->poll_timer.expires = jiffies + msecs_to_jiffies(10);
  398. add_timer(&bt_bmc->poll_timer);
  399. }
  400. regmap_write(bt_bmc->map, bt_bmc->offset + BT_CR0,
  401. (BT_IO_BASE << BT_CR0_IO_BASE) |
  402. (BT_IRQ << BT_CR0_IRQ) |
  403. BT_CR0_EN_CLR_SLV_RDP |
  404. BT_CR0_EN_CLR_SLV_WRP |
  405. BT_CR0_ENABLE_IBT);
  406. clr_b_busy(bt_bmc);
  407. return 0;
  408. }
  409. static int bt_bmc_remove(struct platform_device *pdev)
  410. {
  411. struct bt_bmc *bt_bmc = dev_get_drvdata(&pdev->dev);
  412. misc_deregister(&bt_bmc->miscdev);
  413. if (!bt_bmc->irq)
  414. del_timer_sync(&bt_bmc->poll_timer);
  415. return 0;
  416. }
  417. static const struct of_device_id bt_bmc_match[] = {
  418. { .compatible = "aspeed,ast2400-ibt-bmc" },
  419. { .compatible = "aspeed,ast2500-ibt-bmc" },
  420. { },
  421. };
  422. static struct platform_driver bt_bmc_driver = {
  423. .driver = {
  424. .name = DEVICE_NAME,
  425. .of_match_table = bt_bmc_match,
  426. },
  427. .probe = bt_bmc_probe,
  428. .remove = bt_bmc_remove,
  429. };
  430. module_platform_driver(bt_bmc_driver);
  431. MODULE_DEVICE_TABLE(of, bt_bmc_match);
  432. MODULE_LICENSE("GPL");
  433. MODULE_AUTHOR("Alistair Popple <alistair@popple.id.au>");
  434. MODULE_DESCRIPTION("Linux device interface to the IPMI BT interface");