cvmx-helper-rgmii.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /***********************license start***************
  2. * Author: Cavium Networks
  3. *
  4. * Contact: [email protected]
  5. * This file is part of the OCTEON SDK
  6. *
  7. * Copyright (C) 2003-2018 Cavium, Inc.
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this file; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. * or visit http://www.gnu.org/licenses/.
  23. *
  24. * This file may also be available under a different license from Cavium.
  25. * Contact Cavium Networks for more information
  26. ***********************license end**************************************/
  27. /*
  28. * Functions for RGMII/GMII/MII initialization, configuration,
  29. * and monitoring.
  30. */
  31. #include <asm/octeon/octeon.h>
  32. #include <asm/octeon/cvmx-config.h>
  33. #include <asm/octeon/cvmx-pko.h>
  34. #include <asm/octeon/cvmx-helper.h>
  35. #include <asm/octeon/cvmx-helper-board.h>
  36. #include <asm/octeon/cvmx-npi-defs.h>
  37. #include <asm/octeon/cvmx-gmxx-defs.h>
  38. #include <asm/octeon/cvmx-asxx-defs.h>
  39. #include <asm/octeon/cvmx-dbg-defs.h>
  40. /*
  41. * Probe RGMII ports and determine the number present
  42. *
  43. * @interface: Interface to probe
  44. *
  45. * Returns Number of RGMII/GMII/MII ports (0-4).
  46. */
  47. int __cvmx_helper_rgmii_probe(int interface)
  48. {
  49. int num_ports = 0;
  50. union cvmx_gmxx_inf_mode mode;
  51. mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));
  52. if (mode.s.type) {
  53. if (OCTEON_IS_MODEL(OCTEON_CN38XX)
  54. || OCTEON_IS_MODEL(OCTEON_CN58XX)) {
  55. cvmx_dprintf("ERROR: RGMII initialize called in "
  56. "SPI interface\n");
  57. } else if (OCTEON_IS_MODEL(OCTEON_CN31XX)
  58. || OCTEON_IS_MODEL(OCTEON_CN30XX)
  59. || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
  60. /*
  61. * On these chips "type" says we're in
  62. * GMII/MII mode. This limits us to 2 ports
  63. */
  64. num_ports = 2;
  65. } else {
  66. cvmx_dprintf("ERROR: Unsupported Octeon model in %s\n",
  67. __func__);
  68. }
  69. } else {
  70. if (OCTEON_IS_MODEL(OCTEON_CN38XX)
  71. || OCTEON_IS_MODEL(OCTEON_CN58XX)) {
  72. num_ports = 4;
  73. } else if (OCTEON_IS_MODEL(OCTEON_CN31XX)
  74. || OCTEON_IS_MODEL(OCTEON_CN30XX)
  75. || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
  76. num_ports = 3;
  77. } else {
  78. cvmx_dprintf("ERROR: Unsupported Octeon model in %s\n",
  79. __func__);
  80. }
  81. }
  82. return num_ports;
  83. }
  84. /*
  85. * Put an RGMII interface in loopback mode. Internal packets sent
  86. * out will be received back again on the same port. Externally
  87. * received packets will echo back out.
  88. *
  89. * @port: IPD port number to loop.
  90. */
  91. void cvmx_helper_rgmii_internal_loopback(int port)
  92. {
  93. int interface = (port >> 4) & 1;
  94. int index = port & 0xf;
  95. uint64_t tmp;
  96. union cvmx_gmxx_prtx_cfg gmx_cfg;
  97. gmx_cfg.u64 = 0;
  98. gmx_cfg.s.duplex = 1;
  99. gmx_cfg.s.slottime = 1;
  100. gmx_cfg.s.speed = 1;
  101. cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 1);
  102. cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x200);
  103. cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0x2000);
  104. cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
  105. tmp = cvmx_read_csr(CVMX_ASXX_PRT_LOOP(interface));
  106. cvmx_write_csr(CVMX_ASXX_PRT_LOOP(interface), (1 << index) | tmp);
  107. tmp = cvmx_read_csr(CVMX_ASXX_TX_PRT_EN(interface));
  108. cvmx_write_csr(CVMX_ASXX_TX_PRT_EN(interface), (1 << index) | tmp);
  109. tmp = cvmx_read_csr(CVMX_ASXX_RX_PRT_EN(interface));
  110. cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface), (1 << index) | tmp);
  111. gmx_cfg.s.en = 1;
  112. cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), gmx_cfg.u64);
  113. }
  114. /*
  115. * Workaround ASX setup errata with CN38XX pass1
  116. *
  117. * @interface: Interface to setup
  118. * @port: Port to setup (0..3)
  119. * @cpu_clock_hz:
  120. * Chip frequency in Hertz
  121. *
  122. * Returns Zero on success, negative on failure
  123. */
  124. static int __cvmx_helper_errata_asx_pass1(int interface, int port,
  125. int cpu_clock_hz)
  126. {
  127. /* Set hi water mark as per errata GMX-4 */
  128. if (cpu_clock_hz >= 325000000 && cpu_clock_hz < 375000000)
  129. cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 12);
  130. else if (cpu_clock_hz >= 375000000 && cpu_clock_hz < 437000000)
  131. cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 11);
  132. else if (cpu_clock_hz >= 437000000 && cpu_clock_hz < 550000000)
  133. cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 10);
  134. else if (cpu_clock_hz >= 550000000 && cpu_clock_hz < 687000000)
  135. cvmx_write_csr(CVMX_ASXX_TX_HI_WATERX(port, interface), 9);
  136. else
  137. cvmx_dprintf("Illegal clock frequency (%d). "
  138. "CVMX_ASXX_TX_HI_WATERX not set\n", cpu_clock_hz);
  139. return 0;
  140. }
  141. /*
  142. * Configure all of the ASX, GMX, and PKO registers required
  143. * to get RGMII to function on the supplied interface.
  144. *
  145. * @interface: PKO Interface to configure (0 or 1)
  146. *
  147. * Returns Zero on success
  148. */
  149. int __cvmx_helper_rgmii_enable(int interface)
  150. {
  151. int num_ports = cvmx_helper_ports_on_interface(interface);
  152. int port;
  153. struct cvmx_sysinfo *sys_info_ptr = cvmx_sysinfo_get();
  154. union cvmx_gmxx_inf_mode mode;
  155. union cvmx_asxx_tx_prt_en asx_tx;
  156. union cvmx_asxx_rx_prt_en asx_rx;
  157. mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));
  158. if (mode.s.en == 0)
  159. return -1;
  160. if ((OCTEON_IS_MODEL(OCTEON_CN38XX) ||
  161. OCTEON_IS_MODEL(OCTEON_CN58XX)) && mode.s.type == 1)
  162. /* Ignore SPI interfaces */
  163. return -1;
  164. /* Configure the ASX registers needed to use the RGMII ports */
  165. asx_tx.u64 = 0;
  166. asx_tx.s.prt_en = cvmx_build_mask(num_ports);
  167. cvmx_write_csr(CVMX_ASXX_TX_PRT_EN(interface), asx_tx.u64);
  168. asx_rx.u64 = 0;
  169. asx_rx.s.prt_en = cvmx_build_mask(num_ports);
  170. cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface), asx_rx.u64);
  171. /* Configure the GMX registers needed to use the RGMII ports */
  172. for (port = 0; port < num_ports; port++) {
  173. /* Setting of CVMX_GMXX_TXX_THRESH has been moved to
  174. __cvmx_helper_setup_gmx() */
  175. if (cvmx_octeon_is_pass1())
  176. __cvmx_helper_errata_asx_pass1(interface, port,
  177. sys_info_ptr->
  178. cpu_clock_hz);
  179. else {
  180. /*
  181. * Configure more flexible RGMII preamble
  182. * checking. Pass 1 doesn't support this
  183. * feature.
  184. */
  185. union cvmx_gmxx_rxx_frm_ctl frm_ctl;
  186. frm_ctl.u64 =
  187. cvmx_read_csr(CVMX_GMXX_RXX_FRM_CTL
  188. (port, interface));
  189. /* New field, so must be compile time */
  190. frm_ctl.s.pre_free = 1;
  191. cvmx_write_csr(CVMX_GMXX_RXX_FRM_CTL(port, interface),
  192. frm_ctl.u64);
  193. }
  194. /*
  195. * Each pause frame transmitted will ask for about 10M
  196. * bit times before resume. If buffer space comes
  197. * available before that time has expired, an XON
  198. * pause frame (0 time) will be transmitted to restart
  199. * the flow.
  200. */
  201. cvmx_write_csr(CVMX_GMXX_TXX_PAUSE_PKT_TIME(port, interface),
  202. 20000);
  203. cvmx_write_csr(CVMX_GMXX_TXX_PAUSE_PKT_INTERVAL
  204. (port, interface), 19000);
  205. if (OCTEON_IS_MODEL(OCTEON_CN50XX)) {
  206. cvmx_write_csr(CVMX_ASXX_TX_CLK_SETX(port, interface),
  207. 16);
  208. cvmx_write_csr(CVMX_ASXX_RX_CLK_SETX(port, interface),
  209. 16);
  210. } else {
  211. cvmx_write_csr(CVMX_ASXX_TX_CLK_SETX(port, interface),
  212. 24);
  213. cvmx_write_csr(CVMX_ASXX_RX_CLK_SETX(port, interface),
  214. 24);
  215. }
  216. }
  217. __cvmx_helper_setup_gmx(interface, num_ports);
  218. /* enable the ports now */
  219. for (port = 0; port < num_ports; port++) {
  220. union cvmx_gmxx_prtx_cfg gmx_cfg;
  221. gmx_cfg.u64 =
  222. cvmx_read_csr(CVMX_GMXX_PRTX_CFG(port, interface));
  223. gmx_cfg.s.en = 1;
  224. cvmx_write_csr(CVMX_GMXX_PRTX_CFG(port, interface),
  225. gmx_cfg.u64);
  226. }
  227. __cvmx_interrupt_asxx_enable(interface);
  228. __cvmx_interrupt_gmxx_enable(interface);
  229. return 0;
  230. }
  231. /*
  232. * Return the link state of an IPD/PKO port as returned by
  233. * auto negotiation. The result of this function may not match
  234. * Octeon's link config if auto negotiation has changed since
  235. * the last call to cvmx_helper_link_set().
  236. *
  237. * @ipd_port: IPD/PKO port to query
  238. *
  239. * Returns Link state
  240. */
  241. union cvmx_helper_link_info __cvmx_helper_rgmii_link_get(int ipd_port)
  242. {
  243. int interface = cvmx_helper_get_interface_num(ipd_port);
  244. int index = cvmx_helper_get_interface_index_num(ipd_port);
  245. union cvmx_asxx_prt_loop asxx_prt_loop;
  246. asxx_prt_loop.u64 = cvmx_read_csr(CVMX_ASXX_PRT_LOOP(interface));
  247. if (asxx_prt_loop.s.int_loop & (1 << index)) {
  248. /* Force 1Gbps full duplex on internal loopback */
  249. union cvmx_helper_link_info result;
  250. result.u64 = 0;
  251. result.s.full_duplex = 1;
  252. result.s.link_up = 1;
  253. result.s.speed = 1000;
  254. return result;
  255. } else
  256. return __cvmx_helper_board_link_get(ipd_port);
  257. }
  258. /*
  259. * Configure an IPD/PKO port for the specified link state. This
  260. * function does not influence auto negotiation at the PHY level.
  261. * The passed link state must always match the link state returned
  262. * by cvmx_helper_link_get().
  263. *
  264. * @ipd_port: IPD/PKO port to configure
  265. * @link_info: The new link state
  266. *
  267. * Returns Zero on success, negative on failure
  268. */
  269. int __cvmx_helper_rgmii_link_set(int ipd_port,
  270. union cvmx_helper_link_info link_info)
  271. {
  272. int result = 0;
  273. int interface = cvmx_helper_get_interface_num(ipd_port);
  274. int index = cvmx_helper_get_interface_index_num(ipd_port);
  275. union cvmx_gmxx_prtx_cfg original_gmx_cfg;
  276. union cvmx_gmxx_prtx_cfg new_gmx_cfg;
  277. union cvmx_pko_mem_queue_qos pko_mem_queue_qos;
  278. union cvmx_pko_mem_queue_qos pko_mem_queue_qos_save[16];
  279. union cvmx_gmxx_tx_ovr_bp gmx_tx_ovr_bp;
  280. union cvmx_gmxx_tx_ovr_bp gmx_tx_ovr_bp_save;
  281. int i;
  282. /* Ignore speed sets in the simulator */
  283. if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM)
  284. return 0;
  285. /* Read the current settings so we know the current enable state */
  286. original_gmx_cfg.u64 =
  287. cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
  288. new_gmx_cfg = original_gmx_cfg;
  289. /* Disable the lowest level RX */
  290. cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface),
  291. cvmx_read_csr(CVMX_ASXX_RX_PRT_EN(interface)) &
  292. ~(1 << index));
  293. memset(pko_mem_queue_qos_save, 0, sizeof(pko_mem_queue_qos_save));
  294. /* Disable all queues so that TX should become idle */
  295. for (i = 0; i < cvmx_pko_get_num_queues(ipd_port); i++) {
  296. int queue = cvmx_pko_get_base_queue(ipd_port) + i;
  297. cvmx_write_csr(CVMX_PKO_REG_READ_IDX, queue);
  298. pko_mem_queue_qos.u64 = cvmx_read_csr(CVMX_PKO_MEM_QUEUE_QOS);
  299. pko_mem_queue_qos.s.pid = ipd_port;
  300. pko_mem_queue_qos.s.qid = queue;
  301. pko_mem_queue_qos_save[i] = pko_mem_queue_qos;
  302. pko_mem_queue_qos.s.qos_mask = 0;
  303. cvmx_write_csr(CVMX_PKO_MEM_QUEUE_QOS, pko_mem_queue_qos.u64);
  304. }
  305. /* Disable backpressure */
  306. gmx_tx_ovr_bp.u64 = cvmx_read_csr(CVMX_GMXX_TX_OVR_BP(interface));
  307. gmx_tx_ovr_bp_save = gmx_tx_ovr_bp;
  308. gmx_tx_ovr_bp.s.bp &= ~(1 << index);
  309. gmx_tx_ovr_bp.s.en |= 1 << index;
  310. cvmx_write_csr(CVMX_GMXX_TX_OVR_BP(interface), gmx_tx_ovr_bp.u64);
  311. cvmx_read_csr(CVMX_GMXX_TX_OVR_BP(interface));
  312. /*
  313. * Poll the GMX state machine waiting for it to become
  314. * idle. Preferably we should only change speed when it is
  315. * idle. If it doesn't become idle we will still do the speed
  316. * change, but there is a slight chance that GMX will
  317. * lockup.
  318. */
  319. cvmx_write_csr(CVMX_NPI_DBG_SELECT,
  320. interface * 0x800 + index * 0x100 + 0x880);
  321. CVMX_WAIT_FOR_FIELD64(CVMX_DBG_DATA, union cvmx_dbg_data, data & 7,
  322. ==, 0, 10000);
  323. CVMX_WAIT_FOR_FIELD64(CVMX_DBG_DATA, union cvmx_dbg_data, data & 0xf,
  324. ==, 0, 10000);
  325. /* Disable the port before we make any changes */
  326. new_gmx_cfg.s.en = 0;
  327. cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);
  328. cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
  329. /* Set full/half duplex */
  330. if (cvmx_octeon_is_pass1())
  331. /* Half duplex is broken for 38XX Pass 1 */
  332. new_gmx_cfg.s.duplex = 1;
  333. else if (!link_info.s.link_up)
  334. /* Force full duplex on down links */
  335. new_gmx_cfg.s.duplex = 1;
  336. else
  337. new_gmx_cfg.s.duplex = link_info.s.full_duplex;
  338. /* Set the link speed. Anything unknown is set to 1Gbps */
  339. if (link_info.s.speed == 10) {
  340. new_gmx_cfg.s.slottime = 0;
  341. new_gmx_cfg.s.speed = 0;
  342. } else if (link_info.s.speed == 100) {
  343. new_gmx_cfg.s.slottime = 0;
  344. new_gmx_cfg.s.speed = 0;
  345. } else {
  346. new_gmx_cfg.s.slottime = 1;
  347. new_gmx_cfg.s.speed = 1;
  348. }
  349. /* Adjust the clocks */
  350. if (link_info.s.speed == 10) {
  351. cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 50);
  352. cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x40);
  353. cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0);
  354. } else if (link_info.s.speed == 100) {
  355. cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 5);
  356. cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x40);
  357. cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0);
  358. } else {
  359. cvmx_write_csr(CVMX_GMXX_TXX_CLK(index, interface), 1);
  360. cvmx_write_csr(CVMX_GMXX_TXX_SLOT(index, interface), 0x200);
  361. cvmx_write_csr(CVMX_GMXX_TXX_BURST(index, interface), 0x2000);
  362. }
  363. if (OCTEON_IS_MODEL(OCTEON_CN30XX) || OCTEON_IS_MODEL(OCTEON_CN50XX)) {
  364. if ((link_info.s.speed == 10) || (link_info.s.speed == 100)) {
  365. union cvmx_gmxx_inf_mode mode;
  366. mode.u64 = cvmx_read_csr(CVMX_GMXX_INF_MODE(interface));
  367. /*
  368. * Port .en .type .p0mii Configuration
  369. * ---- --- ----- ------ -----------------------------------------
  370. * X 0 X X All links are disabled.
  371. * 0 1 X 0 Port 0 is RGMII
  372. * 0 1 X 1 Port 0 is MII
  373. * 1 1 0 X Ports 1 and 2 are configured as RGMII ports.
  374. * 1 1 1 X Port 1: GMII/MII; Port 2: disabled. GMII or
  375. * MII port is selected by GMX_PRT1_CFG[SPEED].
  376. */
  377. /* In MII mode, CLK_CNT = 1. */
  378. if (((index == 0) && (mode.s.p0mii == 1))
  379. || ((index != 0) && (mode.s.type == 1))) {
  380. cvmx_write_csr(CVMX_GMXX_TXX_CLK
  381. (index, interface), 1);
  382. }
  383. }
  384. }
  385. /* Do a read to make sure all setup stuff is complete */
  386. cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
  387. /* Save the new GMX setting without enabling the port */
  388. cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);
  389. /* Enable the lowest level RX */
  390. cvmx_write_csr(CVMX_ASXX_RX_PRT_EN(interface),
  391. cvmx_read_csr(CVMX_ASXX_RX_PRT_EN(interface)) | (1 <<
  392. index));
  393. /* Re-enable the TX path */
  394. for (i = 0; i < cvmx_pko_get_num_queues(ipd_port); i++) {
  395. int queue = cvmx_pko_get_base_queue(ipd_port) + i;
  396. cvmx_write_csr(CVMX_PKO_REG_READ_IDX, queue);
  397. cvmx_write_csr(CVMX_PKO_MEM_QUEUE_QOS,
  398. pko_mem_queue_qos_save[i].u64);
  399. }
  400. /* Restore backpressure */
  401. cvmx_write_csr(CVMX_GMXX_TX_OVR_BP(interface), gmx_tx_ovr_bp_save.u64);
  402. /* Restore the GMX enable state. Port config is complete */
  403. new_gmx_cfg.s.en = original_gmx_cfg.s.en;
  404. cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface), new_gmx_cfg.u64);
  405. return result;
  406. }