psb6970.c 10 KB

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