xgene_enet_xgmac.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /* Applied Micro X-Gene SoC Ethernet Driver
  2. *
  3. * Copyright (c) 2014, Applied Micro Circuits Corporation
  4. * Authors: Iyappan Subramanian <isubramanian@apm.com>
  5. * Keyur Chudgar <kchudgar@apm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "xgene_enet_main.h"
  21. #include "xgene_enet_hw.h"
  22. #include "xgene_enet_xgmac.h"
  23. static void xgene_enet_wr_csr(struct xgene_enet_pdata *pdata,
  24. u32 offset, u32 val)
  25. {
  26. void __iomem *addr = pdata->eth_csr_addr + offset;
  27. iowrite32(val, addr);
  28. }
  29. static void xgene_enet_wr_ring_if(struct xgene_enet_pdata *pdata,
  30. u32 offset, u32 val)
  31. {
  32. void __iomem *addr = pdata->eth_ring_if_addr + offset;
  33. iowrite32(val, addr);
  34. }
  35. static void xgene_enet_wr_diag_csr(struct xgene_enet_pdata *pdata,
  36. u32 offset, u32 val)
  37. {
  38. void __iomem *addr = pdata->eth_diag_csr_addr + offset;
  39. iowrite32(val, addr);
  40. }
  41. static bool xgene_enet_wr_indirect(void __iomem *addr, void __iomem *wr,
  42. void __iomem *cmd, void __iomem *cmd_done,
  43. u32 wr_addr, u32 wr_data)
  44. {
  45. u32 done;
  46. u8 wait = 10;
  47. iowrite32(wr_addr, addr);
  48. iowrite32(wr_data, wr);
  49. iowrite32(XGENE_ENET_WR_CMD, cmd);
  50. /* wait for write command to complete */
  51. while (!(done = ioread32(cmd_done)) && wait--)
  52. udelay(1);
  53. if (!done)
  54. return false;
  55. iowrite32(0, cmd);
  56. return true;
  57. }
  58. static void xgene_enet_wr_mac(struct xgene_enet_pdata *pdata,
  59. u32 wr_addr, u32 wr_data)
  60. {
  61. void __iomem *addr, *wr, *cmd, *cmd_done;
  62. addr = pdata->mcx_mac_addr + MAC_ADDR_REG_OFFSET;
  63. wr = pdata->mcx_mac_addr + MAC_WRITE_REG_OFFSET;
  64. cmd = pdata->mcx_mac_addr + MAC_COMMAND_REG_OFFSET;
  65. cmd_done = pdata->mcx_mac_addr + MAC_COMMAND_DONE_REG_OFFSET;
  66. if (!xgene_enet_wr_indirect(addr, wr, cmd, cmd_done, wr_addr, wr_data))
  67. netdev_err(pdata->ndev, "MCX mac write failed, addr: %04x\n",
  68. wr_addr);
  69. }
  70. static void xgene_enet_rd_csr(struct xgene_enet_pdata *pdata,
  71. u32 offset, u32 *val)
  72. {
  73. void __iomem *addr = pdata->eth_csr_addr + offset;
  74. *val = ioread32(addr);
  75. }
  76. static void xgene_enet_rd_diag_csr(struct xgene_enet_pdata *pdata,
  77. u32 offset, u32 *val)
  78. {
  79. void __iomem *addr = pdata->eth_diag_csr_addr + offset;
  80. *val = ioread32(addr);
  81. }
  82. static bool xgene_enet_rd_indirect(void __iomem *addr, void __iomem *rd,
  83. void __iomem *cmd, void __iomem *cmd_done,
  84. u32 rd_addr, u32 *rd_data)
  85. {
  86. u32 done;
  87. u8 wait = 10;
  88. iowrite32(rd_addr, addr);
  89. iowrite32(XGENE_ENET_RD_CMD, cmd);
  90. /* wait for read command to complete */
  91. while (!(done = ioread32(cmd_done)) && wait--)
  92. udelay(1);
  93. if (!done)
  94. return false;
  95. *rd_data = ioread32(rd);
  96. iowrite32(0, cmd);
  97. return true;
  98. }
  99. static void xgene_enet_rd_mac(struct xgene_enet_pdata *pdata,
  100. u32 rd_addr, u32 *rd_data)
  101. {
  102. void __iomem *addr, *rd, *cmd, *cmd_done;
  103. addr = pdata->mcx_mac_addr + MAC_ADDR_REG_OFFSET;
  104. rd = pdata->mcx_mac_addr + MAC_READ_REG_OFFSET;
  105. cmd = pdata->mcx_mac_addr + MAC_COMMAND_REG_OFFSET;
  106. cmd_done = pdata->mcx_mac_addr + MAC_COMMAND_DONE_REG_OFFSET;
  107. if (!xgene_enet_rd_indirect(addr, rd, cmd, cmd_done, rd_addr, rd_data))
  108. netdev_err(pdata->ndev, "MCX mac read failed, addr: %04x\n",
  109. rd_addr);
  110. }
  111. static int xgene_enet_ecc_init(struct xgene_enet_pdata *pdata)
  112. {
  113. struct net_device *ndev = pdata->ndev;
  114. u32 data;
  115. u8 wait = 10;
  116. xgene_enet_wr_diag_csr(pdata, ENET_CFG_MEM_RAM_SHUTDOWN_ADDR, 0x0);
  117. do {
  118. usleep_range(100, 110);
  119. xgene_enet_rd_diag_csr(pdata, ENET_BLOCK_MEM_RDY_ADDR, &data);
  120. } while ((data != 0xffffffff) && wait--);
  121. if (data != 0xffffffff) {
  122. netdev_err(ndev, "Failed to release memory from shutdown\n");
  123. return -ENODEV;
  124. }
  125. return 0;
  126. }
  127. static void xgene_enet_config_ring_if_assoc(struct xgene_enet_pdata *pdata)
  128. {
  129. xgene_enet_wr_ring_if(pdata, ENET_CFGSSQMIWQASSOC_ADDR, 0);
  130. xgene_enet_wr_ring_if(pdata, ENET_CFGSSQMIFPQASSOC_ADDR, 0);
  131. xgene_enet_wr_ring_if(pdata, ENET_CFGSSQMIQMLITEWQASSOC_ADDR, 0);
  132. xgene_enet_wr_ring_if(pdata, ENET_CFGSSQMIQMLITEFPQASSOC_ADDR, 0);
  133. }
  134. static void xgene_xgmac_reset(struct xgene_enet_pdata *pdata)
  135. {
  136. xgene_enet_wr_mac(pdata, AXGMAC_CONFIG_0, HSTMACRST);
  137. xgene_enet_wr_mac(pdata, AXGMAC_CONFIG_0, 0);
  138. }
  139. static void xgene_xgmac_set_mac_addr(struct xgene_enet_pdata *pdata)
  140. {
  141. u32 addr0, addr1;
  142. u8 *dev_addr = pdata->ndev->dev_addr;
  143. addr0 = (dev_addr[3] << 24) | (dev_addr[2] << 16) |
  144. (dev_addr[1] << 8) | dev_addr[0];
  145. addr1 = (dev_addr[5] << 24) | (dev_addr[4] << 16);
  146. xgene_enet_wr_mac(pdata, HSTMACADR_LSW_ADDR, addr0);
  147. xgene_enet_wr_mac(pdata, HSTMACADR_MSW_ADDR, addr1);
  148. }
  149. static u32 xgene_enet_link_status(struct xgene_enet_pdata *pdata)
  150. {
  151. u32 data;
  152. xgene_enet_rd_csr(pdata, XG_LINK_STATUS_ADDR, &data);
  153. return data;
  154. }
  155. static void xgene_xgmac_init(struct xgene_enet_pdata *pdata)
  156. {
  157. u32 data;
  158. xgene_xgmac_reset(pdata);
  159. xgene_enet_rd_mac(pdata, AXGMAC_CONFIG_1, &data);
  160. data |= HSTPPEN;
  161. data &= ~HSTLENCHK;
  162. xgene_enet_wr_mac(pdata, AXGMAC_CONFIG_1, data);
  163. xgene_enet_wr_mac(pdata, HSTMAXFRAME_LENGTH_ADDR, 0x06000600);
  164. xgene_xgmac_set_mac_addr(pdata);
  165. xgene_enet_rd_csr(pdata, XG_RSIF_CONFIG_REG_ADDR, &data);
  166. data |= CFG_RSIF_FPBUFF_TIMEOUT_EN;
  167. xgene_enet_wr_csr(pdata, XG_RSIF_CONFIG_REG_ADDR, data);
  168. xgene_enet_wr_csr(pdata, XG_CFG_BYPASS_ADDR, RESUME_TX);
  169. xgene_enet_wr_csr(pdata, XGENET_RX_DV_GATE_REG_0_ADDR, 0);
  170. xgene_enet_rd_csr(pdata, XG_ENET_SPARE_CFG_REG_ADDR, &data);
  171. data |= BIT(12);
  172. xgene_enet_wr_csr(pdata, XG_ENET_SPARE_CFG_REG_ADDR, data);
  173. xgene_enet_wr_csr(pdata, XG_ENET_SPARE_CFG_REG_1_ADDR, 0x82);
  174. }
  175. static void xgene_xgmac_rx_enable(struct xgene_enet_pdata *pdata)
  176. {
  177. u32 data;
  178. xgene_enet_rd_mac(pdata, AXGMAC_CONFIG_1, &data);
  179. xgene_enet_wr_mac(pdata, AXGMAC_CONFIG_1, data | HSTRFEN);
  180. }
  181. static void xgene_xgmac_tx_enable(struct xgene_enet_pdata *pdata)
  182. {
  183. u32 data;
  184. xgene_enet_rd_mac(pdata, AXGMAC_CONFIG_1, &data);
  185. xgene_enet_wr_mac(pdata, AXGMAC_CONFIG_1, data | HSTTFEN);
  186. }
  187. static void xgene_xgmac_rx_disable(struct xgene_enet_pdata *pdata)
  188. {
  189. u32 data;
  190. xgene_enet_rd_mac(pdata, AXGMAC_CONFIG_1, &data);
  191. xgene_enet_wr_mac(pdata, AXGMAC_CONFIG_1, data & ~HSTRFEN);
  192. }
  193. static void xgene_xgmac_tx_disable(struct xgene_enet_pdata *pdata)
  194. {
  195. u32 data;
  196. xgene_enet_rd_mac(pdata, AXGMAC_CONFIG_1, &data);
  197. xgene_enet_wr_mac(pdata, AXGMAC_CONFIG_1, data & ~HSTTFEN);
  198. }
  199. static int xgene_enet_reset(struct xgene_enet_pdata *pdata)
  200. {
  201. if (!xgene_ring_mgr_init(pdata))
  202. return -ENODEV;
  203. if (!IS_ERR(pdata->clk)) {
  204. clk_prepare_enable(pdata->clk);
  205. clk_disable_unprepare(pdata->clk);
  206. clk_prepare_enable(pdata->clk);
  207. }
  208. xgene_enet_ecc_init(pdata);
  209. xgene_enet_config_ring_if_assoc(pdata);
  210. return 0;
  211. }
  212. static void xgene_enet_xgcle_bypass(struct xgene_enet_pdata *pdata,
  213. u32 dst_ring_num, u16 bufpool_id)
  214. {
  215. u32 cb, fpsel;
  216. xgene_enet_rd_csr(pdata, XCLE_BYPASS_REG0_ADDR, &cb);
  217. cb |= CFG_CLE_BYPASS_EN0;
  218. CFG_CLE_IP_PROTOCOL0_SET(&cb, 3);
  219. xgene_enet_wr_csr(pdata, XCLE_BYPASS_REG0_ADDR, cb);
  220. fpsel = xgene_enet_ring_bufnum(bufpool_id) - 0x20;
  221. xgene_enet_rd_csr(pdata, XCLE_BYPASS_REG1_ADDR, &cb);
  222. CFG_CLE_DSTQID0_SET(&cb, dst_ring_num);
  223. CFG_CLE_FPSEL0_SET(&cb, fpsel);
  224. xgene_enet_wr_csr(pdata, XCLE_BYPASS_REG1_ADDR, cb);
  225. }
  226. static void xgene_enet_shutdown(struct xgene_enet_pdata *pdata)
  227. {
  228. if (!IS_ERR(pdata->clk))
  229. clk_disable_unprepare(pdata->clk);
  230. }
  231. static void xgene_enet_link_state(struct work_struct *work)
  232. {
  233. struct xgene_enet_pdata *pdata = container_of(to_delayed_work(work),
  234. struct xgene_enet_pdata, link_work);
  235. struct net_device *ndev = pdata->ndev;
  236. u32 link_status, poll_interval;
  237. link_status = xgene_enet_link_status(pdata);
  238. if (link_status) {
  239. if (!netif_carrier_ok(ndev)) {
  240. netif_carrier_on(ndev);
  241. xgene_xgmac_init(pdata);
  242. xgene_xgmac_rx_enable(pdata);
  243. xgene_xgmac_tx_enable(pdata);
  244. netdev_info(ndev, "Link is Up - 10Gbps\n");
  245. }
  246. poll_interval = PHY_POLL_LINK_ON;
  247. } else {
  248. if (netif_carrier_ok(ndev)) {
  249. xgene_xgmac_rx_disable(pdata);
  250. xgene_xgmac_tx_disable(pdata);
  251. netif_carrier_off(ndev);
  252. netdev_info(ndev, "Link is Down\n");
  253. }
  254. poll_interval = PHY_POLL_LINK_OFF;
  255. }
  256. schedule_delayed_work(&pdata->link_work, poll_interval);
  257. }
  258. struct xgene_mac_ops xgene_xgmac_ops = {
  259. .init = xgene_xgmac_init,
  260. .reset = xgene_xgmac_reset,
  261. .rx_enable = xgene_xgmac_rx_enable,
  262. .tx_enable = xgene_xgmac_tx_enable,
  263. .rx_disable = xgene_xgmac_rx_disable,
  264. .tx_disable = xgene_xgmac_tx_disable,
  265. .set_mac_addr = xgene_xgmac_set_mac_addr,
  266. .link_state = xgene_enet_link_state
  267. };
  268. struct xgene_port_ops xgene_xgport_ops = {
  269. .reset = xgene_enet_reset,
  270. .cle_bypass = xgene_enet_xgcle_bypass,
  271. .shutdown = xgene_enet_shutdown,
  272. };