ar8216.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * ar8216.c: AR8216 switch driver
  3. *
  4. * Copyright (C) 2009 Felix Fietkau <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/if.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/list.h>
  20. #include <linux/if_ether.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/netlink.h>
  24. #include <linux/bitops.h>
  25. #include <net/genetlink.h>
  26. #include <linux/switch.h>
  27. #include <linux/delay.h>
  28. #include <linux/phy.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/etherdevice.h>
  31. #include "ar8216.h"
  32. /* size of the vlan table */
  33. #define AR8X16_MAX_VLANS 128
  34. #define AR8X16_PROBE_RETRIES 10
  35. struct ar8216_priv {
  36. struct switch_dev dev;
  37. struct phy_device *phy;
  38. u32 (*read)(struct ar8216_priv *priv, int reg);
  39. void (*write)(struct ar8216_priv *priv, int reg, u32 val);
  40. const struct net_device_ops *ndo_old;
  41. struct net_device_ops ndo;
  42. struct mutex reg_mutex;
  43. int chip;
  44. /* all fields below are cleared on reset */
  45. bool vlan;
  46. u16 vlan_id[AR8X16_MAX_VLANS];
  47. u8 vlan_table[AR8X16_MAX_VLANS];
  48. u8 vlan_tagged;
  49. u16 pvid[AR8216_NUM_PORTS];
  50. };
  51. #define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
  52. static inline void
  53. split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
  54. {
  55. regaddr >>= 1;
  56. *r1 = regaddr & 0x1e;
  57. regaddr >>= 5;
  58. *r2 = regaddr & 0x7;
  59. regaddr >>= 3;
  60. *page = regaddr & 0x1ff;
  61. }
  62. static u32
  63. ar8216_mii_read(struct ar8216_priv *priv, int reg)
  64. {
  65. struct phy_device *phy = priv->phy;
  66. u16 r1, r2, page;
  67. u16 lo, hi;
  68. split_addr((u32) reg, &r1, &r2, &page);
  69. phy->bus->write(phy->bus, 0x18, 0, page);
  70. msleep(1); /* wait for the page switch to propagate */
  71. lo = phy->bus->read(phy->bus, 0x10 | r2, r1);
  72. hi = phy->bus->read(phy->bus, 0x10 | r2, r1 + 1);
  73. return (hi << 16) | lo;
  74. }
  75. static void
  76. ar8216_mii_write(struct ar8216_priv *priv, int reg, u32 val)
  77. {
  78. struct phy_device *phy = priv->phy;
  79. u16 r1, r2, r3;
  80. u16 lo, hi;
  81. split_addr((u32) reg, &r1, &r2, &r3);
  82. phy->bus->write(phy->bus, 0x18, 0, r3);
  83. msleep(1); /* wait for the page switch to propagate */
  84. lo = val & 0xffff;
  85. hi = (u16) (val >> 16);
  86. phy->bus->write(phy->bus, 0x10 | r2, r1 + 1, hi);
  87. phy->bus->write(phy->bus, 0x10 | r2, r1, lo);
  88. }
  89. static u32
  90. ar8216_rmw(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
  91. {
  92. u32 v;
  93. v = priv->read(priv, reg);
  94. v &= ~mask;
  95. v |= val;
  96. priv->write(priv, reg, v);
  97. return v;
  98. }
  99. static inline int
  100. ar8216_id_chip(struct ar8216_priv *priv)
  101. {
  102. u32 val;
  103. u16 id;
  104. int i;
  105. val = ar8216_mii_read(priv, AR8216_REG_CTRL);
  106. if (val == ~0)
  107. return UNKNOWN;
  108. id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  109. for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
  110. u16 t;
  111. val = ar8216_mii_read(priv, AR8216_REG_CTRL);
  112. if (val == ~0)
  113. return UNKNOWN;
  114. t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  115. if (t != id)
  116. return UNKNOWN;
  117. }
  118. switch (id) {
  119. case 0x0101:
  120. return AR8216;
  121. case 0x1001:
  122. return AR8316;
  123. default:
  124. printk(KERN_DEBUG
  125. "ar8216: Unknown Atheros device [ver=%d, rev=%d, phy_id=%04x%04x]\n",
  126. (int)(id >> AR8216_CTRL_VERSION_S),
  127. (int)(id & AR8216_CTRL_REVISION),
  128. priv->phy->bus->read(priv->phy->bus, priv->phy->addr, 2),
  129. priv->phy->bus->read(priv->phy->bus, priv->phy->addr, 3));
  130. return UNKNOWN;
  131. }
  132. }
  133. static int
  134. ar8216_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  135. struct switch_val *val)
  136. {
  137. struct ar8216_priv *priv = to_ar8216(dev);
  138. priv->vlan = !!val->value.i;
  139. return 0;
  140. }
  141. static int
  142. ar8216_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  143. struct switch_val *val)
  144. {
  145. struct ar8216_priv *priv = to_ar8216(dev);
  146. val->value.i = priv->vlan;
  147. return 0;
  148. }
  149. static int
  150. ar8216_set_pvid(struct switch_dev *dev, int port, int vlan)
  151. {
  152. struct ar8216_priv *priv = to_ar8216(dev);
  153. /* make sure no invalid PVIDs get set */
  154. if (vlan >= dev->vlans)
  155. return -EINVAL;
  156. priv->pvid[port] = vlan;
  157. return 0;
  158. }
  159. static int
  160. ar8216_get_pvid(struct switch_dev *dev, int port, int *vlan)
  161. {
  162. struct ar8216_priv *priv = to_ar8216(dev);
  163. *vlan = priv->pvid[port];
  164. return 0;
  165. }
  166. static int
  167. ar8216_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
  168. struct switch_val *val)
  169. {
  170. struct ar8216_priv *priv = to_ar8216(dev);
  171. priv->vlan_id[val->port_vlan] = val->value.i;
  172. return 0;
  173. }
  174. static int
  175. ar8216_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
  176. struct switch_val *val)
  177. {
  178. struct ar8216_priv *priv = to_ar8216(dev);
  179. val->value.i = priv->vlan_id[val->port_vlan];
  180. return 0;
  181. }
  182. static int
  183. ar8216_mangle_tx(struct sk_buff *skb, struct net_device *dev)
  184. {
  185. struct ar8216_priv *priv = dev->phy_ptr;
  186. unsigned char *buf;
  187. if (unlikely(!priv))
  188. goto error;
  189. if (!priv->vlan)
  190. goto send;
  191. if (unlikely(skb_headroom(skb) < 2)) {
  192. if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
  193. goto error;
  194. }
  195. buf = skb_push(skb, 2);
  196. buf[0] = 0x10;
  197. buf[1] = 0x80;
  198. send:
  199. return priv->ndo_old->ndo_start_xmit(skb, dev);
  200. error:
  201. dev_kfree_skb_any(skb);
  202. return 0;
  203. }
  204. static int
  205. ar8216_mangle_rx(struct sk_buff *skb, int napi)
  206. {
  207. struct ar8216_priv *priv;
  208. struct net_device *dev;
  209. unsigned char *buf;
  210. int port, vlan;
  211. dev = skb->dev;
  212. if (!dev)
  213. goto error;
  214. priv = dev->phy_ptr;
  215. if (!priv)
  216. goto error;
  217. /* don't strip the header if vlan mode is disabled */
  218. if (!priv->vlan)
  219. goto recv;
  220. /* strip header, get vlan id */
  221. buf = skb->data;
  222. skb_pull(skb, 2);
  223. /* check for vlan header presence */
  224. if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
  225. goto recv;
  226. port = buf[0] & 0xf;
  227. /* no need to fix up packets coming from a tagged source */
  228. if (priv->vlan_tagged & (1 << port))
  229. goto recv;
  230. /* lookup port vid from local table, the switch passes an invalid vlan id */
  231. vlan = priv->vlan_id[priv->pvid[port]];
  232. buf[14 + 2] &= 0xf0;
  233. buf[14 + 2] |= vlan >> 8;
  234. buf[15 + 2] = vlan & 0xff;
  235. recv:
  236. skb->protocol = eth_type_trans(skb, skb->dev);
  237. if (napi)
  238. return netif_receive_skb(skb);
  239. else
  240. return netif_rx(skb);
  241. error:
  242. /* no vlan? eat the packet! */
  243. dev_kfree_skb_any(skb);
  244. return NET_RX_DROP;
  245. }
  246. static int
  247. ar8216_netif_rx(struct sk_buff *skb)
  248. {
  249. return ar8216_mangle_rx(skb, 0);
  250. }
  251. static int
  252. ar8216_netif_receive_skb(struct sk_buff *skb)
  253. {
  254. return ar8216_mangle_rx(skb, 1);
  255. }
  256. static struct switch_attr ar8216_globals[] = {
  257. {
  258. .type = SWITCH_TYPE_INT,
  259. .name = "enable_vlan",
  260. .description = "Enable VLAN mode",
  261. .set = ar8216_set_vlan,
  262. .get = ar8216_get_vlan,
  263. .max = 1
  264. },
  265. };
  266. static struct switch_attr ar8216_port[] = {
  267. };
  268. static struct switch_attr ar8216_vlan[] = {
  269. {
  270. .type = SWITCH_TYPE_INT,
  271. .name = "vid",
  272. .description = "VLAN ID (0-4094)",
  273. .set = ar8216_set_vid,
  274. .get = ar8216_get_vid,
  275. .max = 4094,
  276. },
  277. };
  278. static int
  279. ar8216_get_ports(struct switch_dev *dev, struct switch_val *val)
  280. {
  281. struct ar8216_priv *priv = to_ar8216(dev);
  282. u8 ports = priv->vlan_table[val->port_vlan];
  283. int i;
  284. val->len = 0;
  285. for (i = 0; i < AR8216_NUM_PORTS; i++) {
  286. struct switch_port *p;
  287. if (!(ports & (1 << i)))
  288. continue;
  289. p = &val->value.ports[val->len++];
  290. p->id = i;
  291. if (priv->vlan_tagged & (1 << i))
  292. p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
  293. else
  294. p->flags = 0;
  295. }
  296. return 0;
  297. }
  298. static int
  299. ar8216_set_ports(struct switch_dev *dev, struct switch_val *val)
  300. {
  301. struct ar8216_priv *priv = to_ar8216(dev);
  302. u8 *vt = &priv->vlan_table[val->port_vlan];
  303. int i, j;
  304. *vt = 0;
  305. for (i = 0; i < val->len; i++) {
  306. struct switch_port *p = &val->value.ports[i];
  307. if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
  308. priv->vlan_tagged |= (1 << p->id);
  309. else {
  310. priv->vlan_tagged &= ~(1 << p->id);
  311. priv->pvid[p->id] = val->port_vlan;
  312. /* make sure that an untagged port does not
  313. * appear in other vlans */
  314. for (j = 0; j < AR8X16_MAX_VLANS; j++) {
  315. if (j == val->port_vlan)
  316. continue;
  317. priv->vlan_table[j] &= ~(1 << p->id);
  318. }
  319. }
  320. *vt |= 1 << p->id;
  321. }
  322. return 0;
  323. }
  324. static int
  325. ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
  326. {
  327. int timeout = 20;
  328. while ((priv->read(priv, reg) & mask) != val) {
  329. if (timeout-- <= 0) {
  330. printk(KERN_ERR "ar8216: timeout waiting for operation to complete\n");
  331. return 1;
  332. }
  333. }
  334. return 0;
  335. }
  336. static void
  337. ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
  338. {
  339. if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
  340. return;
  341. if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
  342. val &= AR8216_VTUDATA_MEMBER;
  343. val |= AR8216_VTUDATA_VALID;
  344. priv->write(priv, AR8216_REG_VTU_DATA, val);
  345. }
  346. op |= AR8216_VTU_ACTIVE;
  347. priv->write(priv, AR8216_REG_VTU, op);
  348. }
  349. static int
  350. ar8216_hw_apply(struct switch_dev *dev)
  351. {
  352. struct ar8216_priv *priv = to_ar8216(dev);
  353. u8 portmask[AR8216_NUM_PORTS];
  354. int i, j;
  355. mutex_lock(&priv->reg_mutex);
  356. /* flush all vlan translation unit entries */
  357. ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
  358. memset(portmask, 0, sizeof(portmask));
  359. if (priv->vlan) {
  360. /* calculate the port destination masks and load vlans
  361. * into the vlan translation unit */
  362. for (j = 0; j < AR8X16_MAX_VLANS; j++) {
  363. u8 vp = priv->vlan_table[j];
  364. if (!vp)
  365. continue;
  366. for (i = 0; i < AR8216_NUM_PORTS; i++) {
  367. u8 mask = (1 << i);
  368. if (vp & mask)
  369. portmask[i] |= vp & ~mask;
  370. }
  371. ar8216_vtu_op(priv,
  372. AR8216_VTU_OP_LOAD |
  373. (priv->vlan_id[j] << AR8216_VTU_VID_S),
  374. priv->vlan_table[j]);
  375. }
  376. } else {
  377. /* vlan disabled:
  378. * isolate all ports, but connect them to the cpu port */
  379. for (i = 0; i < AR8216_NUM_PORTS; i++) {
  380. if (i == AR8216_PORT_CPU)
  381. continue;
  382. portmask[i] = 1 << AR8216_PORT_CPU;
  383. portmask[AR8216_PORT_CPU] |= (1 << i);
  384. }
  385. }
  386. /* update the port destination mask registers and tag settings */
  387. for (i = 0; i < AR8216_NUM_PORTS; i++) {
  388. int egress, ingress;
  389. int pvid;
  390. if (priv->vlan) {
  391. pvid = priv->vlan_id[priv->pvid[i]];
  392. } else {
  393. pvid = i;
  394. }
  395. if (priv->vlan && (priv->vlan_tagged & (1 << i))) {
  396. egress = AR8216_OUT_ADD_VLAN;
  397. } else {
  398. egress = AR8216_OUT_STRIP_VLAN;
  399. }
  400. if (priv->vlan) {
  401. ingress = AR8216_IN_SECURE;
  402. } else {
  403. ingress = AR8216_IN_PORT_ONLY;
  404. }
  405. ar8216_rmw(priv, AR8216_REG_PORT_CTRL(i),
  406. AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
  407. AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
  408. AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
  409. AR8216_PORT_CTRL_LEARN |
  410. (priv->vlan && i == AR8216_PORT_CPU && (priv->chip == AR8216) ?
  411. AR8216_PORT_CTRL_HEADER : 0) |
  412. (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
  413. (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
  414. ar8216_rmw(priv, AR8216_REG_PORT_VLAN(i),
  415. AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
  416. AR8216_PORT_VLAN_DEFAULT_ID,
  417. (portmask[i] << AR8216_PORT_VLAN_DEST_PORTS_S) |
  418. (ingress << AR8216_PORT_VLAN_MODE_S) |
  419. (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
  420. }
  421. mutex_unlock(&priv->reg_mutex);
  422. return 0;
  423. }
  424. static int
  425. ar8316_hw_init(struct ar8216_priv *priv) {
  426. static int initialized;
  427. int i;
  428. u32 val;
  429. struct mii_bus *bus;
  430. if (initialized)
  431. return 0;
  432. val = priv->read(priv, 0x8);
  433. if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  434. /* value taken from Ubiquiti RouterStation Pro */
  435. if (val == 0x81461bea) {
  436. /* switch already intialized by bootloader */
  437. initialized = true;
  438. return 0;
  439. }
  440. priv->write(priv, 0x8, 0x81461bea);
  441. } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
  442. /* value taken from AVM Fritz!Box 7390 sources */
  443. if (val == 0x010e5b71) {
  444. /* switch already initialized by bootloader */
  445. initialized = true;
  446. return 0;
  447. }
  448. priv->write(priv, 0x8, 0x010e5b71);
  449. } else {
  450. /* no known value for phy interface */
  451. printk(KERN_ERR "ar8316: unsupported mii mode: %d.\n",
  452. priv->phy->interface);
  453. return -EINVAL;
  454. }
  455. /* standard atheros magic */
  456. priv->write(priv, 0x38, 0xc000050e);
  457. /* Initialize the ports */
  458. bus = priv->phy->bus;
  459. for (i = 0; i < 5; i++) {
  460. if ((i == 4) &&
  461. priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  462. /* work around for phy4 rgmii mode */
  463. bus->write(bus, i, MII_ATH_DBG_ADDR, 0x12);
  464. bus->write(bus, i, MII_ATH_DBG_DATA, 0x480c);
  465. /* rx delay */
  466. bus->write(bus, i, MII_ATH_DBG_ADDR, 0x0);
  467. bus->write(bus, i, MII_ATH_DBG_DATA, 0x824e);
  468. /* tx delay */
  469. bus->write(bus, i, MII_ATH_DBG_ADDR, 0x5);
  470. bus->write(bus, i, MII_ATH_DBG_DATA, 0x3d47);
  471. msleep(1000);
  472. }
  473. /* initialize the port itself */
  474. bus->write(bus, i, MII_ADVERTISE,
  475. ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
  476. bus->write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
  477. bus->write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  478. msleep(1000);
  479. }
  480. initialized = true;
  481. return 0;
  482. }
  483. static int
  484. ar8216_reset_switch(struct switch_dev *dev)
  485. {
  486. struct ar8216_priv *priv = to_ar8216(dev);
  487. int i;
  488. mutex_lock(&priv->reg_mutex);
  489. memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
  490. offsetof(struct ar8216_priv, vlan));
  491. for (i = 0; i < AR8X16_MAX_VLANS; i++) {
  492. priv->vlan_id[i] = i;
  493. }
  494. for (i = 0; i < AR8216_NUM_PORTS; i++) {
  495. /* Enable port learning and tx */
  496. priv->write(priv, AR8216_REG_PORT_CTRL(i),
  497. AR8216_PORT_CTRL_LEARN |
  498. (4 << AR8216_PORT_CTRL_STATE_S));
  499. priv->write(priv, AR8216_REG_PORT_VLAN(i), 0);
  500. /* Configure all PHYs */
  501. if (i == AR8216_PORT_CPU) {
  502. priv->write(priv, AR8216_REG_PORT_STATUS(i),
  503. AR8216_PORT_STATUS_LINK_UP |
  504. ((priv->chip == AR8316) ?
  505. AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
  506. AR8216_PORT_STATUS_TXMAC |
  507. AR8216_PORT_STATUS_RXMAC |
  508. ((priv->chip == AR8316) ? AR8216_PORT_STATUS_RXFLOW : 0) |
  509. ((priv->chip == AR8316) ? AR8216_PORT_STATUS_TXFLOW : 0) |
  510. AR8216_PORT_STATUS_DUPLEX);
  511. } else {
  512. priv->write(priv, AR8216_REG_PORT_STATUS(i),
  513. AR8216_PORT_STATUS_LINK_AUTO);
  514. }
  515. }
  516. /* XXX: undocumented magic from atheros, required! */
  517. priv->write(priv, 0x38, 0xc000050e);
  518. if (priv->chip == AR8216) {
  519. ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  520. AR8216_GCTRL_MTU, 1518 + 8 + 2);
  521. } else if (priv->chip == AR8316) {
  522. /* enable jumbo frames */
  523. ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  524. AR8316_GCTRL_MTU, 9018 + 8 + 2);
  525. }
  526. if (priv->chip == AR8316) {
  527. /* enable cpu port to receive multicast and broadcast frames */
  528. priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
  529. }
  530. mutex_unlock(&priv->reg_mutex);
  531. return ar8216_hw_apply(dev);
  532. }
  533. static const struct switch_dev_ops ar8216_ops = {
  534. .attr_global = {
  535. .attr = ar8216_globals,
  536. .n_attr = ARRAY_SIZE(ar8216_globals),
  537. },
  538. .attr_port = {
  539. .attr = ar8216_port,
  540. .n_attr = ARRAY_SIZE(ar8216_port),
  541. },
  542. .attr_vlan = {
  543. .attr = ar8216_vlan,
  544. .n_attr = ARRAY_SIZE(ar8216_vlan),
  545. },
  546. .get_port_pvid = ar8216_get_pvid,
  547. .set_port_pvid = ar8216_set_pvid,
  548. .get_vlan_ports = ar8216_get_ports,
  549. .set_vlan_ports = ar8216_set_ports,
  550. .apply_config = ar8216_hw_apply,
  551. .reset_switch = ar8216_reset_switch,
  552. };
  553. static int
  554. ar8216_config_init(struct phy_device *pdev)
  555. {
  556. struct ar8216_priv *priv;
  557. struct net_device *dev = pdev->attached_dev;
  558. struct switch_dev *swdev;
  559. int ret;
  560. priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
  561. if (priv == NULL)
  562. return -ENOMEM;
  563. priv->phy = pdev;
  564. priv->chip = ar8216_id_chip(priv);
  565. if (pdev->addr == 0)
  566. printk(KERN_INFO "%s: AR%d switch driver attached.\n",
  567. pdev->attached_dev->name, priv->chip);
  568. if (pdev->addr != 0) {
  569. if (priv->chip == AR8316) {
  570. pdev->supported |= SUPPORTED_1000baseT_Full;
  571. pdev->advertising |= ADVERTISED_1000baseT_Full;
  572. }
  573. kfree(priv);
  574. return 0;
  575. }
  576. pdev->supported = priv->chip == AR8316 ?
  577. SUPPORTED_1000baseT_Full : SUPPORTED_100baseT_Full;
  578. pdev->advertising = pdev->supported;
  579. mutex_init(&priv->reg_mutex);
  580. priv->read = ar8216_mii_read;
  581. priv->write = ar8216_mii_write;
  582. pdev->priv = priv;
  583. swdev = &priv->dev;
  584. swdev->cpu_port = AR8216_PORT_CPU;
  585. swdev->ops = &ar8216_ops;
  586. if (priv->chip == AR8316) {
  587. swdev->name = "Atheros AR8316";
  588. swdev->vlans = AR8X16_MAX_VLANS;
  589. /* port 5 connected to the other mac, therefore unusable */
  590. swdev->ports = (AR8216_NUM_PORTS - 1);
  591. } else {
  592. swdev->name = "Atheros AR8216";
  593. swdev->vlans = AR8216_NUM_VLANS;
  594. swdev->ports = AR8216_NUM_PORTS;
  595. }
  596. if ((ret = register_switch(&priv->dev, pdev->attached_dev)) < 0) {
  597. kfree(priv);
  598. goto done;
  599. }
  600. if (priv->chip == AR8316) {
  601. ret = ar8316_hw_init(priv);
  602. if (ret) {
  603. kfree(priv);
  604. goto done;
  605. }
  606. }
  607. ret = ar8216_reset_switch(&priv->dev);
  608. if (ret) {
  609. kfree(priv);
  610. goto done;
  611. }
  612. dev->phy_ptr = priv;
  613. /* VID fixup only needed on ar8216 */
  614. if (pdev->addr == 0 && priv->chip == AR8216) {
  615. pdev->pkt_align = 2;
  616. pdev->netif_receive_skb = ar8216_netif_receive_skb;
  617. pdev->netif_rx = ar8216_netif_rx;
  618. priv->ndo_old = dev->netdev_ops;
  619. memcpy(&priv->ndo, priv->ndo_old, sizeof(struct net_device_ops));
  620. priv->ndo.ndo_start_xmit = ar8216_mangle_tx;
  621. dev->netdev_ops = &priv->ndo;
  622. }
  623. done:
  624. return ret;
  625. }
  626. static int
  627. ar8216_read_status(struct phy_device *phydev)
  628. {
  629. struct ar8216_priv *priv = phydev->priv;
  630. int ret;
  631. if (phydev->addr != 0) {
  632. return genphy_read_status(phydev);
  633. }
  634. phydev->speed = priv->chip == AR8316 ? SPEED_1000 : SPEED_100;
  635. phydev->duplex = DUPLEX_FULL;
  636. phydev->link = 1;
  637. /* flush the address translation unit */
  638. mutex_lock(&priv->reg_mutex);
  639. ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
  640. if (!ret)
  641. priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
  642. else
  643. ret = -ETIMEDOUT;
  644. mutex_unlock(&priv->reg_mutex);
  645. phydev->state = PHY_RUNNING;
  646. netif_carrier_on(phydev->attached_dev);
  647. phydev->adjust_link(phydev->attached_dev);
  648. return ret;
  649. }
  650. static int
  651. ar8216_config_aneg(struct phy_device *phydev)
  652. {
  653. if (phydev->addr == 0)
  654. return 0;
  655. return genphy_config_aneg(phydev);
  656. }
  657. static int
  658. ar8216_probe(struct phy_device *pdev)
  659. {
  660. struct ar8216_priv priv;
  661. u16 chip;
  662. priv.phy = pdev;
  663. chip = ar8216_id_chip(&priv);
  664. if (chip == UNKNOWN)
  665. return -ENODEV;
  666. return 0;
  667. }
  668. static void
  669. ar8216_remove(struct phy_device *pdev)
  670. {
  671. struct ar8216_priv *priv = pdev->priv;
  672. struct net_device *dev = pdev->attached_dev;
  673. if (!priv)
  674. return;
  675. if (priv->ndo_old && dev)
  676. dev->netdev_ops = priv->ndo_old;
  677. if (pdev->addr == 0)
  678. unregister_switch(&priv->dev);
  679. kfree(priv);
  680. }
  681. static struct phy_driver ar8216_driver = {
  682. .phy_id = 0x004d0000,
  683. .name = "Atheros AR8216/AR8316",
  684. .phy_id_mask = 0xffff0000,
  685. .features = PHY_BASIC_FEATURES,
  686. .probe = ar8216_probe,
  687. .remove = ar8216_remove,
  688. .config_init = &ar8216_config_init,
  689. .config_aneg = &ar8216_config_aneg,
  690. .read_status = &ar8216_read_status,
  691. .driver = { .owner = THIS_MODULE },
  692. };
  693. int __init
  694. ar8216_init(void)
  695. {
  696. return phy_driver_register(&ar8216_driver);
  697. }
  698. void __exit
  699. ar8216_exit(void)
  700. {
  701. phy_driver_unregister(&ar8216_driver);
  702. }
  703. module_init(ar8216_init);
  704. module_exit(ar8216_exit);
  705. MODULE_LICENSE("GPL");