cvmx-mdio.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /***********************license start***************
  2. * Author: Cavium Networks
  3. *
  4. * Contact: support@caviumnetworks.com
  5. * This file is part of the OCTEON SDK
  6. *
  7. * Copyright (c) 2003-2008 Cavium Networks
  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. *
  29. * Interface to the SMI/MDIO hardware, including support for both IEEE 802.3
  30. * clause 22 and clause 45 operations.
  31. *
  32. */
  33. #ifndef __CVMX_MIO_H__
  34. #define __CVMX_MIO_H__
  35. #include "cvmx-smix-defs.h"
  36. /**
  37. * PHY register 0 from the 802.3 spec
  38. */
  39. #define CVMX_MDIO_PHY_REG_CONTROL 0
  40. typedef union {
  41. uint16_t u16;
  42. struct {
  43. uint16_t reset:1;
  44. uint16_t loopback:1;
  45. uint16_t speed_lsb:1;
  46. uint16_t autoneg_enable:1;
  47. uint16_t power_down:1;
  48. uint16_t isolate:1;
  49. uint16_t restart_autoneg:1;
  50. uint16_t duplex:1;
  51. uint16_t collision_test:1;
  52. uint16_t speed_msb:1;
  53. uint16_t unidirectional_enable:1;
  54. uint16_t reserved_0_4:5;
  55. } s;
  56. } cvmx_mdio_phy_reg_control_t;
  57. /**
  58. * PHY register 1 from the 802.3 spec
  59. */
  60. #define CVMX_MDIO_PHY_REG_STATUS 1
  61. typedef union {
  62. uint16_t u16;
  63. struct {
  64. uint16_t capable_100base_t4:1;
  65. uint16_t capable_100base_x_full:1;
  66. uint16_t capable_100base_x_half:1;
  67. uint16_t capable_10_full:1;
  68. uint16_t capable_10_half:1;
  69. uint16_t capable_100base_t2_full:1;
  70. uint16_t capable_100base_t2_half:1;
  71. uint16_t capable_extended_status:1;
  72. uint16_t capable_unidirectional:1;
  73. uint16_t capable_mf_preamble_suppression:1;
  74. uint16_t autoneg_complete:1;
  75. uint16_t remote_fault:1;
  76. uint16_t capable_autoneg:1;
  77. uint16_t link_status:1;
  78. uint16_t jabber_detect:1;
  79. uint16_t capable_extended_registers:1;
  80. } s;
  81. } cvmx_mdio_phy_reg_status_t;
  82. /**
  83. * PHY register 2 from the 802.3 spec
  84. */
  85. #define CVMX_MDIO_PHY_REG_ID1 2
  86. typedef union {
  87. uint16_t u16;
  88. struct {
  89. uint16_t oui_bits_3_18;
  90. } s;
  91. } cvmx_mdio_phy_reg_id1_t;
  92. /**
  93. * PHY register 3 from the 802.3 spec
  94. */
  95. #define CVMX_MDIO_PHY_REG_ID2 3
  96. typedef union {
  97. uint16_t u16;
  98. struct {
  99. uint16_t oui_bits_19_24:6;
  100. uint16_t model:6;
  101. uint16_t revision:4;
  102. } s;
  103. } cvmx_mdio_phy_reg_id2_t;
  104. /**
  105. * PHY register 4 from the 802.3 spec
  106. */
  107. #define CVMX_MDIO_PHY_REG_AUTONEG_ADVER 4
  108. typedef union {
  109. uint16_t u16;
  110. struct {
  111. uint16_t next_page:1;
  112. uint16_t reserved_14:1;
  113. uint16_t remote_fault:1;
  114. uint16_t reserved_12:1;
  115. uint16_t asymmetric_pause:1;
  116. uint16_t pause:1;
  117. uint16_t advert_100base_t4:1;
  118. uint16_t advert_100base_tx_full:1;
  119. uint16_t advert_100base_tx_half:1;
  120. uint16_t advert_10base_tx_full:1;
  121. uint16_t advert_10base_tx_half:1;
  122. uint16_t selector:5;
  123. } s;
  124. } cvmx_mdio_phy_reg_autoneg_adver_t;
  125. /**
  126. * PHY register 5 from the 802.3 spec
  127. */
  128. #define CVMX_MDIO_PHY_REG_LINK_PARTNER_ABILITY 5
  129. typedef union {
  130. uint16_t u16;
  131. struct {
  132. uint16_t next_page:1;
  133. uint16_t ack:1;
  134. uint16_t remote_fault:1;
  135. uint16_t reserved_12:1;
  136. uint16_t asymmetric_pause:1;
  137. uint16_t pause:1;
  138. uint16_t advert_100base_t4:1;
  139. uint16_t advert_100base_tx_full:1;
  140. uint16_t advert_100base_tx_half:1;
  141. uint16_t advert_10base_tx_full:1;
  142. uint16_t advert_10base_tx_half:1;
  143. uint16_t selector:5;
  144. } s;
  145. } cvmx_mdio_phy_reg_link_partner_ability_t;
  146. /**
  147. * PHY register 6 from the 802.3 spec
  148. */
  149. #define CVMX_MDIO_PHY_REG_AUTONEG_EXPANSION 6
  150. typedef union {
  151. uint16_t u16;
  152. struct {
  153. uint16_t reserved_5_15:11;
  154. uint16_t parallel_detection_fault:1;
  155. uint16_t link_partner_next_page_capable:1;
  156. uint16_t local_next_page_capable:1;
  157. uint16_t page_received:1;
  158. uint16_t link_partner_autoneg_capable:1;
  159. } s;
  160. } cvmx_mdio_phy_reg_autoneg_expansion_t;
  161. /**
  162. * PHY register 9 from the 802.3 spec
  163. */
  164. #define CVMX_MDIO_PHY_REG_CONTROL_1000 9
  165. typedef union {
  166. uint16_t u16;
  167. struct {
  168. uint16_t test_mode:3;
  169. uint16_t manual_master_slave:1;
  170. uint16_t master:1;
  171. uint16_t port_type:1;
  172. uint16_t advert_1000base_t_full:1;
  173. uint16_t advert_1000base_t_half:1;
  174. uint16_t reserved_0_7:8;
  175. } s;
  176. } cvmx_mdio_phy_reg_control_1000_t;
  177. /**
  178. * PHY register 10 from the 802.3 spec
  179. */
  180. #define CVMX_MDIO_PHY_REG_STATUS_1000 10
  181. typedef union {
  182. uint16_t u16;
  183. struct {
  184. uint16_t master_slave_fault:1;
  185. uint16_t is_master:1;
  186. uint16_t local_receiver_ok:1;
  187. uint16_t remote_receiver_ok:1;
  188. uint16_t remote_capable_1000base_t_full:1;
  189. uint16_t remote_capable_1000base_t_half:1;
  190. uint16_t reserved_8_9:2;
  191. uint16_t idle_error_count:8;
  192. } s;
  193. } cvmx_mdio_phy_reg_status_1000_t;
  194. /**
  195. * PHY register 15 from the 802.3 spec
  196. */
  197. #define CVMX_MDIO_PHY_REG_EXTENDED_STATUS 15
  198. typedef union {
  199. uint16_t u16;
  200. struct {
  201. uint16_t capable_1000base_x_full:1;
  202. uint16_t capable_1000base_x_half:1;
  203. uint16_t capable_1000base_t_full:1;
  204. uint16_t capable_1000base_t_half:1;
  205. uint16_t reserved_0_11:12;
  206. } s;
  207. } cvmx_mdio_phy_reg_extended_status_t;
  208. /**
  209. * PHY register 13 from the 802.3 spec
  210. */
  211. #define CVMX_MDIO_PHY_REG_MMD_CONTROL 13
  212. typedef union {
  213. uint16_t u16;
  214. struct {
  215. uint16_t function:2;
  216. uint16_t reserved_5_13:9;
  217. uint16_t devad:5;
  218. } s;
  219. } cvmx_mdio_phy_reg_mmd_control_t;
  220. /**
  221. * PHY register 14 from the 802.3 spec
  222. */
  223. #define CVMX_MDIO_PHY_REG_MMD_ADDRESS_DATA 14
  224. typedef union {
  225. uint16_t u16;
  226. struct {
  227. uint16_t address_data:16;
  228. } s;
  229. } cvmx_mdio_phy_reg_mmd_address_data_t;
  230. /* Operating request encodings. */
  231. #define MDIO_CLAUSE_22_WRITE 0
  232. #define MDIO_CLAUSE_22_READ 1
  233. #define MDIO_CLAUSE_45_ADDRESS 0
  234. #define MDIO_CLAUSE_45_WRITE 1
  235. #define MDIO_CLAUSE_45_READ_INC 2
  236. #define MDIO_CLAUSE_45_READ 3
  237. /* MMD identifiers, mostly for accessing devices within XENPAK modules. */
  238. #define CVMX_MMD_DEVICE_PMA_PMD 1
  239. #define CVMX_MMD_DEVICE_WIS 2
  240. #define CVMX_MMD_DEVICE_PCS 3
  241. #define CVMX_MMD_DEVICE_PHY_XS 4
  242. #define CVMX_MMD_DEVICE_DTS_XS 5
  243. #define CVMX_MMD_DEVICE_TC 6
  244. #define CVMX_MMD_DEVICE_CL22_EXT 29
  245. #define CVMX_MMD_DEVICE_VENDOR_1 30
  246. #define CVMX_MMD_DEVICE_VENDOR_2 31
  247. /* Helper function to put MDIO interface into clause 45 mode */
  248. static inline void __cvmx_mdio_set_clause45_mode(int bus_id)
  249. {
  250. union cvmx_smix_clk smi_clk;
  251. /* Put bus into clause 45 mode */
  252. smi_clk.u64 = cvmx_read_csr(CVMX_SMIX_CLK(bus_id));
  253. smi_clk.s.mode = 1;
  254. smi_clk.s.preamble = 1;
  255. cvmx_write_csr(CVMX_SMIX_CLK(bus_id), smi_clk.u64);
  256. }
  257. /* Helper function to put MDIO interface into clause 22 mode */
  258. static inline void __cvmx_mdio_set_clause22_mode(int bus_id)
  259. {
  260. union cvmx_smix_clk smi_clk;
  261. /* Put bus into clause 22 mode */
  262. smi_clk.u64 = cvmx_read_csr(CVMX_SMIX_CLK(bus_id));
  263. smi_clk.s.mode = 0;
  264. cvmx_write_csr(CVMX_SMIX_CLK(bus_id), smi_clk.u64);
  265. }
  266. /**
  267. * Perform an MII read. This function is used to read PHY
  268. * registers controlling auto negotiation.
  269. *
  270. * @bus_id: MDIO bus number. Zero on most chips, but some chips (ex CN56XX)
  271. * support multiple busses.
  272. * @phy_id: The MII phy id
  273. * @location: Register location to read
  274. *
  275. * Returns Result from the read or -1 on failure
  276. */
  277. static inline int cvmx_mdio_read(int bus_id, int phy_id, int location)
  278. {
  279. union cvmx_smix_cmd smi_cmd;
  280. union cvmx_smix_rd_dat smi_rd;
  281. int timeout = 1000;
  282. if (octeon_has_feature(OCTEON_FEATURE_MDIO_CLAUSE_45))
  283. __cvmx_mdio_set_clause22_mode(bus_id);
  284. smi_cmd.u64 = 0;
  285. smi_cmd.s.phy_op = MDIO_CLAUSE_22_READ;
  286. smi_cmd.s.phy_adr = phy_id;
  287. smi_cmd.s.reg_adr = location;
  288. cvmx_write_csr(CVMX_SMIX_CMD(bus_id), smi_cmd.u64);
  289. do {
  290. cvmx_wait(1000);
  291. smi_rd.u64 = cvmx_read_csr(CVMX_SMIX_RD_DAT(bus_id));
  292. } while (smi_rd.s.pending && timeout--);
  293. if (smi_rd.s.val)
  294. return smi_rd.s.dat;
  295. else
  296. return -1;
  297. }
  298. /**
  299. * Perform an MII write. This function is used to write PHY
  300. * registers controlling auto negotiation.
  301. *
  302. * @bus_id: MDIO bus number. Zero on most chips, but some chips (ex CN56XX)
  303. * support multiple busses.
  304. * @phy_id: The MII phy id
  305. * @location: Register location to write
  306. * @val: Value to write
  307. *
  308. * Returns -1 on error
  309. * 0 on success
  310. */
  311. static inline int cvmx_mdio_write(int bus_id, int phy_id, int location, int val)
  312. {
  313. union cvmx_smix_cmd smi_cmd;
  314. union cvmx_smix_wr_dat smi_wr;
  315. int timeout = 1000;
  316. if (octeon_has_feature(OCTEON_FEATURE_MDIO_CLAUSE_45))
  317. __cvmx_mdio_set_clause22_mode(bus_id);
  318. smi_wr.u64 = 0;
  319. smi_wr.s.dat = val;
  320. cvmx_write_csr(CVMX_SMIX_WR_DAT(bus_id), smi_wr.u64);
  321. smi_cmd.u64 = 0;
  322. smi_cmd.s.phy_op = MDIO_CLAUSE_22_WRITE;
  323. smi_cmd.s.phy_adr = phy_id;
  324. smi_cmd.s.reg_adr = location;
  325. cvmx_write_csr(CVMX_SMIX_CMD(bus_id), smi_cmd.u64);
  326. do {
  327. cvmx_wait(1000);
  328. smi_wr.u64 = cvmx_read_csr(CVMX_SMIX_WR_DAT(bus_id));
  329. } while (smi_wr.s.pending && --timeout);
  330. if (timeout <= 0)
  331. return -1;
  332. return 0;
  333. }
  334. /**
  335. * Perform an IEEE 802.3 clause 45 MII read. This function is used to
  336. * read PHY registers controlling auto negotiation.
  337. *
  338. * @bus_id: MDIO bus number. Zero on most chips, but some chips (ex CN56XX)
  339. * support multiple busses.
  340. * @phy_id: The MII phy id
  341. * @device: MDIO Managable Device (MMD) id
  342. * @location: Register location to read
  343. *
  344. * Returns Result from the read or -1 on failure
  345. */
  346. static inline int cvmx_mdio_45_read(int bus_id, int phy_id, int device,
  347. int location)
  348. {
  349. union cvmx_smix_cmd smi_cmd;
  350. union cvmx_smix_rd_dat smi_rd;
  351. union cvmx_smix_wr_dat smi_wr;
  352. int timeout = 1000;
  353. if (!octeon_has_feature(OCTEON_FEATURE_MDIO_CLAUSE_45))
  354. return -1;
  355. __cvmx_mdio_set_clause45_mode(bus_id);
  356. smi_wr.u64 = 0;
  357. smi_wr.s.dat = location;
  358. cvmx_write_csr(CVMX_SMIX_WR_DAT(bus_id), smi_wr.u64);
  359. smi_cmd.u64 = 0;
  360. smi_cmd.s.phy_op = MDIO_CLAUSE_45_ADDRESS;
  361. smi_cmd.s.phy_adr = phy_id;
  362. smi_cmd.s.reg_adr = device;
  363. cvmx_write_csr(CVMX_SMIX_CMD(bus_id), smi_cmd.u64);
  364. do {
  365. cvmx_wait(1000);
  366. smi_wr.u64 = cvmx_read_csr(CVMX_SMIX_WR_DAT(bus_id));
  367. } while (smi_wr.s.pending && --timeout);
  368. if (timeout <= 0) {
  369. cvmx_dprintf("cvmx_mdio_45_read: bus_id %d phy_id %2d "
  370. "device %2d register %2d TIME OUT(address)\n",
  371. bus_id, phy_id, device, location);
  372. return -1;
  373. }
  374. smi_cmd.u64 = 0;
  375. smi_cmd.s.phy_op = MDIO_CLAUSE_45_READ;
  376. smi_cmd.s.phy_adr = phy_id;
  377. smi_cmd.s.reg_adr = device;
  378. cvmx_write_csr(CVMX_SMIX_CMD(bus_id), smi_cmd.u64);
  379. do {
  380. cvmx_wait(1000);
  381. smi_rd.u64 = cvmx_read_csr(CVMX_SMIX_RD_DAT(bus_id));
  382. } while (smi_rd.s.pending && --timeout);
  383. if (timeout <= 0) {
  384. cvmx_dprintf("cvmx_mdio_45_read: bus_id %d phy_id %2d "
  385. "device %2d register %2d TIME OUT(data)\n",
  386. bus_id, phy_id, device, location);
  387. return -1;
  388. }
  389. if (smi_rd.s.val)
  390. return smi_rd.s.dat;
  391. else {
  392. cvmx_dprintf("cvmx_mdio_45_read: bus_id %d phy_id %2d "
  393. "device %2d register %2d INVALID READ\n",
  394. bus_id, phy_id, device, location);
  395. return -1;
  396. }
  397. }
  398. /**
  399. * Perform an IEEE 802.3 clause 45 MII write. This function is used to
  400. * write PHY registers controlling auto negotiation.
  401. *
  402. * @bus_id: MDIO bus number. Zero on most chips, but some chips (ex CN56XX)
  403. * support multiple busses.
  404. * @phy_id: The MII phy id
  405. * @device: MDIO Managable Device (MMD) id
  406. * @location: Register location to write
  407. * @val: Value to write
  408. *
  409. * Returns -1 on error
  410. * 0 on success
  411. */
  412. static inline int cvmx_mdio_45_write(int bus_id, int phy_id, int device,
  413. int location, int val)
  414. {
  415. union cvmx_smix_cmd smi_cmd;
  416. union cvmx_smix_wr_dat smi_wr;
  417. int timeout = 1000;
  418. if (!octeon_has_feature(OCTEON_FEATURE_MDIO_CLAUSE_45))
  419. return -1;
  420. __cvmx_mdio_set_clause45_mode(bus_id);
  421. smi_wr.u64 = 0;
  422. smi_wr.s.dat = location;
  423. cvmx_write_csr(CVMX_SMIX_WR_DAT(bus_id), smi_wr.u64);
  424. smi_cmd.u64 = 0;
  425. smi_cmd.s.phy_op = MDIO_CLAUSE_45_ADDRESS;
  426. smi_cmd.s.phy_adr = phy_id;
  427. smi_cmd.s.reg_adr = device;
  428. cvmx_write_csr(CVMX_SMIX_CMD(bus_id), smi_cmd.u64);
  429. do {
  430. cvmx_wait(1000);
  431. smi_wr.u64 = cvmx_read_csr(CVMX_SMIX_WR_DAT(bus_id));
  432. } while (smi_wr.s.pending && --timeout);
  433. if (timeout <= 0)
  434. return -1;
  435. smi_wr.u64 = 0;
  436. smi_wr.s.dat = val;
  437. cvmx_write_csr(CVMX_SMIX_WR_DAT(bus_id), smi_wr.u64);
  438. smi_cmd.u64 = 0;
  439. smi_cmd.s.phy_op = MDIO_CLAUSE_45_WRITE;
  440. smi_cmd.s.phy_adr = phy_id;
  441. smi_cmd.s.reg_adr = device;
  442. cvmx_write_csr(CVMX_SMIX_CMD(bus_id), smi_cmd.u64);
  443. do {
  444. cvmx_wait(1000);
  445. smi_wr.u64 = cvmx_read_csr(CVMX_SMIX_WR_DAT(bus_id));
  446. } while (smi_wr.s.pending && --timeout);
  447. if (timeout <= 0)
  448. return -1;
  449. return 0;
  450. }
  451. #endif