mdio-i2c.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MDIO I2C bridge
  4. *
  5. * Copyright (C) 2015-2016 Russell King
  6. * Copyright (C) 2021 Marek Behun
  7. *
  8. * Network PHYs can appear on I2C buses when they are part of SFP module.
  9. * This driver exposes these PHYs to the networking PHY code, allowing
  10. * our PHY drivers access to these PHYs, and so allowing configuration
  11. * of their settings.
  12. */
  13. #include <linux/i2c.h>
  14. #include <linux/mdio/mdio-i2c.h>
  15. #include <linux/phy.h>
  16. #include <linux/sfp.h>
  17. /*
  18. * I2C bus addresses 0x50 and 0x51 are normally an EEPROM, which is
  19. * specified to be present in SFP modules. These correspond with PHY
  20. * addresses 16 and 17. Disallow access to these "phy" addresses.
  21. */
  22. static bool i2c_mii_valid_phy_id(int phy_id)
  23. {
  24. return phy_id != 0x10 && phy_id != 0x11;
  25. }
  26. static unsigned int i2c_mii_phy_addr(int phy_id)
  27. {
  28. return phy_id + 0x40;
  29. }
  30. static int i2c_mii_read_default_c45(struct mii_bus *bus, int phy_id, int devad,
  31. int reg)
  32. {
  33. struct i2c_adapter *i2c = bus->priv;
  34. struct i2c_msg msgs[2];
  35. u8 addr[3], data[2], *p;
  36. int bus_addr, ret;
  37. if (!i2c_mii_valid_phy_id(phy_id))
  38. return 0xffff;
  39. p = addr;
  40. if (devad >= 0) {
  41. *p++ = 0x20 | devad;
  42. *p++ = reg >> 8;
  43. }
  44. *p++ = reg;
  45. bus_addr = i2c_mii_phy_addr(phy_id);
  46. msgs[0].addr = bus_addr;
  47. msgs[0].flags = 0;
  48. msgs[0].len = p - addr;
  49. msgs[0].buf = addr;
  50. msgs[1].addr = bus_addr;
  51. msgs[1].flags = I2C_M_RD;
  52. msgs[1].len = sizeof(data);
  53. msgs[1].buf = data;
  54. ret = i2c_transfer(i2c, msgs, ARRAY_SIZE(msgs));
  55. if (ret != ARRAY_SIZE(msgs))
  56. return 0xffff;
  57. return data[0] << 8 | data[1];
  58. }
  59. static int i2c_mii_write_default_c45(struct mii_bus *bus, int phy_id,
  60. int devad, int reg, u16 val)
  61. {
  62. struct i2c_adapter *i2c = bus->priv;
  63. struct i2c_msg msg;
  64. int ret;
  65. u8 data[5], *p;
  66. if (!i2c_mii_valid_phy_id(phy_id))
  67. return 0;
  68. p = data;
  69. if (devad >= 0) {
  70. *p++ = devad;
  71. *p++ = reg >> 8;
  72. }
  73. *p++ = reg;
  74. *p++ = val >> 8;
  75. *p++ = val;
  76. msg.addr = i2c_mii_phy_addr(phy_id);
  77. msg.flags = 0;
  78. msg.len = p - data;
  79. msg.buf = data;
  80. ret = i2c_transfer(i2c, &msg, 1);
  81. return ret < 0 ? ret : 0;
  82. }
  83. static int i2c_mii_read_default_c22(struct mii_bus *bus, int phy_id, int reg)
  84. {
  85. return i2c_mii_read_default_c45(bus, phy_id, -1, reg);
  86. }
  87. static int i2c_mii_write_default_c22(struct mii_bus *bus, int phy_id, int reg,
  88. u16 val)
  89. {
  90. return i2c_mii_write_default_c45(bus, phy_id, -1, reg, val);
  91. }
  92. static int smbus_byte_mii_read_default_c22(struct mii_bus *bus, int phy_id,
  93. int reg)
  94. {
  95. struct i2c_adapter *i2c = bus->priv;
  96. union i2c_smbus_data smbus_data;
  97. int val = 0, ret;
  98. if (!i2c_mii_valid_phy_id(phy_id))
  99. return 0;
  100. i2c_lock_bus(i2c, I2C_LOCK_SEGMENT);
  101. ret = __i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
  102. I2C_SMBUS_READ, reg,
  103. I2C_SMBUS_BYTE_DATA, &smbus_data);
  104. if (ret < 0)
  105. goto unlock;
  106. val = (smbus_data.byte & 0xff) << 8;
  107. ret = __i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
  108. I2C_SMBUS_READ, reg,
  109. I2C_SMBUS_BYTE_DATA, &smbus_data);
  110. unlock:
  111. i2c_unlock_bus(i2c, I2C_LOCK_SEGMENT);
  112. if (ret < 0)
  113. return ret;
  114. val |= smbus_data.byte & 0xff;
  115. return val;
  116. }
  117. static int smbus_byte_mii_write_default_c22(struct mii_bus *bus, int phy_id,
  118. int reg, u16 val)
  119. {
  120. struct i2c_adapter *i2c = bus->priv;
  121. union i2c_smbus_data smbus_data;
  122. int ret;
  123. if (!i2c_mii_valid_phy_id(phy_id))
  124. return 0;
  125. smbus_data.byte = (val & 0xff00) >> 8;
  126. i2c_lock_bus(i2c, I2C_LOCK_SEGMENT);
  127. ret = __i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
  128. I2C_SMBUS_WRITE, reg,
  129. I2C_SMBUS_BYTE_DATA, &smbus_data);
  130. if (ret < 0)
  131. goto unlock;
  132. smbus_data.byte = val & 0xff;
  133. ret = __i2c_smbus_xfer(i2c, i2c_mii_phy_addr(phy_id), 0,
  134. I2C_SMBUS_WRITE, reg,
  135. I2C_SMBUS_BYTE_DATA, &smbus_data);
  136. unlock:
  137. i2c_unlock_bus(i2c, I2C_LOCK_SEGMENT);
  138. return ret < 0 ? ret : 0;
  139. }
  140. /* RollBall SFPs do not access internal PHY via I2C address 0x56, but
  141. * instead via address 0x51, when SFP page is set to 0x03 and password to
  142. * 0xffffffff.
  143. *
  144. * address size contents description
  145. * ------- ---- -------- -----------
  146. * 0x80 1 CMD 0x01/0x02/0x04 for write/read/done
  147. * 0x81 1 DEV Clause 45 device
  148. * 0x82 2 REG Clause 45 register
  149. * 0x84 2 VAL Register value
  150. */
  151. #define ROLLBALL_PHY_I2C_ADDR 0x51
  152. #define ROLLBALL_PASSWORD (SFP_VSL + 3)
  153. #define ROLLBALL_CMD_ADDR 0x80
  154. #define ROLLBALL_DATA_ADDR 0x81
  155. #define ROLLBALL_CMD_WRITE 0x01
  156. #define ROLLBALL_CMD_READ 0x02
  157. #define ROLLBALL_CMD_DONE 0x04
  158. #define SFP_PAGE_ROLLBALL_MDIO 3
  159. static int __i2c_transfer_err(struct i2c_adapter *i2c, struct i2c_msg *msgs,
  160. int num)
  161. {
  162. int ret;
  163. ret = __i2c_transfer(i2c, msgs, num);
  164. if (ret < 0)
  165. return ret;
  166. else if (ret != num)
  167. return -EIO;
  168. else
  169. return 0;
  170. }
  171. static int __i2c_rollball_get_page(struct i2c_adapter *i2c, int bus_addr,
  172. u8 *page)
  173. {
  174. struct i2c_msg msgs[2];
  175. u8 addr = SFP_PAGE;
  176. msgs[0].addr = bus_addr;
  177. msgs[0].flags = 0;
  178. msgs[0].len = 1;
  179. msgs[0].buf = &addr;
  180. msgs[1].addr = bus_addr;
  181. msgs[1].flags = I2C_M_RD;
  182. msgs[1].len = 1;
  183. msgs[1].buf = page;
  184. return __i2c_transfer_err(i2c, msgs, 2);
  185. }
  186. static int __i2c_rollball_set_page(struct i2c_adapter *i2c, int bus_addr,
  187. u8 page)
  188. {
  189. struct i2c_msg msg;
  190. u8 buf[2];
  191. buf[0] = SFP_PAGE;
  192. buf[1] = page;
  193. msg.addr = bus_addr;
  194. msg.flags = 0;
  195. msg.len = 2;
  196. msg.buf = buf;
  197. return __i2c_transfer_err(i2c, &msg, 1);
  198. }
  199. /* In order to not interfere with other SFP code (which possibly may manipulate
  200. * SFP_PAGE), for every transfer we do this:
  201. * 1. lock the bus
  202. * 2. save content of SFP_PAGE
  203. * 3. set SFP_PAGE to 3
  204. * 4. do the transfer
  205. * 5. restore original SFP_PAGE
  206. * 6. unlock the bus
  207. * Note that one might think that steps 2 to 5 could be theoretically done all
  208. * in one call to i2c_transfer (by constructing msgs array in such a way), but
  209. * unfortunately tests show that this does not work :-( Changed SFP_PAGE does
  210. * not take into account until i2c_transfer() is done.
  211. */
  212. static int i2c_transfer_rollball(struct i2c_adapter *i2c,
  213. struct i2c_msg *msgs, int num)
  214. {
  215. int ret, main_err = 0;
  216. u8 saved_page;
  217. i2c_lock_bus(i2c, I2C_LOCK_SEGMENT);
  218. /* save original page */
  219. ret = __i2c_rollball_get_page(i2c, msgs->addr, &saved_page);
  220. if (ret)
  221. goto unlock;
  222. /* change to RollBall MDIO page */
  223. ret = __i2c_rollball_set_page(i2c, msgs->addr, SFP_PAGE_ROLLBALL_MDIO);
  224. if (ret)
  225. goto unlock;
  226. /* do the transfer; we try to restore original page if this fails */
  227. ret = __i2c_transfer_err(i2c, msgs, num);
  228. if (ret)
  229. main_err = ret;
  230. /* restore original page */
  231. ret = __i2c_rollball_set_page(i2c, msgs->addr, saved_page);
  232. unlock:
  233. i2c_unlock_bus(i2c, I2C_LOCK_SEGMENT);
  234. return main_err ? : ret;
  235. }
  236. static int i2c_rollball_mii_poll(struct mii_bus *bus, int bus_addr, u8 *buf,
  237. size_t len)
  238. {
  239. struct i2c_adapter *i2c = bus->priv;
  240. struct i2c_msg msgs[2];
  241. u8 cmd_addr, tmp, *res;
  242. int i, ret;
  243. cmd_addr = ROLLBALL_CMD_ADDR;
  244. res = buf ? buf : &tmp;
  245. len = buf ? len : 1;
  246. msgs[0].addr = bus_addr;
  247. msgs[0].flags = 0;
  248. msgs[0].len = 1;
  249. msgs[0].buf = &cmd_addr;
  250. msgs[1].addr = bus_addr;
  251. msgs[1].flags = I2C_M_RD;
  252. msgs[1].len = len;
  253. msgs[1].buf = res;
  254. /* By experiment it takes up to 70 ms to access a register for these
  255. * SFPs. Sleep 20ms between iterations and try 10 times.
  256. */
  257. i = 10;
  258. do {
  259. msleep(20);
  260. ret = i2c_transfer_rollball(i2c, msgs, ARRAY_SIZE(msgs));
  261. if (ret)
  262. return ret;
  263. if (*res == ROLLBALL_CMD_DONE)
  264. return 0;
  265. } while (i-- > 0);
  266. dev_dbg(&bus->dev, "poll timed out\n");
  267. return -ETIMEDOUT;
  268. }
  269. static int i2c_rollball_mii_cmd(struct mii_bus *bus, int bus_addr, u8 cmd,
  270. u8 *data, size_t len)
  271. {
  272. struct i2c_adapter *i2c = bus->priv;
  273. struct i2c_msg msgs[2];
  274. u8 cmdbuf[2];
  275. cmdbuf[0] = ROLLBALL_CMD_ADDR;
  276. cmdbuf[1] = cmd;
  277. msgs[0].addr = bus_addr;
  278. msgs[0].flags = 0;
  279. msgs[0].len = len;
  280. msgs[0].buf = data;
  281. msgs[1].addr = bus_addr;
  282. msgs[1].flags = 0;
  283. msgs[1].len = sizeof(cmdbuf);
  284. msgs[1].buf = cmdbuf;
  285. return i2c_transfer_rollball(i2c, msgs, ARRAY_SIZE(msgs));
  286. }
  287. static int i2c_mii_read_rollball(struct mii_bus *bus, int phy_id, int devad,
  288. int reg)
  289. {
  290. u8 buf[4], res[6];
  291. int bus_addr, ret;
  292. u16 val;
  293. bus_addr = i2c_mii_phy_addr(phy_id);
  294. if (bus_addr != ROLLBALL_PHY_I2C_ADDR)
  295. return 0xffff;
  296. buf[0] = ROLLBALL_DATA_ADDR;
  297. buf[1] = devad;
  298. buf[2] = (reg >> 8) & 0xff;
  299. buf[3] = reg & 0xff;
  300. ret = i2c_rollball_mii_cmd(bus, bus_addr, ROLLBALL_CMD_READ, buf,
  301. sizeof(buf));
  302. if (ret < 0)
  303. return ret;
  304. ret = i2c_rollball_mii_poll(bus, bus_addr, res, sizeof(res));
  305. if (ret == -ETIMEDOUT)
  306. return 0xffff;
  307. else if (ret < 0)
  308. return ret;
  309. val = res[4] << 8 | res[5];
  310. return val;
  311. }
  312. static int i2c_mii_write_rollball(struct mii_bus *bus, int phy_id, int devad,
  313. int reg, u16 val)
  314. {
  315. int bus_addr, ret;
  316. u8 buf[6];
  317. bus_addr = i2c_mii_phy_addr(phy_id);
  318. if (bus_addr != ROLLBALL_PHY_I2C_ADDR)
  319. return 0;
  320. buf[0] = ROLLBALL_DATA_ADDR;
  321. buf[1] = devad;
  322. buf[2] = (reg >> 8) & 0xff;
  323. buf[3] = reg & 0xff;
  324. buf[4] = val >> 8;
  325. buf[5] = val & 0xff;
  326. ret = i2c_rollball_mii_cmd(bus, bus_addr, ROLLBALL_CMD_WRITE, buf,
  327. sizeof(buf));
  328. if (ret < 0)
  329. return ret;
  330. ret = i2c_rollball_mii_poll(bus, bus_addr, NULL, 0);
  331. if (ret < 0)
  332. return ret;
  333. return 0;
  334. }
  335. static int i2c_mii_init_rollball(struct i2c_adapter *i2c)
  336. {
  337. struct i2c_msg msg;
  338. u8 pw[5];
  339. int ret;
  340. pw[0] = ROLLBALL_PASSWORD;
  341. pw[1] = 0xff;
  342. pw[2] = 0xff;
  343. pw[3] = 0xff;
  344. pw[4] = 0xff;
  345. msg.addr = ROLLBALL_PHY_I2C_ADDR;
  346. msg.flags = 0;
  347. msg.len = sizeof(pw);
  348. msg.buf = pw;
  349. ret = i2c_transfer(i2c, &msg, 1);
  350. if (ret < 0)
  351. return ret;
  352. else if (ret != 1)
  353. return -EIO;
  354. else
  355. return 0;
  356. }
  357. static bool mdio_i2c_check_functionality(struct i2c_adapter *i2c,
  358. enum mdio_i2c_proto protocol)
  359. {
  360. if (i2c_check_functionality(i2c, I2C_FUNC_I2C))
  361. return true;
  362. if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA) &&
  363. protocol == MDIO_I2C_MARVELL_C22)
  364. return true;
  365. return false;
  366. }
  367. struct mii_bus *mdio_i2c_alloc(struct device *parent, struct i2c_adapter *i2c,
  368. enum mdio_i2c_proto protocol)
  369. {
  370. struct mii_bus *mii;
  371. int ret;
  372. if (!mdio_i2c_check_functionality(i2c, protocol))
  373. return ERR_PTR(-EINVAL);
  374. mii = mdiobus_alloc();
  375. if (!mii)
  376. return ERR_PTR(-ENOMEM);
  377. snprintf(mii->id, MII_BUS_ID_SIZE, "i2c:%s", dev_name(parent));
  378. mii->parent = parent;
  379. mii->priv = i2c;
  380. /* Only use SMBus if we have no other choice */
  381. if (i2c_check_functionality(i2c, I2C_FUNC_SMBUS_BYTE_DATA) &&
  382. !i2c_check_functionality(i2c, I2C_FUNC_I2C)) {
  383. mii->read = smbus_byte_mii_read_default_c22;
  384. mii->write = smbus_byte_mii_write_default_c22;
  385. return mii;
  386. }
  387. switch (protocol) {
  388. case MDIO_I2C_ROLLBALL:
  389. ret = i2c_mii_init_rollball(i2c);
  390. if (ret < 0) {
  391. dev_err(parent,
  392. "Cannot initialize RollBall MDIO I2C protocol: %d\n",
  393. ret);
  394. mdiobus_free(mii);
  395. return ERR_PTR(ret);
  396. }
  397. mii->read_c45 = i2c_mii_read_rollball;
  398. mii->write_c45 = i2c_mii_write_rollball;
  399. break;
  400. default:
  401. mii->read = i2c_mii_read_default_c22;
  402. mii->write = i2c_mii_write_default_c22;
  403. mii->read_c45 = i2c_mii_read_default_c45;
  404. mii->write_c45 = i2c_mii_write_default_c45;
  405. break;
  406. }
  407. return mii;
  408. }
  409. EXPORT_SYMBOL_GPL(mdio_i2c_alloc);
  410. MODULE_AUTHOR("Russell King");
  411. MODULE_DESCRIPTION("MDIO I2C bridge library");
  412. MODULE_LICENSE("GPL v2");