psb6970.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * Lantiq PSB6970 (Tantos) Switch driver
  3. *
  4. * Copyright (c) 2009,2010 Team Embedded.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License v2 as published by the
  8. * Free Software Foundation.
  9. *
  10. * The switch programming done in this driver follows the
  11. * "Ethernet Traffic Separation using VLAN" Application Note as
  12. * published by Lantiq.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/switch.h>
  17. #include <linux/phy.h>
  18. #include <linux/version.h>
  19. #define PSB6970_MAX_VLANS 16
  20. #define PSB6970_NUM_PORTS 7
  21. #define PSB6970_DEFAULT_PORT_CPU 6
  22. #define PSB6970_IS_CPU_PORT(x) ((x) > 4)
  23. #define PHYADDR(_reg) ((_reg >> 5) & 0xff), (_reg & 0x1f)
  24. /* --- Identification --- */
  25. #define PSB6970_CI0 0x0100
  26. #define PSB6970_CI0_MASK 0x000f
  27. #define PSB6970_CI1 0x0101
  28. #define PSB6970_CI1_VAL 0x2599
  29. #define PSB6970_CI1_MASK 0xffff
  30. /* --- VLAN filter table --- */
  31. #define PSB6970_VFxL(i) ((i)*2+0x10) /* VLAN Filter Low */
  32. #define PSB6970_VFxL_VV (1 << 15) /* VLAN_Valid */
  33. #define PSB6970_VFxH(i) ((i)*2+0x11) /* VLAN Filter High */
  34. #define PSB6970_VFxH_TM_SHIFT 7 /* Tagged Member */
  35. /* --- Port registers --- */
  36. #define PSB6970_EC(p) ((p)*0x20+2) /* Extended Control */
  37. #define PSB6970_EC_IFNTE (1 << 1) /* Input Force No Tag Enable */
  38. #define PSB6970_PBVM(p) ((p)*0x20+3) /* Port Base VLAN Map */
  39. #define PSB6970_PBVM_VMCE (1 << 8)
  40. #define PSB6970_PBVM_AOVTP (1 << 9)
  41. #define PSB6970_PBVM_VSD (1 << 10)
  42. #define PSB6970_PBVM_VC (1 << 11) /* VID Check with VID table */
  43. #define PSB6970_PBVM_TBVE (1 << 13) /* Tag-Based VLAN enable */
  44. #define PSB6970_DVID(p) ((p)*0x20+4) /* Default VLAN ID & Priority */
  45. struct psb6970_priv {
  46. struct switch_dev dev;
  47. struct phy_device *phy;
  48. u16 (*read) (struct phy_device* phydev, int reg);
  49. void (*write) (struct phy_device* phydev, int reg, u16 val);
  50. struct mutex reg_mutex;
  51. /* all fields below are cleared on reset */
  52. bool vlan;
  53. u16 vlan_id[PSB6970_MAX_VLANS];
  54. u8 vlan_table[PSB6970_MAX_VLANS];
  55. u8 vlan_tagged;
  56. u16 pvid[PSB6970_NUM_PORTS];
  57. };
  58. #define to_psb6970(_dev) container_of(_dev, struct psb6970_priv, dev)
  59. static u16 psb6970_mii_read(struct phy_device *phydev, int reg)
  60. {
  61. struct mii_bus *bus = phydev->mdio.bus;
  62. return bus->read(bus, PHYADDR(reg));
  63. }
  64. static void psb6970_mii_write(struct phy_device *phydev, int reg, u16 val)
  65. {
  66. struct mii_bus *bus = phydev->mdio.bus;
  67. bus->write(bus, PHYADDR(reg), val);
  68. }
  69. static int
  70. psb6970_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  71. struct switch_val *val)
  72. {
  73. struct psb6970_priv *priv = to_psb6970(dev);
  74. priv->vlan = !!val->value.i;
  75. return 0;
  76. }
  77. static int
  78. psb6970_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  79. struct switch_val *val)
  80. {
  81. struct psb6970_priv *priv = to_psb6970(dev);
  82. val->value.i = priv->vlan;
  83. return 0;
  84. }
  85. static int psb6970_set_pvid(struct switch_dev *dev, int port, int vlan)
  86. {
  87. struct psb6970_priv *priv = to_psb6970(dev);
  88. /* make sure no invalid PVIDs get set */
  89. if (vlan >= dev->vlans)
  90. return -EINVAL;
  91. priv->pvid[port] = vlan;
  92. return 0;
  93. }
  94. static int psb6970_get_pvid(struct switch_dev *dev, int port, int *vlan)
  95. {
  96. struct psb6970_priv *priv = to_psb6970(dev);
  97. *vlan = priv->pvid[port];
  98. return 0;
  99. }
  100. static int
  101. psb6970_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
  102. struct switch_val *val)
  103. {
  104. struct psb6970_priv *priv = to_psb6970(dev);
  105. priv->vlan_id[val->port_vlan] = val->value.i;
  106. return 0;
  107. }
  108. static int
  109. psb6970_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
  110. struct switch_val *val)
  111. {
  112. struct psb6970_priv *priv = to_psb6970(dev);
  113. val->value.i = priv->vlan_id[val->port_vlan];
  114. return 0;
  115. }
  116. static struct switch_attr psb6970_globals[] = {
  117. {
  118. .type = SWITCH_TYPE_INT,
  119. .name = "enable_vlan",
  120. .description = "Enable VLAN mode",
  121. .set = psb6970_set_vlan,
  122. .get = psb6970_get_vlan,
  123. .max = 1},
  124. };
  125. static struct switch_attr psb6970_port[] = {
  126. };
  127. static struct switch_attr psb6970_vlan[] = {
  128. {
  129. .type = SWITCH_TYPE_INT,
  130. .name = "vid",
  131. .description = "VLAN ID (0-4094)",
  132. .set = psb6970_set_vid,
  133. .get = psb6970_get_vid,
  134. .max = 4094,
  135. },
  136. };
  137. static int psb6970_get_ports(struct switch_dev *dev, struct switch_val *val)
  138. {
  139. struct psb6970_priv *priv = to_psb6970(dev);
  140. u8 ports = priv->vlan_table[val->port_vlan];
  141. int i;
  142. val->len = 0;
  143. for (i = 0; i < PSB6970_NUM_PORTS; i++) {
  144. struct switch_port *p;
  145. if (!(ports & (1 << i)))
  146. continue;
  147. p = &val->value.ports[val->len++];
  148. p->id = i;
  149. if (priv->vlan_tagged & (1 << i))
  150. p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
  151. else
  152. p->flags = 0;
  153. }
  154. return 0;
  155. }
  156. static int psb6970_set_ports(struct switch_dev *dev, struct switch_val *val)
  157. {
  158. struct psb6970_priv *priv = to_psb6970(dev);
  159. u8 *vt = &priv->vlan_table[val->port_vlan];
  160. int i, j;
  161. *vt = 0;
  162. for (i = 0; i < val->len; i++) {
  163. struct switch_port *p = &val->value.ports[i];
  164. if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
  165. priv->vlan_tagged |= (1 << p->id);
  166. else {
  167. priv->vlan_tagged &= ~(1 << p->id);
  168. priv->pvid[p->id] = val->port_vlan;
  169. /* make sure that an untagged port does not
  170. * appear in other vlans */
  171. for (j = 0; j < PSB6970_MAX_VLANS; j++) {
  172. if (j == val->port_vlan)
  173. continue;
  174. priv->vlan_table[j] &= ~(1 << p->id);
  175. }
  176. }
  177. *vt |= 1 << p->id;
  178. }
  179. return 0;
  180. }
  181. static int psb6970_hw_apply(struct switch_dev *dev)
  182. {
  183. struct psb6970_priv *priv = to_psb6970(dev);
  184. int i, j;
  185. mutex_lock(&priv->reg_mutex);
  186. if (priv->vlan) {
  187. /* into the vlan translation unit */
  188. for (j = 0; j < PSB6970_MAX_VLANS; j++) {
  189. u8 vp = priv->vlan_table[j];
  190. if (vp) {
  191. priv->write(priv->phy, PSB6970_VFxL(j),
  192. PSB6970_VFxL_VV | priv->vlan_id[j]);
  193. priv->write(priv->phy, PSB6970_VFxH(j),
  194. ((vp & priv->
  195. vlan_tagged) <<
  196. PSB6970_VFxH_TM_SHIFT) | vp);
  197. } else /* clear VLAN Valid flag for unused vlans */
  198. priv->write(priv->phy, PSB6970_VFxL(j), 0);
  199. }
  200. }
  201. /* update the port destination mask registers and tag settings */
  202. for (i = 0; i < PSB6970_NUM_PORTS; i++) {
  203. int dvid = 1, pbvm = 0x7f | PSB6970_PBVM_VSD, ec = 0;
  204. if (priv->vlan) {
  205. ec = PSB6970_EC_IFNTE;
  206. dvid = priv->vlan_id[priv->pvid[i]];
  207. pbvm |= PSB6970_PBVM_TBVE | PSB6970_PBVM_VMCE;
  208. if ((i << 1) & priv->vlan_tagged)
  209. pbvm |= PSB6970_PBVM_AOVTP | PSB6970_PBVM_VC;
  210. }
  211. priv->write(priv->phy, PSB6970_PBVM(i), pbvm);
  212. if (!PSB6970_IS_CPU_PORT(i)) {
  213. priv->write(priv->phy, PSB6970_EC(i), ec);
  214. priv->write(priv->phy, PSB6970_DVID(i), dvid);
  215. }
  216. }
  217. mutex_unlock(&priv->reg_mutex);
  218. return 0;
  219. }
  220. static int psb6970_reset_switch(struct switch_dev *dev)
  221. {
  222. struct psb6970_priv *priv = to_psb6970(dev);
  223. int i;
  224. mutex_lock(&priv->reg_mutex);
  225. memset(&priv->vlan, 0, sizeof(struct psb6970_priv) -
  226. offsetof(struct psb6970_priv, vlan));
  227. for (i = 0; i < PSB6970_MAX_VLANS; i++)
  228. priv->vlan_id[i] = i;
  229. mutex_unlock(&priv->reg_mutex);
  230. return psb6970_hw_apply(dev);
  231. }
  232. static const struct switch_dev_ops psb6970_ops = {
  233. .attr_global = {
  234. .attr = psb6970_globals,
  235. .n_attr = ARRAY_SIZE(psb6970_globals),
  236. },
  237. .attr_port = {
  238. .attr = psb6970_port,
  239. .n_attr = ARRAY_SIZE(psb6970_port),
  240. },
  241. .attr_vlan = {
  242. .attr = psb6970_vlan,
  243. .n_attr = ARRAY_SIZE(psb6970_vlan),
  244. },
  245. .get_port_pvid = psb6970_get_pvid,
  246. .set_port_pvid = psb6970_set_pvid,
  247. .get_vlan_ports = psb6970_get_ports,
  248. .set_vlan_ports = psb6970_set_ports,
  249. .apply_config = psb6970_hw_apply,
  250. .reset_switch = psb6970_reset_switch,
  251. };
  252. static int psb6970_config_init(struct phy_device *pdev)
  253. {
  254. struct psb6970_priv *priv;
  255. struct switch_dev *swdev;
  256. int ret;
  257. priv = kzalloc(sizeof(struct psb6970_priv), GFP_KERNEL);
  258. if (priv == NULL)
  259. return -ENOMEM;
  260. priv->phy = pdev;
  261. if (pdev->mdio.addr == 0)
  262. printk(KERN_INFO "%s: psb6970 switch driver attached.\n",
  263. pdev->attached_dev->name);
  264. if (pdev->mdio.addr != 0) {
  265. kfree(priv);
  266. return 0;
  267. }
  268. linkmode_zero(pdev->supported);
  269. linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, pdev->supported);
  270. linkmode_copy(pdev->advertising, pdev->supported);
  271. mutex_init(&priv->reg_mutex);
  272. priv->read = psb6970_mii_read;
  273. priv->write = psb6970_mii_write;
  274. pdev->priv = priv;
  275. swdev = &priv->dev;
  276. swdev->cpu_port = PSB6970_DEFAULT_PORT_CPU;
  277. swdev->ops = &psb6970_ops;
  278. swdev->name = "Lantiq PSB6970";
  279. swdev->vlans = PSB6970_MAX_VLANS;
  280. swdev->ports = PSB6970_NUM_PORTS;
  281. if ((ret = register_switch(&priv->dev, pdev->attached_dev)) < 0) {
  282. kfree(priv);
  283. goto done;
  284. }
  285. ret = psb6970_reset_switch(&priv->dev);
  286. if (ret) {
  287. kfree(priv);
  288. goto done;
  289. }
  290. done:
  291. return ret;
  292. }
  293. static int psb6970_read_status(struct phy_device *phydev)
  294. {
  295. phydev->speed = SPEED_100;
  296. phydev->duplex = DUPLEX_FULL;
  297. phydev->link = 1;
  298. phydev->state = PHY_RUNNING;
  299. netif_carrier_on(phydev->attached_dev);
  300. phydev->adjust_link(phydev->attached_dev);
  301. return 0;
  302. }
  303. static int psb6970_config_aneg(struct phy_device *phydev)
  304. {
  305. return 0;
  306. }
  307. static int psb6970_probe(struct phy_device *pdev)
  308. {
  309. return 0;
  310. }
  311. static void psb6970_remove(struct phy_device *pdev)
  312. {
  313. struct psb6970_priv *priv = pdev->priv;
  314. if (!priv)
  315. return;
  316. if (pdev->mdio.addr == 0)
  317. unregister_switch(&priv->dev);
  318. kfree(priv);
  319. }
  320. static int psb6970_fixup(struct phy_device *dev)
  321. {
  322. struct mii_bus *bus = dev->mdio.bus;
  323. u16 reg;
  324. /* look for the switch on the bus */
  325. reg = bus->read(bus, PHYADDR(PSB6970_CI1)) & PSB6970_CI1_MASK;
  326. if (reg != PSB6970_CI1_VAL)
  327. return 0;
  328. dev->phy_id = (reg << 16);
  329. dev->phy_id |= bus->read(bus, PHYADDR(PSB6970_CI0)) & PSB6970_CI0_MASK;
  330. return 0;
  331. }
  332. static struct phy_driver psb6970_driver = {
  333. .name = "Lantiq PSB6970",
  334. .phy_id = PSB6970_CI1_VAL << 16,
  335. .phy_id_mask = 0xffff0000,
  336. .features = PHY_BASIC_FEATURES,
  337. .probe = psb6970_probe,
  338. .remove = psb6970_remove,
  339. .config_init = &psb6970_config_init,
  340. .config_aneg = &psb6970_config_aneg,
  341. .read_status = &psb6970_read_status,
  342. };
  343. int __init psb6970_init(void)
  344. {
  345. phy_register_fixup_for_id(PHY_ANY_ID, psb6970_fixup);
  346. return phy_driver_register(&psb6970_driver, THIS_MODULE);
  347. }
  348. module_init(psb6970_init);
  349. void __exit psb6970_exit(void)
  350. {
  351. phy_driver_unregister(&psb6970_driver);
  352. }
  353. module_exit(psb6970_exit);
  354. MODULE_DESCRIPTION("Lantiq PSB6970 Switch");
  355. MODULE_AUTHOR("Ithamar R. Adema <[email protected]>");
  356. MODULE_LICENSE("GPL");