bt-bmc.c 12 KB

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