bt-bmc.c 10 KB

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