amd_axi_w1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * amd_axi_w1 - AMD 1Wire programmable logic bus host driver
  4. *
  5. * Copyright (C) 2022-2023 Advanced Micro Devices, Inc. All Rights Reserved.
  6. */
  7. #include <linux/atomic.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/clk.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mod_devicetable.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/types.h>
  18. #include <linux/wait.h>
  19. #include <linux/w1.h>
  20. /* 1-wire AMD IP definition */
  21. #define AXIW1_IPID 0x10ee4453
  22. /* Registers offset */
  23. #define AXIW1_INST_REG 0x0
  24. #define AXIW1_CTRL_REG 0x4
  25. #define AXIW1_IRQE_REG 0x8
  26. #define AXIW1_STAT_REG 0xC
  27. #define AXIW1_DATA_REG 0x10
  28. #define AXIW1_IPVER_REG 0x18
  29. #define AXIW1_IPID_REG 0x1C
  30. /* Instructions */
  31. #define AXIW1_INITPRES 0x0800
  32. #define AXIW1_READBIT 0x0C00
  33. #define AXIW1_WRITEBIT 0x0E00
  34. #define AXIW1_READBYTE 0x0D00
  35. #define AXIW1_WRITEBYTE 0x0F00
  36. /* Status flag masks */
  37. #define AXIW1_DONE BIT(0)
  38. #define AXIW1_READY BIT(4)
  39. #define AXIW1_PRESENCE BIT(31)
  40. #define AXIW1_MAJORVER_MASK GENMASK(23, 8)
  41. #define AXIW1_MINORVER_MASK GENMASK(7, 0)
  42. /* Control flag */
  43. #define AXIW1_GO BIT(0)
  44. #define AXI_CLEAR 0
  45. #define AXI_RESET BIT(31)
  46. #define AXIW1_READDATA BIT(0)
  47. /* Interrupt Enable */
  48. #define AXIW1_READY_IRQ_EN BIT(4)
  49. #define AXIW1_DONE_IRQ_EN BIT(0)
  50. #define AXIW1_TIMEOUT msecs_to_jiffies(100)
  51. #define DRIVER_NAME "amd_axi_w1"
  52. struct amd_axi_w1_local {
  53. struct device *dev;
  54. void __iomem *base_addr;
  55. int irq;
  56. atomic_t flag; /* Set on IRQ, cleared once serviced */
  57. wait_queue_head_t wait_queue;
  58. struct w1_bus_master bus_host;
  59. };
  60. /**
  61. * amd_axi_w1_wait_irq_interruptible_timeout() - Wait for IRQ with timeout.
  62. *
  63. * @amd_axi_w1_local: Pointer to device structure
  64. * @IRQ: IRQ channel to wait on
  65. *
  66. * Return: %0 - OK, %-EINTR - Interrupted, %-EBUSY - Timed out
  67. */
  68. static int amd_axi_w1_wait_irq_interruptible_timeout(struct amd_axi_w1_local *amd_axi_w1_local,
  69. u32 IRQ)
  70. {
  71. int ret;
  72. /* Enable the IRQ requested and wait for flag to indicate it's been triggered */
  73. iowrite32(IRQ, amd_axi_w1_local->base_addr + AXIW1_IRQE_REG);
  74. ret = wait_event_interruptible_timeout(amd_axi_w1_local->wait_queue,
  75. atomic_read(&amd_axi_w1_local->flag) != 0,
  76. AXIW1_TIMEOUT);
  77. if (ret < 0) {
  78. dev_err(amd_axi_w1_local->dev, "Wait IRQ Interrupted\n");
  79. return -EINTR;
  80. }
  81. if (!ret) {
  82. dev_err(amd_axi_w1_local->dev, "Wait IRQ Timeout\n");
  83. return -EBUSY;
  84. }
  85. atomic_set(&amd_axi_w1_local->flag, 0);
  86. return 0;
  87. }
  88. /**
  89. * amd_axi_w1_touch_bit() - Performs the touch-bit function - write a 0 or 1 and reads the level.
  90. *
  91. * @data: Pointer to device structure
  92. * @bit: The level to write
  93. *
  94. * Return: The level read
  95. */
  96. static u8 amd_axi_w1_touch_bit(void *data, u8 bit)
  97. {
  98. struct amd_axi_w1_local *amd_axi_w1_local = data;
  99. u8 val = 0;
  100. int rc;
  101. /* Wait for READY signal to be 1 to ensure 1-wire IP is ready */
  102. while ((ioread32(amd_axi_w1_local->base_addr + AXIW1_STAT_REG) & AXIW1_READY) == 0) {
  103. rc = amd_axi_w1_wait_irq_interruptible_timeout(amd_axi_w1_local,
  104. AXIW1_READY_IRQ_EN);
  105. if (rc < 0)
  106. return 1; /* Callee doesn't test for error. Return inactive bus state */
  107. }
  108. if (bit)
  109. /* Read. Write read Bit command in register 0 */
  110. iowrite32(AXIW1_READBIT, amd_axi_w1_local->base_addr + AXIW1_INST_REG);
  111. else
  112. /* Write. Write tx Bit command in instruction register with bit to transmit */
  113. iowrite32(AXIW1_WRITEBIT + (bit & 0x01),
  114. amd_axi_w1_local->base_addr + AXIW1_INST_REG);
  115. /* Write Go signal and clear control reset signal in control register */
  116. iowrite32(AXIW1_GO, amd_axi_w1_local->base_addr + AXIW1_CTRL_REG);
  117. /* Wait for done signal to be 1 */
  118. while ((ioread32(amd_axi_w1_local->base_addr + AXIW1_STAT_REG) & AXIW1_DONE) != 1) {
  119. rc = amd_axi_w1_wait_irq_interruptible_timeout(amd_axi_w1_local, AXIW1_DONE_IRQ_EN);
  120. if (rc < 0)
  121. return 1; /* Callee doesn't test for error. Return inactive bus state */
  122. }
  123. /* If read, Retrieve data from register */
  124. if (bit)
  125. val = (u8)(ioread32(amd_axi_w1_local->base_addr + AXIW1_DATA_REG) & AXIW1_READDATA);
  126. /* Clear Go signal in register 1 */
  127. iowrite32(AXI_CLEAR, amd_axi_w1_local->base_addr + AXIW1_CTRL_REG);
  128. return val;
  129. }
  130. /**
  131. * amd_axi_w1_read_byte - Performs the read byte function.
  132. *
  133. * @data: Pointer to device structure
  134. * Return: The value read
  135. */
  136. static u8 amd_axi_w1_read_byte(void *data)
  137. {
  138. struct amd_axi_w1_local *amd_axi_w1_local = data;
  139. u8 val = 0;
  140. int rc;
  141. /* Wait for READY signal to be 1 to ensure 1-wire IP is ready */
  142. while ((ioread32(amd_axi_w1_local->base_addr + AXIW1_STAT_REG) & AXIW1_READY) == 0) {
  143. rc = amd_axi_w1_wait_irq_interruptible_timeout(amd_axi_w1_local,
  144. AXIW1_READY_IRQ_EN);
  145. if (rc < 0)
  146. return 0xFF; /* Return inactive bus state */
  147. }
  148. /* Write read Byte command in instruction register*/
  149. iowrite32(AXIW1_READBYTE, amd_axi_w1_local->base_addr + AXIW1_INST_REG);
  150. /* Write Go signal and clear control reset signal in control register */
  151. iowrite32(AXIW1_GO, amd_axi_w1_local->base_addr + AXIW1_CTRL_REG);
  152. /* Wait for done signal to be 1 */
  153. while ((ioread32(amd_axi_w1_local->base_addr + AXIW1_STAT_REG) & AXIW1_DONE) != 1) {
  154. rc = amd_axi_w1_wait_irq_interruptible_timeout(amd_axi_w1_local, AXIW1_DONE_IRQ_EN);
  155. if (rc < 0)
  156. return 0xFF; /* Return inactive bus state */
  157. }
  158. /* Retrieve LSB bit in data register to get RX byte */
  159. val = (u8)(ioread32(amd_axi_w1_local->base_addr + AXIW1_DATA_REG) & 0x000000FF);
  160. /* Clear Go signal in control register */
  161. iowrite32(AXI_CLEAR, amd_axi_w1_local->base_addr + AXIW1_CTRL_REG);
  162. return val;
  163. }
  164. /**
  165. * amd_axi_w1_write_byte - Performs the write byte function.
  166. *
  167. * @data: The ds2482 channel pointer
  168. * @val: The value to write
  169. */
  170. static void amd_axi_w1_write_byte(void *data, u8 val)
  171. {
  172. struct amd_axi_w1_local *amd_axi_w1_local = data;
  173. int rc;
  174. /* Wait for READY signal to be 1 to ensure 1-wire IP is ready */
  175. while ((ioread32(amd_axi_w1_local->base_addr + AXIW1_STAT_REG) & AXIW1_READY) == 0) {
  176. rc = amd_axi_w1_wait_irq_interruptible_timeout(amd_axi_w1_local,
  177. AXIW1_READY_IRQ_EN);
  178. if (rc < 0)
  179. return;
  180. }
  181. /* Write tx Byte command in instruction register with bit to transmit */
  182. iowrite32(AXIW1_WRITEBYTE + val, amd_axi_w1_local->base_addr + AXIW1_INST_REG);
  183. /* Write Go signal and clear control reset signal in register 1 */
  184. iowrite32(AXIW1_GO, amd_axi_w1_local->base_addr + AXIW1_CTRL_REG);
  185. /* Wait for done signal to be 1 */
  186. while ((ioread32(amd_axi_w1_local->base_addr + AXIW1_STAT_REG) & AXIW1_DONE) != 1) {
  187. rc = amd_axi_w1_wait_irq_interruptible_timeout(amd_axi_w1_local,
  188. AXIW1_DONE_IRQ_EN);
  189. if (rc < 0)
  190. return;
  191. }
  192. /* Clear Go signal in control register */
  193. iowrite32(AXI_CLEAR, amd_axi_w1_local->base_addr + AXIW1_CTRL_REG);
  194. }
  195. /**
  196. * amd_axi_w1_reset_bus() - Issues a reset bus sequence.
  197. *
  198. * @data: the bus host data struct
  199. * Return: 0=Device present, 1=No device present or error
  200. */
  201. static u8 amd_axi_w1_reset_bus(void *data)
  202. {
  203. struct amd_axi_w1_local *amd_axi_w1_local = data;
  204. u8 val = 0;
  205. int rc;
  206. /* Reset 1-wire Axi IP */
  207. iowrite32(AXI_RESET, amd_axi_w1_local->base_addr + AXIW1_CTRL_REG);
  208. /* Wait for READY signal to be 1 to ensure 1-wire IP is ready */
  209. while ((ioread32(amd_axi_w1_local->base_addr + AXIW1_STAT_REG) & AXIW1_READY) == 0) {
  210. rc = amd_axi_w1_wait_irq_interruptible_timeout(amd_axi_w1_local,
  211. AXIW1_READY_IRQ_EN);
  212. if (rc < 0)
  213. return 1; /* Something went wrong with the hardware */
  214. }
  215. /* Write Initialization command in instruction register */
  216. iowrite32(AXIW1_INITPRES, amd_axi_w1_local->base_addr + AXIW1_INST_REG);
  217. /* Write Go signal and clear control reset signal in register 1 */
  218. iowrite32(AXIW1_GO, amd_axi_w1_local->base_addr + AXIW1_CTRL_REG);
  219. /* Wait for done signal to be 1 */
  220. while ((ioread32(amd_axi_w1_local->base_addr + AXIW1_STAT_REG) & AXIW1_DONE) != 1) {
  221. rc = amd_axi_w1_wait_irq_interruptible_timeout(amd_axi_w1_local, AXIW1_DONE_IRQ_EN);
  222. if (rc < 0)
  223. return 1; /* Something went wrong with the hardware */
  224. }
  225. /* Retrieve MSB bit in status register to get failure bit */
  226. if ((ioread32(amd_axi_w1_local->base_addr + AXIW1_STAT_REG) & AXIW1_PRESENCE) != 0)
  227. val = 1;
  228. /* Clear Go signal in control register */
  229. iowrite32(AXI_CLEAR, amd_axi_w1_local->base_addr + AXIW1_CTRL_REG);
  230. return val;
  231. }
  232. /* Reset the 1-wire AXI IP. Put the IP in reset state and clear registers */
  233. static void amd_axi_w1_reset(struct amd_axi_w1_local *amd_axi_w1_local)
  234. {
  235. iowrite32(AXI_RESET, amd_axi_w1_local->base_addr + AXIW1_CTRL_REG);
  236. iowrite32(AXI_CLEAR, amd_axi_w1_local->base_addr + AXIW1_INST_REG);
  237. iowrite32(AXI_CLEAR, amd_axi_w1_local->base_addr + AXIW1_IRQE_REG);
  238. iowrite32(AXI_CLEAR, amd_axi_w1_local->base_addr + AXIW1_STAT_REG);
  239. iowrite32(AXI_CLEAR, amd_axi_w1_local->base_addr + AXIW1_DATA_REG);
  240. }
  241. static irqreturn_t amd_axi_w1_irq(int irq, void *lp)
  242. {
  243. struct amd_axi_w1_local *amd_axi_w1_local = lp;
  244. /* Reset interrupt trigger */
  245. iowrite32(AXI_CLEAR, amd_axi_w1_local->base_addr + AXIW1_IRQE_REG);
  246. atomic_set(&amd_axi_w1_local->flag, 1);
  247. wake_up_interruptible(&amd_axi_w1_local->wait_queue);
  248. return IRQ_HANDLED;
  249. }
  250. static int amd_axi_w1_probe(struct platform_device *pdev)
  251. {
  252. struct device *dev = &pdev->dev;
  253. struct amd_axi_w1_local *lp;
  254. struct clk *clk;
  255. u32 ver_major, ver_minor;
  256. int val, rc = 0;
  257. lp = devm_kzalloc(dev, sizeof(*lp), GFP_KERNEL);
  258. if (!lp)
  259. return -ENOMEM;
  260. lp->dev = dev;
  261. lp->base_addr = devm_platform_ioremap_resource(pdev, 0);
  262. if (IS_ERR(lp->base_addr))
  263. return PTR_ERR(lp->base_addr);
  264. lp->irq = platform_get_irq(pdev, 0);
  265. if (lp->irq < 0)
  266. return lp->irq;
  267. rc = devm_request_irq(dev, lp->irq, &amd_axi_w1_irq, IRQF_TRIGGER_HIGH, DRIVER_NAME, lp);
  268. if (rc)
  269. return rc;
  270. /* Initialize wait queue and flag */
  271. init_waitqueue_head(&lp->wait_queue);
  272. clk = devm_clk_get_enabled(dev, NULL);
  273. if (IS_ERR(clk))
  274. return PTR_ERR(clk);
  275. /* Verify IP presence in HW */
  276. if (ioread32(lp->base_addr + AXIW1_IPID_REG) != AXIW1_IPID) {
  277. dev_err(dev, "AMD 1-wire IP not detected in hardware\n");
  278. return -ENODEV;
  279. }
  280. /*
  281. * Allow for future driver expansion supporting new hardware features
  282. * This driver currently only supports hardware 1.x, but include logic
  283. * to detect if a potentially incompatible future version is used
  284. * by reading major version ID. It is highly undesirable for new IP versions
  285. * to break the API, but this code will at least allow for graceful failure
  286. * should that happen. Future new features can be enabled by hardware
  287. * incrementing the minor version and augmenting the driver to detect capability
  288. * using the minor version number
  289. */
  290. val = ioread32(lp->base_addr + AXIW1_IPVER_REG);
  291. ver_major = FIELD_GET(AXIW1_MAJORVER_MASK, val);
  292. ver_minor = FIELD_GET(AXIW1_MINORVER_MASK, val);
  293. if (ver_major != 1) {
  294. dev_err(dev, "AMD AXI W1 host version %u.%u is not supported by this driver",
  295. ver_major, ver_minor);
  296. return -ENODEV;
  297. }
  298. lp->bus_host.data = lp;
  299. lp->bus_host.touch_bit = amd_axi_w1_touch_bit;
  300. lp->bus_host.read_byte = amd_axi_w1_read_byte;
  301. lp->bus_host.write_byte = amd_axi_w1_write_byte;
  302. lp->bus_host.reset_bus = amd_axi_w1_reset_bus;
  303. amd_axi_w1_reset(lp);
  304. platform_set_drvdata(pdev, lp);
  305. rc = w1_add_master_device(&lp->bus_host);
  306. if (rc) {
  307. dev_err(dev, "Could not add host device\n");
  308. return rc;
  309. }
  310. return 0;
  311. }
  312. static void amd_axi_w1_remove(struct platform_device *pdev)
  313. {
  314. struct amd_axi_w1_local *lp = platform_get_drvdata(pdev);
  315. w1_remove_master_device(&lp->bus_host);
  316. }
  317. static const struct of_device_id amd_axi_w1_of_match[] = {
  318. { .compatible = "amd,axi-1wire-host" },
  319. { /* end of list */ },
  320. };
  321. MODULE_DEVICE_TABLE(of, amd_axi_w1_of_match);
  322. static struct platform_driver amd_axi_w1_driver = {
  323. .probe = amd_axi_w1_probe,
  324. .remove = amd_axi_w1_remove,
  325. .driver = {
  326. .name = DRIVER_NAME,
  327. .of_match_table = amd_axi_w1_of_match,
  328. },
  329. };
  330. module_platform_driver(amd_axi_w1_driver);
  331. MODULE_LICENSE("GPL");
  332. MODULE_AUTHOR("Kris Chaplin <kris.chaplin@amd.com>");
  333. MODULE_DESCRIPTION("Driver for AMD AXI 1 Wire IP core");