1
0

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