ihs_axi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2016
  4. * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
  5. *
  6. * (C) Copyright 2017, 2018
  7. * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
  8. */
  9. #include <common.h>
  10. #include <axi.h>
  11. #include <dm.h>
  12. #include <regmap.h>
  13. /**
  14. * struct ihs_axi_regs - Structure for the register map of a IHS AXI device
  15. * @interrupt_status: Status register to indicate certain events (e.g.
  16. * error during transfer, transfer complete, etc.)
  17. * @interrupt_enable_control: Register to both control which statuses will be
  18. * indicated in the interrupt_status register, and
  19. * to change bus settings
  20. * @address_lsb: Least significant 16-bit word of the address of a
  21. * device to transfer data from/to
  22. * @address_msb: Most significant 16-bit word of the address of a
  23. * device to transfer data from/to
  24. * @write_data_lsb: Least significant 16-bit word of the data to be
  25. * written to a device
  26. * @write_data_msb: Most significant 16-bit word of the data to be
  27. * written to a device
  28. * @read_data_lsb: Least significant 16-bit word of the data read
  29. * from a device
  30. * @read_data_msb: Most significant 16-bit word of the data read
  31. * from a device
  32. */
  33. struct ihs_axi_regs {
  34. u16 interrupt_status;
  35. u16 interrupt_enable_control;
  36. u16 address_lsb;
  37. u16 address_msb;
  38. u16 write_data_lsb;
  39. u16 write_data_msb;
  40. u16 read_data_lsb;
  41. u16 read_data_msb;
  42. };
  43. /**
  44. * ihs_axi_set() - Convenience macro to set values in register map
  45. * @map: The register map to write to
  46. * @member: The member of the ihs_axi_regs structure to write
  47. * @val: The value to write to the register map
  48. */
  49. #define ihs_axi_set(map, member, val) \
  50. regmap_set(map, struct ihs_axi_regs, member, val)
  51. /**
  52. * ihs_axi_get() - Convenience macro to read values from register map
  53. * @map: The register map to read from
  54. * @member: The member of the ihs_axi_regs structure to read
  55. * @valp: Pointer to a buffer to receive the value read
  56. */
  57. #define ihs_axi_get(map, member, valp) \
  58. regmap_get(map, struct ihs_axi_regs, member, valp)
  59. /**
  60. * struct ihs_axi_priv - Private data structure of IHS AXI devices
  61. * @map: Register map for the IHS AXI device
  62. */
  63. struct ihs_axi_priv {
  64. struct regmap *map;
  65. };
  66. /**
  67. * enum status_reg - Description of bits in the interrupt_status register
  68. * @STATUS_READ_COMPLETE_EVENT: A read transfer was completed
  69. * @STATUS_WRITE_COMPLETE_EVENT: A write transfer was completed
  70. * @STATUS_TIMEOUT_EVENT: A timeout has occurred during the transfer
  71. * @STATUS_ERROR_EVENT: A error has occurred during the transfer
  72. * @STATUS_AXI_INT: A AXI interrupt has occurred
  73. * @STATUS_READ_DATA_AVAILABLE: Data is available to be read
  74. * @STATUS_BUSY: The bus is busy
  75. * @STATUS_INIT_DONE: The bus has finished initializing
  76. */
  77. enum status_reg {
  78. STATUS_READ_COMPLETE_EVENT = BIT(15),
  79. STATUS_WRITE_COMPLETE_EVENT = BIT(14),
  80. STATUS_TIMEOUT_EVENT = BIT(13),
  81. STATUS_ERROR_EVENT = BIT(12),
  82. STATUS_AXI_INT = BIT(11),
  83. STATUS_READ_DATA_AVAILABLE = BIT(7),
  84. STATUS_BUSY = BIT(6),
  85. STATUS_INIT_DONE = BIT(5),
  86. };
  87. /**
  88. * enum control_reg - Description of bit fields in the interrupt_enable_control
  89. * register
  90. * @CONTROL_READ_COMPLETE_EVENT_ENABLE: STATUS_READ_COMPLETE_EVENT will be
  91. * raised in the interrupt_status register
  92. * @CONTROL_WRITE_COMPLETE_EVENT_ENABLE: STATUS_WRITE_COMPLETE_EVENT will be
  93. * raised in the interrupt_status register
  94. * @CONTROL_TIMEOUT_EVENT_ENABLE: STATUS_TIMEOUT_EVENT will be raised in
  95. * the interrupt_status register
  96. * @CONTROL_ERROR_EVENT_ENABLE: STATUS_ERROR_EVENT will be raised in
  97. * the interrupt_status register
  98. * @CONTROL_AXI_INT_ENABLE: STATUS_AXI_INT will be raised in the
  99. * interrupt_status register
  100. * @CONTROL_CMD_NOP: Configure bus to send a NOP command
  101. * for the next transfer
  102. * @CONTROL_CMD_WRITE: Configure bus to do a write transfer
  103. * @CONTROL_CMD_WRITE_POST_INC: Auto-increment address after write
  104. * transfer
  105. * @CONTROL_CMD_READ: Configure bus to do a read transfer
  106. * @CONTROL_CMD_READ_POST_INC: Auto-increment address after read
  107. * transfer
  108. */
  109. enum control_reg {
  110. CONTROL_READ_COMPLETE_EVENT_ENABLE = BIT(15),
  111. CONTROL_WRITE_COMPLETE_EVENT_ENABLE = BIT(14),
  112. CONTROL_TIMEOUT_EVENT_ENABLE = BIT(13),
  113. CONTROL_ERROR_EVENT_ENABLE = BIT(12),
  114. CONTROL_AXI_INT_ENABLE = BIT(11),
  115. CONTROL_CMD_NOP = 0x0,
  116. CONTROL_CMD_WRITE = 0x8,
  117. CONTROL_CMD_WRITE_POST_INC = 0x9,
  118. CONTROL_CMD_READ = 0xa,
  119. CONTROL_CMD_READ_POST_INC = 0xb,
  120. };
  121. /**
  122. * enum axi_cmd - Determine if transfer is read or write transfer
  123. * @AXI_CMD_READ: The transfer should be a read transfer
  124. * @AXI_CMD_WRITE: The transfer should be a write transfer
  125. */
  126. enum axi_cmd {
  127. AXI_CMD_READ,
  128. AXI_CMD_WRITE,
  129. };
  130. /**
  131. * ihs_axi_transfer() - Run transfer on the AXI bus
  132. * @bus: The AXI bus device on which to run the transfer on
  133. * @address: The address to use in the transfer (i.e. which address to
  134. * read/write from/to)
  135. * @cmd: Should the transfer be a read or write transfer?
  136. *
  137. * Return: 0 if OK, -ve on error
  138. */
  139. static int ihs_axi_transfer(struct udevice *bus, ulong address,
  140. enum axi_cmd cmd)
  141. {
  142. struct ihs_axi_priv *priv = dev_get_priv(bus);
  143. /* Try waiting for events up to 10 times */
  144. const uint WAIT_TRIES = 10;
  145. u16 wait_mask = STATUS_TIMEOUT_EVENT |
  146. STATUS_ERROR_EVENT;
  147. u16 complete_flag;
  148. u16 status;
  149. uint k;
  150. if (cmd == AXI_CMD_READ) {
  151. complete_flag = STATUS_READ_COMPLETE_EVENT;
  152. cmd = CONTROL_CMD_READ;
  153. } else {
  154. complete_flag = STATUS_WRITE_COMPLETE_EVENT;
  155. cmd = CONTROL_CMD_WRITE;
  156. }
  157. wait_mask |= complete_flag;
  158. /* Lower 16 bit */
  159. ihs_axi_set(priv->map, address_lsb, address & 0xffff);
  160. /* Upper 16 bit */
  161. ihs_axi_set(priv->map, address_msb, (address >> 16) & 0xffff);
  162. ihs_axi_set(priv->map, interrupt_status, wait_mask);
  163. ihs_axi_set(priv->map, interrupt_enable_control, cmd);
  164. for (k = WAIT_TRIES; k > 0; --k) {
  165. ihs_axi_get(priv->map, interrupt_status, &status);
  166. if (status & wait_mask)
  167. break;
  168. udelay(1);
  169. }
  170. /*
  171. * k == 0 -> Tries ran out with no event we were waiting for actually
  172. * occurring.
  173. */
  174. if (!k)
  175. ihs_axi_get(priv->map, interrupt_status, &status);
  176. if (status & complete_flag)
  177. return 0;
  178. if (status & STATUS_ERROR_EVENT) {
  179. debug("%s: Error occurred during transfer\n", bus->name);
  180. return -EIO;
  181. }
  182. debug("%s: Transfer timed out\n", bus->name);
  183. return -ETIMEDOUT;
  184. }
  185. /*
  186. * API
  187. */
  188. static int ihs_axi_read(struct udevice *dev, ulong address, void *data,
  189. enum axi_size_t size)
  190. {
  191. struct ihs_axi_priv *priv = dev_get_priv(dev);
  192. int ret;
  193. u16 data_lsb, data_msb;
  194. u32 *p = data;
  195. if (size != AXI_SIZE_32) {
  196. debug("%s: transfer size '%d' not supported\n",
  197. dev->name, size);
  198. return -ENOSYS;
  199. }
  200. ret = ihs_axi_transfer(dev, address, AXI_CMD_READ);
  201. if (ret < 0) {
  202. debug("%s: Error during AXI transfer (err = %d)\n",
  203. dev->name, ret);
  204. return ret;
  205. }
  206. ihs_axi_get(priv->map, read_data_lsb, &data_lsb);
  207. ihs_axi_get(priv->map, read_data_msb, &data_msb);
  208. /* Assemble data from two 16-bit words */
  209. *p = (data_msb << 16) | data_lsb;
  210. return 0;
  211. }
  212. static int ihs_axi_write(struct udevice *dev, ulong address, void *data,
  213. enum axi_size_t size)
  214. {
  215. struct ihs_axi_priv *priv = dev_get_priv(dev);
  216. int ret;
  217. u32 *p = data;
  218. if (size != AXI_SIZE_32) {
  219. debug("%s: transfer size '%d' not supported\n",
  220. dev->name, size);
  221. return -ENOSYS;
  222. }
  223. /* Lower 16 bit */
  224. ihs_axi_set(priv->map, write_data_lsb, *p & 0xffff);
  225. /* Upper 16 bit */
  226. ihs_axi_set(priv->map, write_data_msb, (*p >> 16) & 0xffff);
  227. ret = ihs_axi_transfer(dev, address, AXI_CMD_WRITE);
  228. if (ret < 0) {
  229. debug("%s: Error during AXI transfer (err = %d)\n",
  230. dev->name, ret);
  231. return ret;
  232. }
  233. return 0;
  234. }
  235. static const struct udevice_id ihs_axi_ids[] = {
  236. { .compatible = "gdsys,ihs_axi" },
  237. { /* sentinel */ }
  238. };
  239. static const struct axi_ops ihs_axi_ops = {
  240. .read = ihs_axi_read,
  241. .write = ihs_axi_write,
  242. };
  243. static int ihs_axi_probe(struct udevice *dev)
  244. {
  245. struct ihs_axi_priv *priv = dev_get_priv(dev);
  246. regmap_init_mem(dev_ofnode(dev), &priv->map);
  247. return 0;
  248. }
  249. U_BOOT_DRIVER(ihs_axi_bus) = {
  250. .name = "ihs_axi_bus",
  251. .id = UCLASS_AXI,
  252. .of_match = ihs_axi_ids,
  253. .ops = &ihs_axi_ops,
  254. .priv_auto_alloc_size = sizeof(struct ihs_axi_priv),
  255. .probe = ihs_axi_probe,
  256. };