phy_device.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. /* Framework for finding and configuring PHYs.
  2. * Also contains generic PHY driver
  3. *
  4. * Author: Andy Fleming
  5. *
  6. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/errno.h>
  18. #include <linux/unistd.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/mm.h>
  27. #include <linux/module.h>
  28. #include <linux/mii.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/phy.h>
  31. #include <linux/mdio.h>
  32. #include <linux/io.h>
  33. #include <linux/uaccess.h>
  34. #include <asm/irq.h>
  35. MODULE_DESCRIPTION("PHY library");
  36. MODULE_AUTHOR("Andy Fleming");
  37. MODULE_LICENSE("GPL");
  38. void phy_device_free(struct phy_device *phydev)
  39. {
  40. put_device(&phydev->dev);
  41. }
  42. EXPORT_SYMBOL(phy_device_free);
  43. static void phy_device_release(struct device *dev)
  44. {
  45. kfree(to_phy_device(dev));
  46. }
  47. enum genphy_driver {
  48. GENPHY_DRV_1G,
  49. GENPHY_DRV_10G,
  50. GENPHY_DRV_MAX
  51. };
  52. static struct phy_driver genphy_driver[GENPHY_DRV_MAX];
  53. static LIST_HEAD(phy_fixup_list);
  54. static DEFINE_MUTEX(phy_fixup_lock);
  55. /**
  56. * phy_register_fixup - creates a new phy_fixup and adds it to the list
  57. * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID)
  58. * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
  59. * It can also be PHY_ANY_UID
  60. * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
  61. * comparison
  62. * @run: The actual code to be run when a matching PHY is found
  63. */
  64. int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
  65. int (*run)(struct phy_device *))
  66. {
  67. struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
  68. if (!fixup)
  69. return -ENOMEM;
  70. strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
  71. fixup->phy_uid = phy_uid;
  72. fixup->phy_uid_mask = phy_uid_mask;
  73. fixup->run = run;
  74. mutex_lock(&phy_fixup_lock);
  75. list_add_tail(&fixup->list, &phy_fixup_list);
  76. mutex_unlock(&phy_fixup_lock);
  77. return 0;
  78. }
  79. EXPORT_SYMBOL(phy_register_fixup);
  80. /* Registers a fixup to be run on any PHY with the UID in phy_uid */
  81. int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
  82. int (*run)(struct phy_device *))
  83. {
  84. return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
  85. }
  86. EXPORT_SYMBOL(phy_register_fixup_for_uid);
  87. /* Registers a fixup to be run on the PHY with id string bus_id */
  88. int phy_register_fixup_for_id(const char *bus_id,
  89. int (*run)(struct phy_device *))
  90. {
  91. return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
  92. }
  93. EXPORT_SYMBOL(phy_register_fixup_for_id);
  94. /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
  95. * Fixups can be set to match any in one or more fields.
  96. */
  97. static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
  98. {
  99. if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0)
  100. if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
  101. return 0;
  102. if ((fixup->phy_uid & fixup->phy_uid_mask) !=
  103. (phydev->phy_id & fixup->phy_uid_mask))
  104. if (fixup->phy_uid != PHY_ANY_UID)
  105. return 0;
  106. return 1;
  107. }
  108. /* Runs any matching fixups for this phydev */
  109. static int phy_scan_fixups(struct phy_device *phydev)
  110. {
  111. struct phy_fixup *fixup;
  112. mutex_lock(&phy_fixup_lock);
  113. list_for_each_entry(fixup, &phy_fixup_list, list) {
  114. if (phy_needs_fixup(phydev, fixup)) {
  115. int err = fixup->run(phydev);
  116. if (err < 0) {
  117. mutex_unlock(&phy_fixup_lock);
  118. return err;
  119. }
  120. }
  121. }
  122. mutex_unlock(&phy_fixup_lock);
  123. return 0;
  124. }
  125. struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
  126. bool is_c45,
  127. struct phy_c45_device_ids *c45_ids)
  128. {
  129. struct phy_device *dev;
  130. /* We allocate the device, and initialize the default values */
  131. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  132. if (NULL == dev)
  133. return (struct phy_device *)PTR_ERR((void *)-ENOMEM);
  134. dev->dev.release = phy_device_release;
  135. dev->speed = 0;
  136. dev->duplex = -1;
  137. dev->pause = 0;
  138. dev->asym_pause = 0;
  139. dev->link = 1;
  140. dev->interface = PHY_INTERFACE_MODE_GMII;
  141. dev->autoneg = AUTONEG_ENABLE;
  142. dev->is_c45 = is_c45;
  143. dev->addr = addr;
  144. dev->phy_id = phy_id;
  145. if (c45_ids)
  146. dev->c45_ids = *c45_ids;
  147. dev->bus = bus;
  148. dev->dev.parent = bus->parent;
  149. dev->dev.bus = &mdio_bus_type;
  150. dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
  151. dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr);
  152. dev->state = PHY_DOWN;
  153. mutex_init(&dev->lock);
  154. INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
  155. INIT_WORK(&dev->phy_queue, phy_change);
  156. /* Request the appropriate module unconditionally; don't
  157. * bother trying to do so only if it isn't already loaded,
  158. * because that gets complicated. A hotplug event would have
  159. * done an unconditional modprobe anyway.
  160. * We don't do normal hotplug because it won't work for MDIO
  161. * -- because it relies on the device staying around for long
  162. * enough for the driver to get loaded. With MDIO, the NIC
  163. * driver will get bored and give up as soon as it finds that
  164. * there's no driver _already_ loaded.
  165. */
  166. request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
  167. device_initialize(&dev->dev);
  168. return dev;
  169. }
  170. EXPORT_SYMBOL(phy_device_create);
  171. /**
  172. * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
  173. * @bus: the target MII bus
  174. * @addr: PHY address on the MII bus
  175. * @phy_id: where to store the ID retrieved.
  176. * @c45_ids: where to store the c45 ID information.
  177. *
  178. * If the PHY devices-in-package appears to be valid, it and the
  179. * corresponding identifiers are stored in @c45_ids, zero is stored
  180. * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
  181. * zero on success.
  182. *
  183. */
  184. static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
  185. struct phy_c45_device_ids *c45_ids) {
  186. int phy_reg;
  187. int i, reg_addr;
  188. const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
  189. /* Find first non-zero Devices In package. Device
  190. * zero is reserved, so don't probe it.
  191. */
  192. for (i = 1;
  193. i < num_ids && c45_ids->devices_in_package == 0;
  194. i++) {
  195. reg_addr = MII_ADDR_C45 | i << 16 | 6;
  196. phy_reg = mdiobus_read(bus, addr, reg_addr);
  197. if (phy_reg < 0)
  198. return -EIO;
  199. c45_ids->devices_in_package = (phy_reg & 0xffff) << 16;
  200. reg_addr = MII_ADDR_C45 | i << 16 | 5;
  201. phy_reg = mdiobus_read(bus, addr, reg_addr);
  202. if (phy_reg < 0)
  203. return -EIO;
  204. c45_ids->devices_in_package |= (phy_reg & 0xffff);
  205. /* If mostly Fs, there is no device there,
  206. * let's get out of here.
  207. */
  208. if ((c45_ids->devices_in_package & 0x1fffffff) == 0x1fffffff) {
  209. *phy_id = 0xffffffff;
  210. return 0;
  211. }
  212. }
  213. /* Now probe Device Identifiers for each device present. */
  214. for (i = 1; i < num_ids; i++) {
  215. if (!(c45_ids->devices_in_package & (1 << i)))
  216. continue;
  217. reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
  218. phy_reg = mdiobus_read(bus, addr, reg_addr);
  219. if (phy_reg < 0)
  220. return -EIO;
  221. c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
  222. reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
  223. phy_reg = mdiobus_read(bus, addr, reg_addr);
  224. if (phy_reg < 0)
  225. return -EIO;
  226. c45_ids->device_ids[i] |= (phy_reg & 0xffff);
  227. }
  228. *phy_id = 0;
  229. return 0;
  230. }
  231. /**
  232. * get_phy_id - reads the specified addr for its ID.
  233. * @bus: the target MII bus
  234. * @addr: PHY address on the MII bus
  235. * @phy_id: where to store the ID retrieved.
  236. * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
  237. * @c45_ids: where to store the c45 ID information.
  238. *
  239. * Description: In the case of a 802.3-c22 PHY, reads the ID registers
  240. * of the PHY at @addr on the @bus, stores it in @phy_id and returns
  241. * zero on success.
  242. *
  243. * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
  244. * its return value is in turn returned.
  245. *
  246. */
  247. static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
  248. bool is_c45, struct phy_c45_device_ids *c45_ids)
  249. {
  250. int phy_reg;
  251. if (is_c45)
  252. return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
  253. /* Grab the bits from PHYIR1, and put them in the upper half */
  254. phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
  255. if (phy_reg < 0)
  256. return -EIO;
  257. *phy_id = (phy_reg & 0xffff) << 16;
  258. /* Grab the bits from PHYIR2, and put them in the lower half */
  259. phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
  260. if (phy_reg < 0)
  261. return -EIO;
  262. *phy_id |= (phy_reg & 0xffff);
  263. return 0;
  264. }
  265. /**
  266. * get_phy_device - reads the specified PHY device and returns its @phy_device
  267. * struct
  268. * @bus: the target MII bus
  269. * @addr: PHY address on the MII bus
  270. * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
  271. *
  272. * Description: Reads the ID registers of the PHY at @addr on the
  273. * @bus, then allocates and returns the phy_device to represent it.
  274. */
  275. struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
  276. {
  277. struct phy_c45_device_ids c45_ids = {0};
  278. u32 phy_id = 0;
  279. int r;
  280. r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
  281. if (r)
  282. return ERR_PTR(r);
  283. /* If the phy_id is mostly Fs, there is no device there */
  284. if ((phy_id & 0x1fffffff) == 0x1fffffff)
  285. return NULL;
  286. return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
  287. }
  288. EXPORT_SYMBOL(get_phy_device);
  289. /**
  290. * phy_device_register - Register the phy device on the MDIO bus
  291. * @phydev: phy_device structure to be added to the MDIO bus
  292. */
  293. int phy_device_register(struct phy_device *phydev)
  294. {
  295. int err;
  296. /* Don't register a phy if one is already registered at this address */
  297. if (phydev->bus->phy_map[phydev->addr])
  298. return -EINVAL;
  299. phydev->bus->phy_map[phydev->addr] = phydev;
  300. /* Run all of the fixups for this PHY */
  301. err = phy_init_hw(phydev);
  302. if (err) {
  303. pr_err("PHY %d failed to initialize\n", phydev->addr);
  304. goto out;
  305. }
  306. err = device_add(&phydev->dev);
  307. if (err) {
  308. pr_err("PHY %d failed to add\n", phydev->addr);
  309. goto out;
  310. }
  311. return 0;
  312. out:
  313. phydev->bus->phy_map[phydev->addr] = NULL;
  314. return err;
  315. }
  316. EXPORT_SYMBOL(phy_device_register);
  317. /**
  318. * phy_find_first - finds the first PHY device on the bus
  319. * @bus: the target MII bus
  320. */
  321. struct phy_device *phy_find_first(struct mii_bus *bus)
  322. {
  323. int addr;
  324. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  325. if (bus->phy_map[addr])
  326. return bus->phy_map[addr];
  327. }
  328. return NULL;
  329. }
  330. EXPORT_SYMBOL(phy_find_first);
  331. /**
  332. * phy_prepare_link - prepares the PHY layer to monitor link status
  333. * @phydev: target phy_device struct
  334. * @handler: callback function for link status change notifications
  335. *
  336. * Description: Tells the PHY infrastructure to handle the
  337. * gory details on monitoring link status (whether through
  338. * polling or an interrupt), and to call back to the
  339. * connected device driver when the link status changes.
  340. * If you want to monitor your own link state, don't call
  341. * this function.
  342. */
  343. static void phy_prepare_link(struct phy_device *phydev,
  344. void (*handler)(struct net_device *))
  345. {
  346. phydev->adjust_link = handler;
  347. }
  348. /**
  349. * phy_connect_direct - connect an ethernet device to a specific phy_device
  350. * @dev: the network device to connect
  351. * @phydev: the pointer to the phy device
  352. * @handler: callback function for state change notifications
  353. * @interface: PHY device's interface
  354. */
  355. int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
  356. void (*handler)(struct net_device *),
  357. phy_interface_t interface)
  358. {
  359. int rc;
  360. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  361. if (rc)
  362. return rc;
  363. phy_prepare_link(phydev, handler);
  364. phy_start_machine(phydev);
  365. if (phydev->irq > 0)
  366. phy_start_interrupts(phydev);
  367. return 0;
  368. }
  369. EXPORT_SYMBOL(phy_connect_direct);
  370. /**
  371. * phy_connect - connect an ethernet device to a PHY device
  372. * @dev: the network device to connect
  373. * @bus_id: the id string of the PHY device to connect
  374. * @handler: callback function for state change notifications
  375. * @interface: PHY device's interface
  376. *
  377. * Description: Convenience function for connecting ethernet
  378. * devices to PHY devices. The default behavior is for
  379. * the PHY infrastructure to handle everything, and only notify
  380. * the connected driver when the link status changes. If you
  381. * don't want, or can't use the provided functionality, you may
  382. * choose to call only the subset of functions which provide
  383. * the desired functionality.
  384. */
  385. struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
  386. void (*handler)(struct net_device *),
  387. phy_interface_t interface)
  388. {
  389. struct phy_device *phydev;
  390. struct device *d;
  391. int rc;
  392. /* Search the list of PHY devices on the mdio bus for the
  393. * PHY with the requested name
  394. */
  395. d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
  396. if (!d) {
  397. pr_err("PHY %s not found\n", bus_id);
  398. return ERR_PTR(-ENODEV);
  399. }
  400. phydev = to_phy_device(d);
  401. rc = phy_connect_direct(dev, phydev, handler, interface);
  402. if (rc)
  403. return ERR_PTR(rc);
  404. return phydev;
  405. }
  406. EXPORT_SYMBOL(phy_connect);
  407. /**
  408. * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
  409. * device
  410. * @phydev: target phy_device struct
  411. */
  412. void phy_disconnect(struct phy_device *phydev)
  413. {
  414. if (phydev->irq > 0)
  415. phy_stop_interrupts(phydev);
  416. phy_stop_machine(phydev);
  417. phydev->adjust_link = NULL;
  418. phy_detach(phydev);
  419. }
  420. EXPORT_SYMBOL(phy_disconnect);
  421. /**
  422. * phy_poll_reset - Safely wait until a PHY reset has properly completed
  423. * @phydev: The PHY device to poll
  424. *
  425. * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
  426. * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
  427. * register must be polled until the BMCR_RESET bit clears.
  428. *
  429. * Furthermore, any attempts to write to PHY registers may have no effect
  430. * or even generate MDIO bus errors until this is complete.
  431. *
  432. * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
  433. * standard and do not fully reset after the BMCR_RESET bit is set, and may
  434. * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
  435. * effort to support such broken PHYs, this function is separate from the
  436. * standard phy_init_hw() which will zero all the other bits in the BMCR
  437. * and reapply all driver-specific and board-specific fixups.
  438. */
  439. static int phy_poll_reset(struct phy_device *phydev)
  440. {
  441. /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
  442. unsigned int retries = 12;
  443. int ret;
  444. do {
  445. msleep(50);
  446. ret = phy_read(phydev, MII_BMCR);
  447. if (ret < 0)
  448. return ret;
  449. } while (ret & BMCR_RESET && --retries);
  450. if (ret & BMCR_RESET)
  451. return -ETIMEDOUT;
  452. /* Some chips (smsc911x) may still need up to another 1ms after the
  453. * BMCR_RESET bit is cleared before they are usable.
  454. */
  455. msleep(1);
  456. return 0;
  457. }
  458. int phy_init_hw(struct phy_device *phydev)
  459. {
  460. int ret;
  461. if (!phydev->drv || !phydev->drv->config_init)
  462. return 0;
  463. ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
  464. if (ret < 0)
  465. return ret;
  466. ret = phy_poll_reset(phydev);
  467. if (ret < 0)
  468. return ret;
  469. ret = phy_scan_fixups(phydev);
  470. if (ret < 0)
  471. return ret;
  472. return phydev->drv->config_init(phydev);
  473. }
  474. EXPORT_SYMBOL(phy_init_hw);
  475. /**
  476. * phy_attach_direct - attach a network device to a given PHY device pointer
  477. * @dev: network device to attach
  478. * @phydev: Pointer to phy_device to attach
  479. * @flags: PHY device's dev_flags
  480. * @interface: PHY device's interface
  481. *
  482. * Description: Called by drivers to attach to a particular PHY
  483. * device. The phy_device is found, and properly hooked up
  484. * to the phy_driver. If no driver is attached, then a
  485. * generic driver is used. The phy_device is given a ptr to
  486. * the attaching device, and given a callback for link status
  487. * change. The phy_device is returned to the attaching driver.
  488. */
  489. int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  490. u32 flags, phy_interface_t interface)
  491. {
  492. struct device *d = &phydev->dev;
  493. int err;
  494. /* Assume that if there is no driver, that it doesn't
  495. * exist, and we should use the genphy driver.
  496. */
  497. if (NULL == d->driver) {
  498. if (phydev->is_c45)
  499. d->driver = &genphy_driver[GENPHY_DRV_10G].driver;
  500. else
  501. d->driver = &genphy_driver[GENPHY_DRV_1G].driver;
  502. err = d->driver->probe(d);
  503. if (err >= 0)
  504. err = device_bind_driver(d);
  505. if (err)
  506. return err;
  507. }
  508. if (phydev->attached_dev) {
  509. dev_err(&dev->dev, "PHY already attached\n");
  510. return -EBUSY;
  511. }
  512. phydev->attached_dev = dev;
  513. dev->phydev = phydev;
  514. phydev->dev_flags = flags;
  515. phydev->interface = interface;
  516. phydev->state = PHY_READY;
  517. /* Do initial configuration here, now that
  518. * we have certain key parameters
  519. * (dev_flags and interface)
  520. */
  521. err = phy_init_hw(phydev);
  522. if (err)
  523. phy_detach(phydev);
  524. phy_resume(phydev);
  525. return err;
  526. }
  527. EXPORT_SYMBOL(phy_attach_direct);
  528. /**
  529. * phy_attach - attach a network device to a particular PHY device
  530. * @dev: network device to attach
  531. * @bus_id: Bus ID of PHY device to attach
  532. * @interface: PHY device's interface
  533. *
  534. * Description: Same as phy_attach_direct() except that a PHY bus_id
  535. * string is passed instead of a pointer to a struct phy_device.
  536. */
  537. struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
  538. phy_interface_t interface)
  539. {
  540. struct bus_type *bus = &mdio_bus_type;
  541. struct phy_device *phydev;
  542. struct device *d;
  543. int rc;
  544. /* Search the list of PHY devices on the mdio bus for the
  545. * PHY with the requested name
  546. */
  547. d = bus_find_device_by_name(bus, NULL, bus_id);
  548. if (!d) {
  549. pr_err("PHY %s not found\n", bus_id);
  550. return ERR_PTR(-ENODEV);
  551. }
  552. phydev = to_phy_device(d);
  553. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  554. if (rc)
  555. return ERR_PTR(rc);
  556. return phydev;
  557. }
  558. EXPORT_SYMBOL(phy_attach);
  559. /**
  560. * phy_detach - detach a PHY device from its network device
  561. * @phydev: target phy_device struct
  562. */
  563. void phy_detach(struct phy_device *phydev)
  564. {
  565. int i;
  566. phydev->attached_dev->phydev = NULL;
  567. phydev->attached_dev = NULL;
  568. phy_suspend(phydev);
  569. /* If the device had no specific driver before (i.e. - it
  570. * was using the generic driver), we unbind the device
  571. * from the generic driver so that there's a chance a
  572. * real driver could be loaded
  573. */
  574. for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) {
  575. if (phydev->dev.driver == &genphy_driver[i].driver) {
  576. device_release_driver(&phydev->dev);
  577. break;
  578. }
  579. }
  580. }
  581. EXPORT_SYMBOL(phy_detach);
  582. int phy_suspend(struct phy_device *phydev)
  583. {
  584. struct phy_driver *phydrv = to_phy_driver(phydev->dev.driver);
  585. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  586. /* If the device has WOL enabled, we cannot suspend the PHY */
  587. phy_ethtool_get_wol(phydev, &wol);
  588. if (wol.wolopts)
  589. return -EBUSY;
  590. if (phydrv->suspend)
  591. return phydrv->suspend(phydev);
  592. return 0;
  593. }
  594. int phy_resume(struct phy_device *phydev)
  595. {
  596. struct phy_driver *phydrv = to_phy_driver(phydev->dev.driver);
  597. if (phydrv->resume)
  598. return phydrv->resume(phydev);
  599. return 0;
  600. }
  601. /* Generic PHY support and helper functions */
  602. /**
  603. * genphy_config_advert - sanitize and advertise auto-negotiation parameters
  604. * @phydev: target phy_device struct
  605. *
  606. * Description: Writes MII_ADVERTISE with the appropriate values,
  607. * after sanitizing the values to make sure we only advertise
  608. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  609. * hasn't changed, and > 0 if it has changed.
  610. */
  611. static int genphy_config_advert(struct phy_device *phydev)
  612. {
  613. u32 advertise;
  614. int oldadv, adv, bmsr;
  615. int err, changed = 0;
  616. /* Only allow advertising what this PHY supports */
  617. phydev->advertising &= phydev->supported;
  618. advertise = phydev->advertising;
  619. /* Setup standard advertisement */
  620. adv = phy_read(phydev, MII_ADVERTISE);
  621. if (adv < 0)
  622. return adv;
  623. oldadv = adv;
  624. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  625. ADVERTISE_PAUSE_ASYM);
  626. adv |= ethtool_adv_to_mii_adv_t(advertise);
  627. if (adv != oldadv) {
  628. err = phy_write(phydev, MII_ADVERTISE, adv);
  629. if (err < 0)
  630. return err;
  631. changed = 1;
  632. }
  633. bmsr = phy_read(phydev, MII_BMSR);
  634. if (bmsr < 0)
  635. return bmsr;
  636. /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
  637. * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
  638. * logical 1.
  639. */
  640. if (!(bmsr & BMSR_ESTATEN))
  641. return changed;
  642. /* Configure gigabit if it's supported */
  643. adv = phy_read(phydev, MII_CTRL1000);
  644. if (adv < 0)
  645. return adv;
  646. oldadv = adv;
  647. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  648. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  649. SUPPORTED_1000baseT_Full)) {
  650. adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
  651. if (adv != oldadv)
  652. changed = 1;
  653. }
  654. err = phy_write(phydev, MII_CTRL1000, adv);
  655. if (err < 0)
  656. return err;
  657. return changed;
  658. }
  659. /**
  660. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  661. * @phydev: target phy_device struct
  662. *
  663. * Description: Configures MII_BMCR to force speed/duplex
  664. * to the values in phydev. Assumes that the values are valid.
  665. * Please see phy_sanitize_settings().
  666. */
  667. int genphy_setup_forced(struct phy_device *phydev)
  668. {
  669. int ctl = 0;
  670. phydev->pause = 0;
  671. phydev->asym_pause = 0;
  672. if (SPEED_1000 == phydev->speed)
  673. ctl |= BMCR_SPEED1000;
  674. else if (SPEED_100 == phydev->speed)
  675. ctl |= BMCR_SPEED100;
  676. if (DUPLEX_FULL == phydev->duplex)
  677. ctl |= BMCR_FULLDPLX;
  678. return phy_write(phydev, MII_BMCR, ctl);
  679. }
  680. EXPORT_SYMBOL(genphy_setup_forced);
  681. /**
  682. * genphy_restart_aneg - Enable and Restart Autonegotiation
  683. * @phydev: target phy_device struct
  684. */
  685. int genphy_restart_aneg(struct phy_device *phydev)
  686. {
  687. int ctl = phy_read(phydev, MII_BMCR);
  688. if (ctl < 0)
  689. return ctl;
  690. ctl |= BMCR_ANENABLE | BMCR_ANRESTART;
  691. /* Don't isolate the PHY if we're negotiating */
  692. ctl &= ~BMCR_ISOLATE;
  693. return phy_write(phydev, MII_BMCR, ctl);
  694. }
  695. EXPORT_SYMBOL(genphy_restart_aneg);
  696. /**
  697. * genphy_config_aneg - restart auto-negotiation or write BMCR
  698. * @phydev: target phy_device struct
  699. *
  700. * Description: If auto-negotiation is enabled, we configure the
  701. * advertising, and then restart auto-negotiation. If it is not
  702. * enabled, then we write the BMCR.
  703. */
  704. int genphy_config_aneg(struct phy_device *phydev)
  705. {
  706. int result;
  707. if (AUTONEG_ENABLE != phydev->autoneg)
  708. return genphy_setup_forced(phydev);
  709. result = genphy_config_advert(phydev);
  710. if (result < 0) /* error */
  711. return result;
  712. if (result == 0) {
  713. /* Advertisement hasn't changed, but maybe aneg was never on to
  714. * begin with? Or maybe phy was isolated?
  715. */
  716. int ctl = phy_read(phydev, MII_BMCR);
  717. if (ctl < 0)
  718. return ctl;
  719. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  720. result = 1; /* do restart aneg */
  721. }
  722. /* Only restart aneg if we are advertising something different
  723. * than we were before.
  724. */
  725. if (result > 0)
  726. result = genphy_restart_aneg(phydev);
  727. return result;
  728. }
  729. EXPORT_SYMBOL(genphy_config_aneg);
  730. static int gen10g_config_aneg(struct phy_device *phydev)
  731. {
  732. return 0;
  733. }
  734. /**
  735. * genphy_update_link - update link status in @phydev
  736. * @phydev: target phy_device struct
  737. *
  738. * Description: Update the value in phydev->link to reflect the
  739. * current link value. In order to do this, we need to read
  740. * the status register twice, keeping the second value.
  741. */
  742. int genphy_update_link(struct phy_device *phydev)
  743. {
  744. int status;
  745. /* Do a fake read */
  746. status = phy_read(phydev, MII_BMSR);
  747. if (status < 0)
  748. return status;
  749. /* Read link and autonegotiation status */
  750. status = phy_read(phydev, MII_BMSR);
  751. if (status < 0)
  752. return status;
  753. if ((status & BMSR_LSTATUS) == 0)
  754. phydev->link = 0;
  755. else
  756. phydev->link = 1;
  757. return 0;
  758. }
  759. EXPORT_SYMBOL(genphy_update_link);
  760. /**
  761. * genphy_read_status - check the link status and update current link state
  762. * @phydev: target phy_device struct
  763. *
  764. * Description: Check the link, then figure out the current state
  765. * by comparing what we advertise with what the link partner
  766. * advertises. Start by checking the gigabit possibilities,
  767. * then move on to 10/100.
  768. */
  769. int genphy_read_status(struct phy_device *phydev)
  770. {
  771. int adv;
  772. int err;
  773. int lpa;
  774. int lpagb = 0;
  775. int common_adv;
  776. int common_adv_gb = 0;
  777. /* Update the link, but return if there was an error */
  778. err = genphy_update_link(phydev);
  779. if (err)
  780. return err;
  781. phydev->lp_advertising = 0;
  782. if (AUTONEG_ENABLE == phydev->autoneg) {
  783. if (phydev->supported & (SUPPORTED_1000baseT_Half
  784. | SUPPORTED_1000baseT_Full)) {
  785. lpagb = phy_read(phydev, MII_STAT1000);
  786. if (lpagb < 0)
  787. return lpagb;
  788. adv = phy_read(phydev, MII_CTRL1000);
  789. if (adv < 0)
  790. return adv;
  791. phydev->lp_advertising =
  792. mii_stat1000_to_ethtool_lpa_t(lpagb);
  793. common_adv_gb = lpagb & adv << 2;
  794. }
  795. lpa = phy_read(phydev, MII_LPA);
  796. if (lpa < 0)
  797. return lpa;
  798. phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
  799. adv = phy_read(phydev, MII_ADVERTISE);
  800. if (adv < 0)
  801. return adv;
  802. common_adv = lpa & adv;
  803. phydev->speed = SPEED_10;
  804. phydev->duplex = DUPLEX_HALF;
  805. phydev->pause = 0;
  806. phydev->asym_pause = 0;
  807. if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
  808. phydev->speed = SPEED_1000;
  809. if (common_adv_gb & LPA_1000FULL)
  810. phydev->duplex = DUPLEX_FULL;
  811. } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
  812. phydev->speed = SPEED_100;
  813. if (common_adv & LPA_100FULL)
  814. phydev->duplex = DUPLEX_FULL;
  815. } else
  816. if (common_adv & LPA_10FULL)
  817. phydev->duplex = DUPLEX_FULL;
  818. if (phydev->duplex == DUPLEX_FULL) {
  819. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  820. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  821. }
  822. } else {
  823. int bmcr = phy_read(phydev, MII_BMCR);
  824. if (bmcr < 0)
  825. return bmcr;
  826. if (bmcr & BMCR_FULLDPLX)
  827. phydev->duplex = DUPLEX_FULL;
  828. else
  829. phydev->duplex = DUPLEX_HALF;
  830. if (bmcr & BMCR_SPEED1000)
  831. phydev->speed = SPEED_1000;
  832. else if (bmcr & BMCR_SPEED100)
  833. phydev->speed = SPEED_100;
  834. else
  835. phydev->speed = SPEED_10;
  836. phydev->pause = 0;
  837. phydev->asym_pause = 0;
  838. }
  839. return 0;
  840. }
  841. EXPORT_SYMBOL(genphy_read_status);
  842. static int gen10g_read_status(struct phy_device *phydev)
  843. {
  844. int devad, reg;
  845. u32 mmd_mask = phydev->c45_ids.devices_in_package;
  846. phydev->link = 1;
  847. /* For now just lie and say it's 10G all the time */
  848. phydev->speed = SPEED_10000;
  849. phydev->duplex = DUPLEX_FULL;
  850. for (devad = 0; mmd_mask; devad++, mmd_mask = mmd_mask >> 1) {
  851. if (!(mmd_mask & 1))
  852. continue;
  853. /* Read twice because link state is latched and a
  854. * read moves the current state into the register
  855. */
  856. phy_read_mmd(phydev, devad, MDIO_STAT1);
  857. reg = phy_read_mmd(phydev, devad, MDIO_STAT1);
  858. if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
  859. phydev->link = 0;
  860. }
  861. return 0;
  862. }
  863. static int genphy_config_init(struct phy_device *phydev)
  864. {
  865. int val;
  866. u32 features;
  867. /* For now, I'll claim that the generic driver supports
  868. * all possible port types
  869. */
  870. features = (SUPPORTED_TP | SUPPORTED_MII
  871. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  872. SUPPORTED_BNC);
  873. /* Do we support autonegotiation? */
  874. val = phy_read(phydev, MII_BMSR);
  875. if (val < 0)
  876. return val;
  877. if (val & BMSR_ANEGCAPABLE)
  878. features |= SUPPORTED_Autoneg;
  879. if (val & BMSR_100FULL)
  880. features |= SUPPORTED_100baseT_Full;
  881. if (val & BMSR_100HALF)
  882. features |= SUPPORTED_100baseT_Half;
  883. if (val & BMSR_10FULL)
  884. features |= SUPPORTED_10baseT_Full;
  885. if (val & BMSR_10HALF)
  886. features |= SUPPORTED_10baseT_Half;
  887. if (val & BMSR_ESTATEN) {
  888. val = phy_read(phydev, MII_ESTATUS);
  889. if (val < 0)
  890. return val;
  891. if (val & ESTATUS_1000_TFULL)
  892. features |= SUPPORTED_1000baseT_Full;
  893. if (val & ESTATUS_1000_THALF)
  894. features |= SUPPORTED_1000baseT_Half;
  895. }
  896. phydev->supported = features;
  897. phydev->advertising = features;
  898. return 0;
  899. }
  900. static int gen10g_config_init(struct phy_device *phydev)
  901. {
  902. /* Temporarily just say we support everything */
  903. phydev->supported = SUPPORTED_10000baseT_Full;
  904. phydev->advertising = SUPPORTED_10000baseT_Full;
  905. return 0;
  906. }
  907. int genphy_suspend(struct phy_device *phydev)
  908. {
  909. int value;
  910. mutex_lock(&phydev->lock);
  911. value = phy_read(phydev, MII_BMCR);
  912. phy_write(phydev, MII_BMCR, value | BMCR_PDOWN);
  913. mutex_unlock(&phydev->lock);
  914. return 0;
  915. }
  916. EXPORT_SYMBOL(genphy_suspend);
  917. static int gen10g_suspend(struct phy_device *phydev)
  918. {
  919. return 0;
  920. }
  921. int genphy_resume(struct phy_device *phydev)
  922. {
  923. int value;
  924. mutex_lock(&phydev->lock);
  925. value = phy_read(phydev, MII_BMCR);
  926. phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN);
  927. mutex_unlock(&phydev->lock);
  928. return 0;
  929. }
  930. EXPORT_SYMBOL(genphy_resume);
  931. static int gen10g_resume(struct phy_device *phydev)
  932. {
  933. return 0;
  934. }
  935. /**
  936. * phy_probe - probe and init a PHY device
  937. * @dev: device to probe and init
  938. *
  939. * Description: Take care of setting up the phy_device structure,
  940. * set the state to READY (the driver's init function should
  941. * set it to STARTING if needed).
  942. */
  943. static int phy_probe(struct device *dev)
  944. {
  945. struct phy_device *phydev = to_phy_device(dev);
  946. struct device_driver *drv = phydev->dev.driver;
  947. struct phy_driver *phydrv = to_phy_driver(drv);
  948. int err = 0;
  949. phydev->drv = phydrv;
  950. /* Disable the interrupt if the PHY doesn't support it
  951. * but the interrupt is still a valid one
  952. */
  953. if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
  954. phy_interrupt_is_valid(phydev))
  955. phydev->irq = PHY_POLL;
  956. if (phydrv->flags & PHY_IS_INTERNAL)
  957. phydev->is_internal = true;
  958. mutex_lock(&phydev->lock);
  959. /* Start out supporting everything. Eventually,
  960. * a controller will attach, and may modify one
  961. * or both of these values
  962. */
  963. phydev->supported = phydrv->features;
  964. phydev->advertising = phydrv->features;
  965. /* Set the state to READY by default */
  966. phydev->state = PHY_READY;
  967. if (phydev->drv->probe)
  968. err = phydev->drv->probe(phydev);
  969. mutex_unlock(&phydev->lock);
  970. return err;
  971. }
  972. static int phy_remove(struct device *dev)
  973. {
  974. struct phy_device *phydev = to_phy_device(dev);
  975. mutex_lock(&phydev->lock);
  976. phydev->state = PHY_DOWN;
  977. mutex_unlock(&phydev->lock);
  978. if (phydev->drv->remove)
  979. phydev->drv->remove(phydev);
  980. phydev->drv = NULL;
  981. return 0;
  982. }
  983. /**
  984. * phy_driver_register - register a phy_driver with the PHY layer
  985. * @new_driver: new phy_driver to register
  986. */
  987. int phy_driver_register(struct phy_driver *new_driver)
  988. {
  989. int retval;
  990. new_driver->driver.name = new_driver->name;
  991. new_driver->driver.bus = &mdio_bus_type;
  992. new_driver->driver.probe = phy_probe;
  993. new_driver->driver.remove = phy_remove;
  994. retval = driver_register(&new_driver->driver);
  995. if (retval) {
  996. pr_err("%s: Error %d in registering driver\n",
  997. new_driver->name, retval);
  998. return retval;
  999. }
  1000. pr_debug("%s: Registered new driver\n", new_driver->name);
  1001. return 0;
  1002. }
  1003. EXPORT_SYMBOL(phy_driver_register);
  1004. int phy_drivers_register(struct phy_driver *new_driver, int n)
  1005. {
  1006. int i, ret = 0;
  1007. for (i = 0; i < n; i++) {
  1008. ret = phy_driver_register(new_driver + i);
  1009. if (ret) {
  1010. while (i-- > 0)
  1011. phy_driver_unregister(new_driver + i);
  1012. break;
  1013. }
  1014. }
  1015. return ret;
  1016. }
  1017. EXPORT_SYMBOL(phy_drivers_register);
  1018. void phy_driver_unregister(struct phy_driver *drv)
  1019. {
  1020. driver_unregister(&drv->driver);
  1021. }
  1022. EXPORT_SYMBOL(phy_driver_unregister);
  1023. void phy_drivers_unregister(struct phy_driver *drv, int n)
  1024. {
  1025. int i;
  1026. for (i = 0; i < n; i++)
  1027. phy_driver_unregister(drv + i);
  1028. }
  1029. EXPORT_SYMBOL(phy_drivers_unregister);
  1030. static struct phy_driver genphy_driver[] = {
  1031. {
  1032. .phy_id = 0xffffffff,
  1033. .phy_id_mask = 0xffffffff,
  1034. .name = "Generic PHY",
  1035. .config_init = genphy_config_init,
  1036. .features = 0,
  1037. .config_aneg = genphy_config_aneg,
  1038. .read_status = genphy_read_status,
  1039. .suspend = genphy_suspend,
  1040. .resume = genphy_resume,
  1041. .driver = { .owner = THIS_MODULE, },
  1042. }, {
  1043. .phy_id = 0xffffffff,
  1044. .phy_id_mask = 0xffffffff,
  1045. .name = "Generic 10G PHY",
  1046. .config_init = gen10g_config_init,
  1047. .features = 0,
  1048. .config_aneg = gen10g_config_aneg,
  1049. .read_status = gen10g_read_status,
  1050. .suspend = gen10g_suspend,
  1051. .resume = gen10g_resume,
  1052. .driver = {.owner = THIS_MODULE, },
  1053. } };
  1054. static int __init phy_init(void)
  1055. {
  1056. int rc;
  1057. rc = mdio_bus_init();
  1058. if (rc)
  1059. return rc;
  1060. rc = phy_drivers_register(genphy_driver,
  1061. ARRAY_SIZE(genphy_driver));
  1062. if (rc)
  1063. mdio_bus_exit();
  1064. return rc;
  1065. }
  1066. static void __exit phy_exit(void)
  1067. {
  1068. phy_drivers_unregister(genphy_driver,
  1069. ARRAY_SIZE(genphy_driver));
  1070. mdio_bus_exit();
  1071. }
  1072. subsys_initcall(phy_init);
  1073. module_exit(phy_exit);