bt-bmc.c 11 KB

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