fixed.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * drivers/net/phy/fixed.c
  3. *
  4. * Driver for fixed PHYs, when transceiver is able to operate in one fixed mode.
  5. *
  6. * Author: Vitaly Bordug
  7. *
  8. * Copyright (c) 2006 MontaVista Software, Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/string.h>
  19. #include <linux/errno.h>
  20. #include <linux/unistd.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/mm.h>
  30. #include <linux/module.h>
  31. #include <linux/mii.h>
  32. #include <linux/ethtool.h>
  33. #include <linux/phy.h>
  34. #include <asm/io.h>
  35. #include <asm/irq.h>
  36. #include <asm/uaccess.h>
  37. #define MII_REGS_NUM 7
  38. /*
  39. The idea is to emulate normal phy behavior by responding with
  40. pre-defined values to mii BMCR read, so that read_status hook could
  41. take all the needed info.
  42. */
  43. struct fixed_phy_status {
  44. u8 link;
  45. u16 speed;
  46. u8 duplex;
  47. };
  48. /*-----------------------------------------------------------------------------
  49. * Private information hoder for mii_bus
  50. *-----------------------------------------------------------------------------*/
  51. struct fixed_info {
  52. u16 *regs;
  53. u8 regs_num;
  54. struct fixed_phy_status phy_status;
  55. struct phy_device *phydev; /* pointer to the container */
  56. /* link & speed cb */
  57. int(*link_update)(struct net_device*, struct fixed_phy_status*);
  58. };
  59. /*-----------------------------------------------------------------------------
  60. * If something weird is required to be done with link/speed,
  61. * network driver is able to assign a function to implement this.
  62. * May be useful for PHY's that need to be software-driven.
  63. *-----------------------------------------------------------------------------*/
  64. int fixed_mdio_set_link_update(struct phy_device* phydev,
  65. int(*link_update)(struct net_device*, struct fixed_phy_status*))
  66. {
  67. struct fixed_info *fixed;
  68. if(link_update == NULL)
  69. return -EINVAL;
  70. if(phydev) {
  71. if(phydev->bus) {
  72. fixed = phydev->bus->priv;
  73. fixed->link_update = link_update;
  74. return 0;
  75. }
  76. }
  77. return -EINVAL;
  78. }
  79. EXPORT_SYMBOL(fixed_mdio_set_link_update);
  80. /*-----------------------------------------------------------------------------
  81. * This is used for updating internal mii regs from the status
  82. *-----------------------------------------------------------------------------*/
  83. static int fixed_mdio_update_regs(struct fixed_info *fixed)
  84. {
  85. u16 *regs = fixed->regs;
  86. u16 bmsr = 0;
  87. u16 bmcr = 0;
  88. if(!regs) {
  89. printk(KERN_ERR "%s: regs not set up", __FUNCTION__);
  90. return -EINVAL;
  91. }
  92. if(fixed->phy_status.link)
  93. bmsr |= BMSR_LSTATUS;
  94. if(fixed->phy_status.duplex) {
  95. bmcr |= BMCR_FULLDPLX;
  96. switch ( fixed->phy_status.speed ) {
  97. case 100:
  98. bmsr |= BMSR_100FULL;
  99. bmcr |= BMCR_SPEED100;
  100. break;
  101. case 10:
  102. bmsr |= BMSR_10FULL;
  103. break;
  104. }
  105. } else {
  106. switch ( fixed->phy_status.speed ) {
  107. case 100:
  108. bmsr |= BMSR_100HALF;
  109. bmcr |= BMCR_SPEED100;
  110. break;
  111. case 10:
  112. bmsr |= BMSR_100HALF;
  113. break;
  114. }
  115. }
  116. regs[MII_BMCR] = bmcr;
  117. regs[MII_BMSR] = bmsr | 0x800; /*we are always capable of 10 hdx*/
  118. return 0;
  119. }
  120. static int fixed_mii_read(struct mii_bus *bus, int phy_id, int location)
  121. {
  122. struct fixed_info *fixed = bus->priv;
  123. /* if user has registered link update callback, use it */
  124. if(fixed->phydev)
  125. if(fixed->phydev->attached_dev) {
  126. if(fixed->link_update) {
  127. fixed->link_update(fixed->phydev->attached_dev,
  128. &fixed->phy_status);
  129. fixed_mdio_update_regs(fixed);
  130. }
  131. }
  132. if ((unsigned int)location >= fixed->regs_num)
  133. return -1;
  134. return fixed->regs[location];
  135. }
  136. static int fixed_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val)
  137. {
  138. /* do nothing for now*/
  139. return 0;
  140. }
  141. static int fixed_mii_reset(struct mii_bus *bus)
  142. {
  143. /*nothing here - no way/need to reset it*/
  144. return 0;
  145. }
  146. static int fixed_config_aneg(struct phy_device *phydev)
  147. {
  148. /* :TODO:03/13/2006 09:45:37 PM::
  149. The full autoneg funcionality can be emulated,
  150. but no need to have anything here for now
  151. */
  152. return 0;
  153. }
  154. /*-----------------------------------------------------------------------------
  155. * the manual bind will do the magic - with phy_id_mask == 0
  156. * match will never return true...
  157. *-----------------------------------------------------------------------------*/
  158. static struct phy_driver fixed_mdio_driver = {
  159. .name = "Fixed PHY",
  160. .features = PHY_BASIC_FEATURES,
  161. .config_aneg = fixed_config_aneg,
  162. .read_status = genphy_read_status,
  163. .driver = { .owner = THIS_MODULE,},
  164. };
  165. /*-----------------------------------------------------------------------------
  166. * This func is used to create all the necessary stuff, bind
  167. * the fixed phy driver and register all it on the mdio_bus_type.
  168. * speed is either 10 or 100, duplex is boolean.
  169. * number is used to create multiple fixed PHYs, so that several devices can
  170. * utilize them simultaneously.
  171. *-----------------------------------------------------------------------------*/
  172. static int fixed_mdio_register_device(int number, int speed, int duplex)
  173. {
  174. struct mii_bus *new_bus;
  175. struct fixed_info *fixed;
  176. struct phy_device *phydev;
  177. int err = 0;
  178. struct device* dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  179. if (NULL == dev)
  180. return -ENOMEM;
  181. new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
  182. if (NULL == new_bus) {
  183. kfree(dev);
  184. return -ENOMEM;
  185. }
  186. fixed = kzalloc(sizeof(struct fixed_info), GFP_KERNEL);
  187. if (NULL == fixed) {
  188. kfree(dev);
  189. kfree(new_bus);
  190. return -ENOMEM;
  191. }
  192. fixed->regs = kzalloc(MII_REGS_NUM*sizeof(int), GFP_KERNEL);
  193. fixed->regs_num = MII_REGS_NUM;
  194. fixed->phy_status.speed = speed;
  195. fixed->phy_status.duplex = duplex;
  196. fixed->phy_status.link = 1;
  197. new_bus->name = "Fixed MII Bus",
  198. new_bus->read = &fixed_mii_read,
  199. new_bus->write = &fixed_mii_write,
  200. new_bus->reset = &fixed_mii_reset,
  201. /*set up workspace*/
  202. fixed_mdio_update_regs(fixed);
  203. new_bus->priv = fixed;
  204. new_bus->dev = dev;
  205. dev_set_drvdata(dev, new_bus);
  206. /* create phy_device and register it on the mdio bus */
  207. phydev = phy_device_create(new_bus, 0, 0);
  208. /*
  209. Put the phydev pointer into the fixed pack so that bus read/write code could
  210. be able to access for instance attached netdev. Well it doesn't have to do
  211. so, only in case of utilizing user-specified link-update...
  212. */
  213. fixed->phydev = phydev;
  214. if(NULL == phydev) {
  215. err = -ENOMEM;
  216. goto device_create_fail;
  217. }
  218. phydev->irq = -1;
  219. phydev->dev.bus = &mdio_bus_type;
  220. if(number)
  221. snprintf(phydev->dev.bus_id, BUS_ID_SIZE,
  222. "fixed_%d@%d:%d", number, speed, duplex);
  223. else
  224. snprintf(phydev->dev.bus_id, BUS_ID_SIZE,
  225. "fixed@%d:%d", speed, duplex);
  226. phydev->bus = new_bus;
  227. err = device_register(&phydev->dev);
  228. if(err) {
  229. printk(KERN_ERR "Phy %s failed to register\n",
  230. phydev->dev.bus_id);
  231. goto bus_register_fail;
  232. }
  233. /*
  234. the mdio bus has phy_id match... In order not to do it
  235. artificially, we are binding the driver here by hand;
  236. it will be the same for all the fixed phys anyway.
  237. */
  238. down_write(&phydev->dev.bus->subsys.rwsem);
  239. phydev->dev.driver = &fixed_mdio_driver.driver;
  240. err = phydev->dev.driver->probe(&phydev->dev);
  241. if(err < 0) {
  242. printk(KERN_ERR "Phy %s: problems with fixed driver\n",phydev->dev.bus_id);
  243. up_write(&phydev->dev.bus->subsys.rwsem);
  244. goto probe_fail;
  245. }
  246. err = device_bind_driver(&phydev->dev);
  247. up_write(&phydev->dev.bus->subsys.rwsem);
  248. if (err)
  249. goto probe_fail;
  250. return 0;
  251. probe_fail:
  252. device_unregister(&phydev->dev);
  253. bus_register_fail:
  254. kfree(phydev);
  255. device_create_fail:
  256. kfree(dev);
  257. kfree(new_bus);
  258. kfree(fixed);
  259. return err;
  260. }
  261. MODULE_DESCRIPTION("Fixed PHY device & driver for PAL");
  262. MODULE_AUTHOR("Vitaly Bordug");
  263. MODULE_LICENSE("GPL");
  264. static int __init fixed_init(void)
  265. {
  266. #if 0
  267. int ret;
  268. int duplex = 0;
  269. #endif
  270. /* register on the bus... Not expected to be matched with anything there... */
  271. phy_driver_register(&fixed_mdio_driver);
  272. /* So let the fun begin...
  273. We will create several mdio devices here, and will bound the upper
  274. driver to them.
  275. Then the external software can lookup the phy bus by searching
  276. fixed@speed:duplex, e.g. fixed@100:1, to be connected to the
  277. virtual 100M Fdx phy.
  278. In case several virtual PHYs required, the bus_id will be in form
  279. fixed_<num>@<speed>:<duplex>, which make it able even to define
  280. driver-specific link control callback, if for instance PHY is completely
  281. SW-driven.
  282. */
  283. #ifdef CONFIG_FIXED_MII_DUPLEX
  284. #if 0
  285. duplex = 1;
  286. #endif
  287. #endif
  288. #ifdef CONFIG_FIXED_MII_100_FDX
  289. fixed_mdio_register_device(0, 100, 1);
  290. #endif
  291. #ifdef CONFIX_FIXED_MII_10_FDX
  292. fixed_mdio_register_device(0, 10, 1);
  293. #endif
  294. return 0;
  295. }
  296. static void __exit fixed_exit(void)
  297. {
  298. phy_driver_unregister(&fixed_mdio_driver);
  299. /* :WARNING:02/18/2006 04:32:40 AM:: Cleanup all the created stuff */
  300. }
  301. module_init(fixed_init);
  302. module_exit(fixed_exit);