ar8216.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  1. /*
  2. * ar8216.c: AR8216 switch driver
  3. *
  4. * Copyright (C) 2009 Felix Fietkau <[email protected]>
  5. * Copyright (C) 2011-2012 Gabor Juhos <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/if.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/if_ether.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/netlink.h>
  25. #include <linux/bitops.h>
  26. #include <net/genetlink.h>
  27. #include <linux/switch.h>
  28. #include <linux/delay.h>
  29. #include <linux/phy.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/lockdep.h>
  33. #include <linux/ar8216_platform.h>
  34. #include "ar8216.h"
  35. /* size of the vlan table */
  36. #define AR8X16_MAX_VLANS 128
  37. #define AR8X16_PROBE_RETRIES 10
  38. #define AR8X16_MAX_PORTS 8
  39. struct ar8216_priv;
  40. #define AR8XXX_CAP_GIGE BIT(0)
  41. enum {
  42. AR8XXX_VER_AR8216 = 0x01,
  43. AR8XXX_VER_AR8236 = 0x03,
  44. AR8XXX_VER_AR8316 = 0x10,
  45. AR8XXX_VER_AR8327 = 0x12,
  46. };
  47. struct ar8xxx_chip {
  48. unsigned long caps;
  49. int (*hw_init)(struct ar8216_priv *priv);
  50. void (*init_globals)(struct ar8216_priv *priv);
  51. void (*init_port)(struct ar8216_priv *priv, int port);
  52. void (*setup_port)(struct ar8216_priv *priv, int port, u32 egress,
  53. u32 ingress, u32 members, u32 pvid);
  54. u32 (*read_port_status)(struct ar8216_priv *priv, int port);
  55. int (*atu_flush)(struct ar8216_priv *priv);
  56. void (*vtu_flush)(struct ar8216_priv *priv);
  57. void (*vtu_load_vlan)(struct ar8216_priv *priv, u32 vid, u32 port_mask);
  58. };
  59. struct ar8216_priv {
  60. struct switch_dev dev;
  61. struct phy_device *phy;
  62. u32 (*read)(struct ar8216_priv *priv, int reg);
  63. void (*write)(struct ar8216_priv *priv, int reg, u32 val);
  64. const struct net_device_ops *ndo_old;
  65. struct net_device_ops ndo;
  66. struct mutex reg_mutex;
  67. u8 chip_ver;
  68. u8 chip_rev;
  69. const struct ar8xxx_chip *chip;
  70. bool initialized;
  71. bool port4_phy;
  72. char buf[80];
  73. bool init;
  74. bool mii_lo_first;
  75. /* all fields below are cleared on reset */
  76. bool vlan;
  77. u16 vlan_id[AR8X16_MAX_VLANS];
  78. u8 vlan_table[AR8X16_MAX_VLANS];
  79. u8 vlan_tagged;
  80. u16 pvid[AR8X16_MAX_PORTS];
  81. };
  82. #define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
  83. static inline bool ar8xxx_has_gige(struct ar8216_priv *priv)
  84. {
  85. return priv->chip->caps & AR8XXX_CAP_GIGE;
  86. }
  87. static inline bool chip_is_ar8216(struct ar8216_priv *priv)
  88. {
  89. return priv->chip_ver == AR8XXX_VER_AR8216;
  90. }
  91. static inline bool chip_is_ar8236(struct ar8216_priv *priv)
  92. {
  93. return priv->chip_ver == AR8XXX_VER_AR8236;
  94. }
  95. static inline bool chip_is_ar8316(struct ar8216_priv *priv)
  96. {
  97. return priv->chip_ver == AR8XXX_VER_AR8316;
  98. }
  99. static inline bool chip_is_ar8327(struct ar8216_priv *priv)
  100. {
  101. return priv->chip_ver == AR8XXX_VER_AR8327;
  102. }
  103. static inline void
  104. split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
  105. {
  106. regaddr >>= 1;
  107. *r1 = regaddr & 0x1e;
  108. regaddr >>= 5;
  109. *r2 = regaddr & 0x7;
  110. regaddr >>= 3;
  111. *page = regaddr & 0x1ff;
  112. }
  113. static u32
  114. ar8216_mii_read(struct ar8216_priv *priv, int reg)
  115. {
  116. struct phy_device *phy = priv->phy;
  117. struct mii_bus *bus = phy->bus;
  118. u16 r1, r2, page;
  119. u16 lo, hi;
  120. split_addr((u32) reg, &r1, &r2, &page);
  121. mutex_lock(&bus->mdio_lock);
  122. bus->write(bus, 0x18, 0, page);
  123. usleep_range(1000, 2000); /* wait for the page switch to propagate */
  124. lo = bus->read(bus, 0x10 | r2, r1);
  125. hi = bus->read(bus, 0x10 | r2, r1 + 1);
  126. mutex_unlock(&bus->mdio_lock);
  127. return (hi << 16) | lo;
  128. }
  129. static void
  130. ar8216_mii_write(struct ar8216_priv *priv, int reg, u32 val)
  131. {
  132. struct phy_device *phy = priv->phy;
  133. struct mii_bus *bus = phy->bus;
  134. u16 r1, r2, r3;
  135. u16 lo, hi;
  136. split_addr((u32) reg, &r1, &r2, &r3);
  137. lo = val & 0xffff;
  138. hi = (u16) (val >> 16);
  139. mutex_lock(&bus->mdio_lock);
  140. bus->write(bus, 0x18, 0, r3);
  141. usleep_range(1000, 2000); /* wait for the page switch to propagate */
  142. if (priv->mii_lo_first) {
  143. bus->write(bus, 0x10 | r2, r1, lo);
  144. bus->write(bus, 0x10 | r2, r1 + 1, hi);
  145. } else {
  146. bus->write(bus, 0x10 | r2, r1 + 1, hi);
  147. bus->write(bus, 0x10 | r2, r1, lo);
  148. }
  149. mutex_unlock(&bus->mdio_lock);
  150. }
  151. static void
  152. ar8216_phy_dbg_write(struct ar8216_priv *priv, int phy_addr,
  153. u16 dbg_addr, u16 dbg_data)
  154. {
  155. struct mii_bus *bus = priv->phy->bus;
  156. mutex_lock(&bus->mdio_lock);
  157. bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
  158. bus->write(bus, phy_addr, MII_ATH_DBG_DATA, dbg_data);
  159. mutex_unlock(&bus->mdio_lock);
  160. }
  161. static void
  162. ar8216_phy_mmd_write(struct ar8216_priv *priv, int phy_addr, u16 addr, u16 data)
  163. {
  164. struct mii_bus *bus = priv->phy->bus;
  165. mutex_lock(&bus->mdio_lock);
  166. bus->write(bus, phy_addr, MII_ATH_MMD_ADDR, addr);
  167. bus->write(bus, phy_addr, MII_ATH_MMD_DATA, data);
  168. mutex_unlock(&bus->mdio_lock);
  169. }
  170. static u32
  171. ar8216_rmw(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
  172. {
  173. u32 v;
  174. lockdep_assert_held(&priv->reg_mutex);
  175. v = priv->read(priv, reg);
  176. v &= ~mask;
  177. v |= val;
  178. priv->write(priv, reg, v);
  179. return v;
  180. }
  181. static void
  182. ar8216_read_port_link(struct ar8216_priv *priv, int port,
  183. struct switch_port_link *link)
  184. {
  185. u32 status;
  186. u32 speed;
  187. memset(link, '\0', sizeof(*link));
  188. status = priv->chip->read_port_status(priv, port);
  189. link->aneg = !!(status & AR8216_PORT_STATUS_LINK_AUTO);
  190. if (link->aneg) {
  191. link->link = !!(status & AR8216_PORT_STATUS_LINK_UP);
  192. if (!link->link)
  193. return;
  194. } else {
  195. link->link = true;
  196. }
  197. link->duplex = !!(status & AR8216_PORT_STATUS_DUPLEX);
  198. link->tx_flow = !!(status & AR8216_PORT_STATUS_TXFLOW);
  199. link->rx_flow = !!(status & AR8216_PORT_STATUS_RXFLOW);
  200. speed = (status & AR8216_PORT_STATUS_SPEED) >>
  201. AR8216_PORT_STATUS_SPEED_S;
  202. switch (speed) {
  203. case AR8216_PORT_SPEED_10M:
  204. link->speed = SWITCH_PORT_SPEED_10;
  205. break;
  206. case AR8216_PORT_SPEED_100M:
  207. link->speed = SWITCH_PORT_SPEED_100;
  208. break;
  209. case AR8216_PORT_SPEED_1000M:
  210. link->speed = SWITCH_PORT_SPEED_1000;
  211. break;
  212. default:
  213. link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  214. break;
  215. }
  216. }
  217. static struct sk_buff *
  218. ar8216_mangle_tx(struct net_device *dev, struct sk_buff *skb)
  219. {
  220. struct ar8216_priv *priv = dev->phy_ptr;
  221. unsigned char *buf;
  222. if (unlikely(!priv))
  223. goto error;
  224. if (!priv->vlan)
  225. goto send;
  226. if (unlikely(skb_headroom(skb) < 2)) {
  227. if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
  228. goto error;
  229. }
  230. buf = skb_push(skb, 2);
  231. buf[0] = 0x10;
  232. buf[1] = 0x80;
  233. send:
  234. return skb;
  235. error:
  236. dev_kfree_skb_any(skb);
  237. return NULL;
  238. }
  239. static void
  240. ar8216_mangle_rx(struct net_device *dev, struct sk_buff *skb)
  241. {
  242. struct ar8216_priv *priv;
  243. unsigned char *buf;
  244. int port, vlan;
  245. priv = dev->phy_ptr;
  246. if (!priv)
  247. return;
  248. /* don't strip the header if vlan mode is disabled */
  249. if (!priv->vlan)
  250. return;
  251. /* strip header, get vlan id */
  252. buf = skb->data;
  253. skb_pull(skb, 2);
  254. /* check for vlan header presence */
  255. if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
  256. return;
  257. port = buf[0] & 0xf;
  258. /* no need to fix up packets coming from a tagged source */
  259. if (priv->vlan_tagged & (1 << port))
  260. return;
  261. /* lookup port vid from local table, the switch passes an invalid vlan id */
  262. vlan = priv->vlan_id[priv->pvid[port]];
  263. buf[14 + 2] &= 0xf0;
  264. buf[14 + 2] |= vlan >> 8;
  265. buf[15 + 2] = vlan & 0xff;
  266. }
  267. static int
  268. ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
  269. {
  270. int timeout = 20;
  271. u32 t = 0;
  272. while (1) {
  273. t = priv->read(priv, reg);
  274. if ((t & mask) == val)
  275. return 0;
  276. if (timeout-- <= 0)
  277. break;
  278. udelay(10);
  279. }
  280. pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
  281. (unsigned int) reg, t, mask, val);
  282. return -ETIMEDOUT;
  283. }
  284. static void
  285. ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
  286. {
  287. if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
  288. return;
  289. if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
  290. val &= AR8216_VTUDATA_MEMBER;
  291. val |= AR8216_VTUDATA_VALID;
  292. priv->write(priv, AR8216_REG_VTU_DATA, val);
  293. }
  294. op |= AR8216_VTU_ACTIVE;
  295. priv->write(priv, AR8216_REG_VTU, op);
  296. }
  297. static void
  298. ar8216_vtu_flush(struct ar8216_priv *priv)
  299. {
  300. ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
  301. }
  302. static void
  303. ar8216_vtu_load_vlan(struct ar8216_priv *priv, u32 vid, u32 port_mask)
  304. {
  305. u32 op;
  306. op = AR8216_VTU_OP_LOAD | (vid << AR8216_VTU_VID_S);
  307. ar8216_vtu_op(priv, op, port_mask);
  308. }
  309. static int
  310. ar8216_atu_flush(struct ar8216_priv *priv)
  311. {
  312. int ret;
  313. ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
  314. if (!ret)
  315. priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
  316. return ret;
  317. }
  318. static u32
  319. ar8216_read_port_status(struct ar8216_priv *priv, int port)
  320. {
  321. return priv->read(priv, AR8216_REG_PORT_STATUS(port));
  322. }
  323. static void
  324. ar8216_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
  325. u32 members, u32 pvid)
  326. {
  327. u32 header;
  328. if (chip_is_ar8216(priv) && priv->vlan && port == AR8216_PORT_CPU)
  329. header = AR8216_PORT_CTRL_HEADER;
  330. else
  331. header = 0;
  332. ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
  333. AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
  334. AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
  335. AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
  336. AR8216_PORT_CTRL_LEARN | header |
  337. (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
  338. (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
  339. ar8216_rmw(priv, AR8216_REG_PORT_VLAN(port),
  340. AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
  341. AR8216_PORT_VLAN_DEFAULT_ID,
  342. (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
  343. (ingress << AR8216_PORT_VLAN_MODE_S) |
  344. (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
  345. }
  346. static int
  347. ar8216_hw_init(struct ar8216_priv *priv)
  348. {
  349. return 0;
  350. }
  351. static void
  352. ar8216_init_globals(struct ar8216_priv *priv)
  353. {
  354. /* standard atheros magic */
  355. priv->write(priv, 0x38, 0xc000050e);
  356. ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  357. AR8216_GCTRL_MTU, 1518 + 8 + 2);
  358. }
  359. static void
  360. ar8216_init_port(struct ar8216_priv *priv, int port)
  361. {
  362. /* Enable port learning and tx */
  363. priv->write(priv, AR8216_REG_PORT_CTRL(port),
  364. AR8216_PORT_CTRL_LEARN |
  365. (4 << AR8216_PORT_CTRL_STATE_S));
  366. priv->write(priv, AR8216_REG_PORT_VLAN(port), 0);
  367. if (port == AR8216_PORT_CPU) {
  368. priv->write(priv, AR8216_REG_PORT_STATUS(port),
  369. AR8216_PORT_STATUS_LINK_UP |
  370. (ar8xxx_has_gige(priv) ?
  371. AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
  372. AR8216_PORT_STATUS_TXMAC |
  373. AR8216_PORT_STATUS_RXMAC |
  374. (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_RXFLOW : 0) |
  375. (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_TXFLOW : 0) |
  376. AR8216_PORT_STATUS_DUPLEX);
  377. } else {
  378. priv->write(priv, AR8216_REG_PORT_STATUS(port),
  379. AR8216_PORT_STATUS_LINK_AUTO);
  380. }
  381. }
  382. static const struct ar8xxx_chip ar8216_chip = {
  383. .hw_init = ar8216_hw_init,
  384. .init_globals = ar8216_init_globals,
  385. .init_port = ar8216_init_port,
  386. .setup_port = ar8216_setup_port,
  387. .read_port_status = ar8216_read_port_status,
  388. .atu_flush = ar8216_atu_flush,
  389. .vtu_flush = ar8216_vtu_flush,
  390. .vtu_load_vlan = ar8216_vtu_load_vlan,
  391. };
  392. static void
  393. ar8236_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
  394. u32 members, u32 pvid)
  395. {
  396. ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
  397. AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
  398. AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
  399. AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
  400. AR8216_PORT_CTRL_LEARN |
  401. (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
  402. (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
  403. ar8216_rmw(priv, AR8236_REG_PORT_VLAN(port),
  404. AR8236_PORT_VLAN_DEFAULT_ID,
  405. (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
  406. ar8216_rmw(priv, AR8236_REG_PORT_VLAN2(port),
  407. AR8236_PORT_VLAN2_VLAN_MODE |
  408. AR8236_PORT_VLAN2_MEMBER,
  409. (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
  410. (members << AR8236_PORT_VLAN2_MEMBER_S));
  411. }
  412. static int
  413. ar8236_hw_init(struct ar8216_priv *priv)
  414. {
  415. int i;
  416. struct mii_bus *bus;
  417. if (priv->initialized)
  418. return 0;
  419. /* Initialize the PHYs */
  420. bus = priv->phy->bus;
  421. for (i = 0; i < 5; i++) {
  422. mdiobus_write(bus, i, MII_ADVERTISE,
  423. ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
  424. ADVERTISE_PAUSE_ASYM);
  425. mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  426. }
  427. msleep(1000);
  428. priv->initialized = true;
  429. return 0;
  430. }
  431. static void
  432. ar8236_init_globals(struct ar8216_priv *priv)
  433. {
  434. /* enable jumbo frames */
  435. ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  436. AR8316_GCTRL_MTU, 9018 + 8 + 2);
  437. }
  438. static const struct ar8xxx_chip ar8236_chip = {
  439. .hw_init = ar8236_hw_init,
  440. .init_globals = ar8236_init_globals,
  441. .init_port = ar8216_init_port,
  442. .setup_port = ar8236_setup_port,
  443. .read_port_status = ar8216_read_port_status,
  444. .atu_flush = ar8216_atu_flush,
  445. .vtu_flush = ar8216_vtu_flush,
  446. .vtu_load_vlan = ar8216_vtu_load_vlan,
  447. };
  448. static int
  449. ar8316_hw_init(struct ar8216_priv *priv)
  450. {
  451. int i;
  452. u32 val, newval;
  453. struct mii_bus *bus;
  454. val = priv->read(priv, 0x8);
  455. if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  456. if (priv->port4_phy) {
  457. /* value taken from Ubiquiti RouterStation Pro */
  458. newval = 0x81461bea;
  459. printk(KERN_INFO "ar8316: Using port 4 as PHY\n");
  460. } else {
  461. newval = 0x01261be2;
  462. printk(KERN_INFO "ar8316: Using port 4 as switch port\n");
  463. }
  464. } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
  465. /* value taken from AVM Fritz!Box 7390 sources */
  466. newval = 0x010e5b71;
  467. } else {
  468. /* no known value for phy interface */
  469. printk(KERN_ERR "ar8316: unsupported mii mode: %d.\n",
  470. priv->phy->interface);
  471. return -EINVAL;
  472. }
  473. if (val == newval)
  474. goto out;
  475. priv->write(priv, 0x8, newval);
  476. /* Initialize the ports */
  477. bus = priv->phy->bus;
  478. for (i = 0; i < 5; i++) {
  479. if ((i == 4) && priv->port4_phy &&
  480. priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  481. /* work around for phy4 rgmii mode */
  482. ar8216_phy_dbg_write(priv, i, 0x12, 0x480c);
  483. /* rx delay */
  484. ar8216_phy_dbg_write(priv, i, 0x0, 0x824e);
  485. /* tx delay */
  486. ar8216_phy_dbg_write(priv, i, 0x5, 0x3d47);
  487. msleep(1000);
  488. }
  489. /* initialize the port itself */
  490. mdiobus_write(bus, i, MII_ADVERTISE,
  491. ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
  492. mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
  493. mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  494. msleep(1000);
  495. }
  496. out:
  497. priv->initialized = true;
  498. return 0;
  499. }
  500. static void
  501. ar8316_init_globals(struct ar8216_priv *priv)
  502. {
  503. /* standard atheros magic */
  504. priv->write(priv, 0x38, 0xc000050e);
  505. /* enable cpu port to receive multicast and broadcast frames */
  506. priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
  507. /* enable jumbo frames */
  508. ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  509. AR8316_GCTRL_MTU, 9018 + 8 + 2);
  510. }
  511. static const struct ar8xxx_chip ar8316_chip = {
  512. .caps = AR8XXX_CAP_GIGE,
  513. .hw_init = ar8316_hw_init,
  514. .init_globals = ar8316_init_globals,
  515. .init_port = ar8216_init_port,
  516. .setup_port = ar8216_setup_port,
  517. .read_port_status = ar8216_read_port_status,
  518. .atu_flush = ar8216_atu_flush,
  519. .vtu_flush = ar8216_vtu_flush,
  520. .vtu_load_vlan = ar8216_vtu_load_vlan,
  521. };
  522. static u32
  523. ar8327_get_pad_cfg(struct ar8327_pad_cfg *cfg)
  524. {
  525. u32 t;
  526. if (!cfg)
  527. return 0;
  528. t = 0;
  529. switch (cfg->mode) {
  530. case AR8327_PAD_NC:
  531. break;
  532. case AR8327_PAD_MAC2MAC_MII:
  533. t = AR8327_PAD_MAC_MII_EN;
  534. if (cfg->rxclk_sel)
  535. t |= AR8327_PAD_MAC_MII_RXCLK_SEL;
  536. if (cfg->txclk_sel)
  537. t |= AR8327_PAD_MAC_MII_TXCLK_SEL;
  538. break;
  539. case AR8327_PAD_MAC2MAC_GMII:
  540. t = AR8327_PAD_MAC_GMII_EN;
  541. if (cfg->rxclk_sel)
  542. t |= AR8327_PAD_MAC_GMII_RXCLK_SEL;
  543. if (cfg->txclk_sel)
  544. t |= AR8327_PAD_MAC_GMII_TXCLK_SEL;
  545. break;
  546. case AR8327_PAD_MAC_SGMII:
  547. t = AR8327_PAD_SGMII_EN;
  548. break;
  549. case AR8327_PAD_MAC2PHY_MII:
  550. t = AR8327_PAD_PHY_MII_EN;
  551. if (cfg->rxclk_sel)
  552. t |= AR8327_PAD_PHY_MII_RXCLK_SEL;
  553. if (cfg->txclk_sel)
  554. t |= AR8327_PAD_PHY_MII_TXCLK_SEL;
  555. break;
  556. case AR8327_PAD_MAC2PHY_GMII:
  557. t = AR8327_PAD_PHY_GMII_EN;
  558. if (cfg->pipe_rxclk_sel)
  559. t |= AR8327_PAD_PHY_GMII_PIPE_RXCLK_SEL;
  560. if (cfg->rxclk_sel)
  561. t |= AR8327_PAD_PHY_GMII_RXCLK_SEL;
  562. if (cfg->txclk_sel)
  563. t |= AR8327_PAD_PHY_GMII_TXCLK_SEL;
  564. break;
  565. case AR8327_PAD_MAC_RGMII:
  566. t = AR8327_PAD_RGMII_EN;
  567. t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
  568. t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
  569. if (cfg->rxclk_delay_en)
  570. t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
  571. if (cfg->txclk_delay_en)
  572. t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
  573. break;
  574. case AR8327_PAD_PHY_GMII:
  575. t = AR8327_PAD_PHYX_GMII_EN;
  576. break;
  577. case AR8327_PAD_PHY_RGMII:
  578. t = AR8327_PAD_PHYX_RGMII_EN;
  579. break;
  580. case AR8327_PAD_PHY_MII:
  581. t = AR8327_PAD_PHYX_MII_EN;
  582. break;
  583. }
  584. return t;
  585. }
  586. static void
  587. ar8327_phy_fixup(struct ar8216_priv *priv, int phy)
  588. {
  589. switch (priv->chip_rev) {
  590. case 1:
  591. /* For 100M waveform */
  592. ar8216_phy_dbg_write(priv, phy, 0, 0x02ea);
  593. /* Turn on Gigabit clock */
  594. ar8216_phy_dbg_write(priv, phy, 0x3d, 0x68a0);
  595. break;
  596. case 2:
  597. ar8216_phy_mmd_write(priv, phy, 0x7, 0x3c);
  598. ar8216_phy_mmd_write(priv, phy, 0x4007, 0x0);
  599. /* fallthrough */
  600. case 4:
  601. ar8216_phy_mmd_write(priv, phy, 0x3, 0x800d);
  602. ar8216_phy_mmd_write(priv, phy, 0x4003, 0x803f);
  603. ar8216_phy_dbg_write(priv, phy, 0x3d, 0x6860);
  604. ar8216_phy_dbg_write(priv, phy, 0x5, 0x2c46);
  605. ar8216_phy_dbg_write(priv, phy, 0x3c, 0x6000);
  606. break;
  607. }
  608. }
  609. static int
  610. ar8327_hw_init(struct ar8216_priv *priv)
  611. {
  612. struct ar8327_platform_data *pdata;
  613. u32 t;
  614. int i;
  615. pdata = priv->phy->dev.platform_data;
  616. if (!pdata)
  617. return -EINVAL;
  618. t = ar8327_get_pad_cfg(pdata->pad0_cfg);
  619. priv->write(priv, AR8327_REG_PAD0_MODE, t);
  620. t = ar8327_get_pad_cfg(pdata->pad5_cfg);
  621. priv->write(priv, AR8327_REG_PAD5_MODE, t);
  622. t = ar8327_get_pad_cfg(pdata->pad6_cfg);
  623. priv->write(priv, AR8327_REG_PAD6_MODE, t);
  624. priv->write(priv, AR8327_REG_POWER_ON_STRIP, 0x40000000);
  625. for (i = 0; i < AR8327_NUM_PHYS; i++)
  626. ar8327_phy_fixup(priv, i);
  627. return 0;
  628. }
  629. static void
  630. ar8327_init_globals(struct ar8216_priv *priv)
  631. {
  632. u32 t;
  633. /* enable CPU port and disable mirror port */
  634. t = AR8327_FWD_CTRL0_CPU_PORT_EN |
  635. AR8327_FWD_CTRL0_MIRROR_PORT;
  636. priv->write(priv, AR8327_REG_FWD_CTRL0, t);
  637. /* forward multicast and broadcast frames to CPU */
  638. t = (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_UC_FLOOD_S) |
  639. (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_MC_FLOOD_S) |
  640. (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_BC_FLOOD_S);
  641. priv->write(priv, AR8327_REG_FWD_CTRL1, t);
  642. /* setup MTU */
  643. ar8216_rmw(priv, AR8327_REG_MAX_FRAME_SIZE,
  644. AR8327_MAX_FRAME_SIZE_MTU, 1518 + 8 + 2);
  645. }
  646. static void
  647. ar8327_init_cpuport(struct ar8216_priv *priv)
  648. {
  649. struct ar8327_platform_data *pdata;
  650. struct ar8327_port_cfg *cfg;
  651. u32 t;
  652. pdata = priv->phy->dev.platform_data;
  653. if (!pdata)
  654. return;
  655. cfg = &pdata->cpuport_cfg;
  656. if (!cfg->force_link) {
  657. priv->write(priv, AR8327_REG_PORT_STATUS(AR8216_PORT_CPU),
  658. AR8216_PORT_STATUS_LINK_AUTO);
  659. return;
  660. }
  661. t = AR8216_PORT_STATUS_TXMAC | AR8216_PORT_STATUS_RXMAC;
  662. t |= cfg->duplex ? AR8216_PORT_STATUS_DUPLEX : 0;
  663. t |= cfg->rxpause ? AR8216_PORT_STATUS_RXFLOW : 0;
  664. t |= cfg->txpause ? AR8216_PORT_STATUS_TXFLOW : 0;
  665. switch (cfg->speed) {
  666. case AR8327_PORT_SPEED_10:
  667. t |= AR8216_PORT_SPEED_10M;
  668. break;
  669. case AR8327_PORT_SPEED_100:
  670. t |= AR8216_PORT_SPEED_100M;
  671. break;
  672. case AR8327_PORT_SPEED_1000:
  673. t |= AR8216_PORT_SPEED_1000M;
  674. break;
  675. }
  676. priv->write(priv, AR8327_REG_PORT_STATUS(AR8216_PORT_CPU), t);
  677. }
  678. static void
  679. ar8327_init_port(struct ar8216_priv *priv, int port)
  680. {
  681. u32 t;
  682. if (port == AR8216_PORT_CPU) {
  683. ar8327_init_cpuport(priv);
  684. } else {
  685. t = AR8216_PORT_STATUS_LINK_AUTO;
  686. priv->write(priv, AR8327_REG_PORT_STATUS(port), t);
  687. }
  688. priv->write(priv, AR8327_REG_PORT_HEADER(port), 0);
  689. priv->write(priv, AR8327_REG_PORT_VLAN0(port), 0);
  690. t = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH << AR8327_PORT_VLAN1_OUT_MODE_S;
  691. priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
  692. t = AR8327_PORT_LOOKUP_LEARN;
  693. t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
  694. priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
  695. }
  696. static u32
  697. ar8327_read_port_status(struct ar8216_priv *priv, int port)
  698. {
  699. return priv->read(priv, AR8327_REG_PORT_STATUS(port));
  700. }
  701. static int
  702. ar8327_atu_flush(struct ar8216_priv *priv)
  703. {
  704. int ret;
  705. ret = ar8216_wait_bit(priv, AR8327_REG_ATU_FUNC,
  706. AR8327_ATU_FUNC_BUSY, 0);
  707. if (!ret)
  708. priv->write(priv, AR8327_REG_ATU_FUNC,
  709. AR8327_ATU_FUNC_OP_FLUSH);
  710. return ret;
  711. }
  712. static void
  713. ar8327_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
  714. {
  715. if (ar8216_wait_bit(priv, AR8327_REG_VTU_FUNC1,
  716. AR8327_VTU_FUNC1_BUSY, 0))
  717. return;
  718. if ((op & AR8327_VTU_FUNC1_OP) == AR8327_VTU_FUNC1_OP_LOAD)
  719. priv->write(priv, AR8327_REG_VTU_FUNC0, val);
  720. op |= AR8327_VTU_FUNC1_BUSY;
  721. priv->write(priv, AR8327_REG_VTU_FUNC1, op);
  722. }
  723. static void
  724. ar8327_vtu_flush(struct ar8216_priv *priv)
  725. {
  726. ar8327_vtu_op(priv, AR8327_VTU_FUNC1_OP_FLUSH, 0);
  727. }
  728. static void
  729. ar8327_vtu_load_vlan(struct ar8216_priv *priv, u32 vid, u32 port_mask)
  730. {
  731. u32 op;
  732. u32 val;
  733. int i;
  734. op = AR8327_VTU_FUNC1_OP_LOAD | (vid << AR8327_VTU_FUNC1_VID_S);
  735. val = AR8327_VTU_FUNC0_VALID | AR8327_VTU_FUNC0_IVL;
  736. for (i = 0; i < AR8327_NUM_PORTS; i++) {
  737. u32 mode;
  738. if ((port_mask & BIT(i)) == 0)
  739. mode = AR8327_VTU_FUNC0_EG_MODE_NOT;
  740. else if (priv->vlan == 0)
  741. mode = AR8327_VTU_FUNC0_EG_MODE_KEEP;
  742. else if (priv->vlan_tagged & BIT(i))
  743. mode = AR8327_VTU_FUNC0_EG_MODE_TAG;
  744. else
  745. mode = AR8327_VTU_FUNC0_EG_MODE_UNTAG;
  746. val |= mode << AR8327_VTU_FUNC0_EG_MODE_S(i);
  747. }
  748. ar8327_vtu_op(priv, op, val);
  749. }
  750. static void
  751. ar8327_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
  752. u32 members, u32 pvid)
  753. {
  754. u32 t;
  755. u32 mode;
  756. t = pvid << AR8327_PORT_VLAN0_DEF_SVID_S;
  757. t |= pvid << AR8327_PORT_VLAN0_DEF_CVID_S;
  758. priv->write(priv, AR8327_REG_PORT_VLAN0(port), t);
  759. mode = AR8327_PORT_VLAN1_OUT_MODE_UNMOD;
  760. switch (egress) {
  761. case AR8216_OUT_KEEP:
  762. mode = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH;
  763. break;
  764. case AR8216_OUT_STRIP_VLAN:
  765. mode = AR8327_PORT_VLAN1_OUT_MODE_UNTAG;
  766. break;
  767. case AR8216_OUT_ADD_VLAN:
  768. mode = AR8327_PORT_VLAN1_OUT_MODE_TAG;
  769. break;
  770. }
  771. t = AR8327_PORT_VLAN1_PORT_VLAN_PROP;
  772. t |= mode << AR8327_PORT_VLAN1_OUT_MODE_S;
  773. priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
  774. t = members;
  775. t |= AR8327_PORT_LOOKUP_LEARN;
  776. t |= ingress << AR8327_PORT_LOOKUP_IN_MODE_S;
  777. t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
  778. priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
  779. }
  780. static const struct ar8xxx_chip ar8327_chip = {
  781. .caps = AR8XXX_CAP_GIGE,
  782. .hw_init = ar8327_hw_init,
  783. .init_globals = ar8327_init_globals,
  784. .init_port = ar8327_init_port,
  785. .setup_port = ar8327_setup_port,
  786. .read_port_status = ar8327_read_port_status,
  787. .atu_flush = ar8327_atu_flush,
  788. .vtu_flush = ar8327_vtu_flush,
  789. .vtu_load_vlan = ar8327_vtu_load_vlan,
  790. };
  791. static int
  792. ar8216_sw_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  793. struct switch_val *val)
  794. {
  795. struct ar8216_priv *priv = to_ar8216(dev);
  796. priv->vlan = !!val->value.i;
  797. return 0;
  798. }
  799. static int
  800. ar8216_sw_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  801. struct switch_val *val)
  802. {
  803. struct ar8216_priv *priv = to_ar8216(dev);
  804. val->value.i = priv->vlan;
  805. return 0;
  806. }
  807. static int
  808. ar8216_sw_set_pvid(struct switch_dev *dev, int port, int vlan)
  809. {
  810. struct ar8216_priv *priv = to_ar8216(dev);
  811. /* make sure no invalid PVIDs get set */
  812. if (vlan >= dev->vlans)
  813. return -EINVAL;
  814. priv->pvid[port] = vlan;
  815. return 0;
  816. }
  817. static int
  818. ar8216_sw_get_pvid(struct switch_dev *dev, int port, int *vlan)
  819. {
  820. struct ar8216_priv *priv = to_ar8216(dev);
  821. *vlan = priv->pvid[port];
  822. return 0;
  823. }
  824. static int
  825. ar8216_sw_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
  826. struct switch_val *val)
  827. {
  828. struct ar8216_priv *priv = to_ar8216(dev);
  829. priv->vlan_id[val->port_vlan] = val->value.i;
  830. return 0;
  831. }
  832. static int
  833. ar8216_sw_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
  834. struct switch_val *val)
  835. {
  836. struct ar8216_priv *priv = to_ar8216(dev);
  837. val->value.i = priv->vlan_id[val->port_vlan];
  838. return 0;
  839. }
  840. static int
  841. ar8216_sw_get_port_link(struct switch_dev *dev, int port,
  842. struct switch_port_link *link)
  843. {
  844. struct ar8216_priv *priv = to_ar8216(dev);
  845. ar8216_read_port_link(priv, port, link);
  846. return 0;
  847. }
  848. static int
  849. ar8216_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
  850. {
  851. struct ar8216_priv *priv = to_ar8216(dev);
  852. u8 ports = priv->vlan_table[val->port_vlan];
  853. int i;
  854. val->len = 0;
  855. for (i = 0; i < dev->ports; i++) {
  856. struct switch_port *p;
  857. if (!(ports & (1 << i)))
  858. continue;
  859. p = &val->value.ports[val->len++];
  860. p->id = i;
  861. if (priv->vlan_tagged & (1 << i))
  862. p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
  863. else
  864. p->flags = 0;
  865. }
  866. return 0;
  867. }
  868. static int
  869. ar8216_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
  870. {
  871. struct ar8216_priv *priv = to_ar8216(dev);
  872. u8 *vt = &priv->vlan_table[val->port_vlan];
  873. int i, j;
  874. *vt = 0;
  875. for (i = 0; i < val->len; i++) {
  876. struct switch_port *p = &val->value.ports[i];
  877. if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
  878. priv->vlan_tagged |= (1 << p->id);
  879. } else {
  880. priv->vlan_tagged &= ~(1 << p->id);
  881. priv->pvid[p->id] = val->port_vlan;
  882. /* make sure that an untagged port does not
  883. * appear in other vlans */
  884. for (j = 0; j < AR8X16_MAX_VLANS; j++) {
  885. if (j == val->port_vlan)
  886. continue;
  887. priv->vlan_table[j] &= ~(1 << p->id);
  888. }
  889. }
  890. *vt |= 1 << p->id;
  891. }
  892. return 0;
  893. }
  894. static int
  895. ar8216_sw_hw_apply(struct switch_dev *dev)
  896. {
  897. struct ar8216_priv *priv = to_ar8216(dev);
  898. u8 portmask[AR8X16_MAX_PORTS];
  899. int i, j;
  900. mutex_lock(&priv->reg_mutex);
  901. /* flush all vlan translation unit entries */
  902. priv->chip->vtu_flush(priv);
  903. memset(portmask, 0, sizeof(portmask));
  904. if (!priv->init) {
  905. /* calculate the port destination masks and load vlans
  906. * into the vlan translation unit */
  907. for (j = 0; j < AR8X16_MAX_VLANS; j++) {
  908. u8 vp = priv->vlan_table[j];
  909. if (!vp)
  910. continue;
  911. for (i = 0; i < dev->ports; i++) {
  912. u8 mask = (1 << i);
  913. if (vp & mask)
  914. portmask[i] |= vp & ~mask;
  915. }
  916. priv->chip->vtu_load_vlan(priv, priv->vlan_id[j],
  917. priv->vlan_table[j]);
  918. }
  919. } else {
  920. /* vlan disabled:
  921. * isolate all ports, but connect them to the cpu port */
  922. for (i = 0; i < dev->ports; i++) {
  923. if (i == AR8216_PORT_CPU)
  924. continue;
  925. portmask[i] = 1 << AR8216_PORT_CPU;
  926. portmask[AR8216_PORT_CPU] |= (1 << i);
  927. }
  928. }
  929. /* update the port destination mask registers and tag settings */
  930. for (i = 0; i < dev->ports; i++) {
  931. int egress, ingress;
  932. int pvid;
  933. if (priv->vlan) {
  934. pvid = priv->vlan_id[priv->pvid[i]];
  935. if (priv->vlan_tagged & (1 << i))
  936. egress = AR8216_OUT_ADD_VLAN;
  937. else
  938. egress = AR8216_OUT_STRIP_VLAN;
  939. ingress = AR8216_IN_SECURE;
  940. } else {
  941. pvid = i;
  942. egress = AR8216_OUT_KEEP;
  943. ingress = AR8216_IN_PORT_ONLY;
  944. }
  945. priv->chip->setup_port(priv, i, egress, ingress, portmask[i],
  946. pvid);
  947. }
  948. mutex_unlock(&priv->reg_mutex);
  949. return 0;
  950. }
  951. static int
  952. ar8216_sw_reset_switch(struct switch_dev *dev)
  953. {
  954. struct ar8216_priv *priv = to_ar8216(dev);
  955. int i;
  956. mutex_lock(&priv->reg_mutex);
  957. memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
  958. offsetof(struct ar8216_priv, vlan));
  959. for (i = 0; i < AR8X16_MAX_VLANS; i++)
  960. priv->vlan_id[i] = i;
  961. /* Configure all ports */
  962. for (i = 0; i < dev->ports; i++)
  963. priv->chip->init_port(priv, i);
  964. priv->chip->init_globals(priv);
  965. mutex_unlock(&priv->reg_mutex);
  966. return ar8216_sw_hw_apply(dev);
  967. }
  968. static struct switch_attr ar8216_globals[] = {
  969. {
  970. .type = SWITCH_TYPE_INT,
  971. .name = "enable_vlan",
  972. .description = "Enable VLAN mode",
  973. .set = ar8216_sw_set_vlan,
  974. .get = ar8216_sw_get_vlan,
  975. .max = 1
  976. },
  977. };
  978. static struct switch_attr ar8216_port[] = {
  979. };
  980. static struct switch_attr ar8216_vlan[] = {
  981. {
  982. .type = SWITCH_TYPE_INT,
  983. .name = "vid",
  984. .description = "VLAN ID (0-4094)",
  985. .set = ar8216_sw_set_vid,
  986. .get = ar8216_sw_get_vid,
  987. .max = 4094,
  988. },
  989. };
  990. static const struct switch_dev_ops ar8216_sw_ops = {
  991. .attr_global = {
  992. .attr = ar8216_globals,
  993. .n_attr = ARRAY_SIZE(ar8216_globals),
  994. },
  995. .attr_port = {
  996. .attr = ar8216_port,
  997. .n_attr = ARRAY_SIZE(ar8216_port),
  998. },
  999. .attr_vlan = {
  1000. .attr = ar8216_vlan,
  1001. .n_attr = ARRAY_SIZE(ar8216_vlan),
  1002. },
  1003. .get_port_pvid = ar8216_sw_get_pvid,
  1004. .set_port_pvid = ar8216_sw_set_pvid,
  1005. .get_vlan_ports = ar8216_sw_get_ports,
  1006. .set_vlan_ports = ar8216_sw_set_ports,
  1007. .apply_config = ar8216_sw_hw_apply,
  1008. .reset_switch = ar8216_sw_reset_switch,
  1009. .get_port_link = ar8216_sw_get_port_link,
  1010. };
  1011. static int
  1012. ar8216_id_chip(struct ar8216_priv *priv)
  1013. {
  1014. u32 val;
  1015. u16 id;
  1016. int i;
  1017. val = ar8216_mii_read(priv, AR8216_REG_CTRL);
  1018. if (val == ~0)
  1019. return -ENODEV;
  1020. id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  1021. for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
  1022. u16 t;
  1023. val = ar8216_mii_read(priv, AR8216_REG_CTRL);
  1024. if (val == ~0)
  1025. return -ENODEV;
  1026. t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  1027. if (t != id)
  1028. return -ENODEV;
  1029. }
  1030. priv->chip_ver = (id & AR8216_CTRL_VERSION) >> AR8216_CTRL_VERSION_S;
  1031. priv->chip_rev = (id & AR8216_CTRL_REVISION);
  1032. switch (priv->chip_ver) {
  1033. case AR8XXX_VER_AR8216:
  1034. priv->chip = &ar8216_chip;
  1035. break;
  1036. case AR8XXX_VER_AR8236:
  1037. priv->chip = &ar8236_chip;
  1038. break;
  1039. case AR8XXX_VER_AR8316:
  1040. priv->chip = &ar8316_chip;
  1041. break;
  1042. case AR8XXX_VER_AR8327:
  1043. priv->mii_lo_first = true;
  1044. priv->chip = &ar8327_chip;
  1045. break;
  1046. default:
  1047. printk(KERN_DEBUG
  1048. "ar8216: Unknown Atheros device [ver=%d, rev=%d, phy_id=%04x%04x]\n",
  1049. priv->chip_ver, priv->chip_rev,
  1050. mdiobus_read(priv->phy->bus, priv->phy->addr, 2),
  1051. mdiobus_read(priv->phy->bus, priv->phy->addr, 3));
  1052. return -ENODEV;
  1053. }
  1054. return 0;
  1055. }
  1056. static int
  1057. ar8216_config_init(struct phy_device *pdev)
  1058. {
  1059. struct ar8216_priv *priv = pdev->priv;
  1060. struct net_device *dev = pdev->attached_dev;
  1061. struct switch_dev *swdev;
  1062. int ret;
  1063. if (!priv) {
  1064. priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
  1065. if (priv == NULL)
  1066. return -ENOMEM;
  1067. }
  1068. priv->phy = pdev;
  1069. ret = ar8216_id_chip(priv);
  1070. if (ret)
  1071. goto err_free_priv;
  1072. if (pdev->addr != 0) {
  1073. if (ar8xxx_has_gige(priv)) {
  1074. pdev->supported |= SUPPORTED_1000baseT_Full;
  1075. pdev->advertising |= ADVERTISED_1000baseT_Full;
  1076. }
  1077. if (chip_is_ar8316(priv)) {
  1078. /* check if we're attaching to the switch twice */
  1079. pdev = pdev->bus->phy_map[0];
  1080. if (!pdev) {
  1081. kfree(priv);
  1082. return 0;
  1083. }
  1084. /* switch device has not been initialized, reuse priv */
  1085. if (!pdev->priv) {
  1086. priv->port4_phy = true;
  1087. pdev->priv = priv;
  1088. return 0;
  1089. }
  1090. kfree(priv);
  1091. /* switch device has been initialized, reinit */
  1092. priv = pdev->priv;
  1093. priv->dev.ports = (AR8216_NUM_PORTS - 1);
  1094. priv->initialized = false;
  1095. priv->port4_phy = true;
  1096. ar8316_hw_init(priv);
  1097. return 0;
  1098. }
  1099. kfree(priv);
  1100. return 0;
  1101. }
  1102. if (ar8xxx_has_gige(priv))
  1103. pdev->supported = SUPPORTED_1000baseT_Full;
  1104. else
  1105. pdev->supported = SUPPORTED_100baseT_Full;
  1106. pdev->advertising = pdev->supported;
  1107. mutex_init(&priv->reg_mutex);
  1108. priv->read = ar8216_mii_read;
  1109. priv->write = ar8216_mii_write;
  1110. pdev->priv = priv;
  1111. swdev = &priv->dev;
  1112. swdev->cpu_port = AR8216_PORT_CPU;
  1113. swdev->ops = &ar8216_sw_ops;
  1114. swdev->ports = AR8216_NUM_PORTS;
  1115. if (chip_is_ar8316(priv)) {
  1116. swdev->name = "Atheros AR8316";
  1117. swdev->vlans = AR8X16_MAX_VLANS;
  1118. if (priv->port4_phy) {
  1119. /* port 5 connected to the other mac, therefore unusable */
  1120. swdev->ports = (AR8216_NUM_PORTS - 1);
  1121. }
  1122. } else if (chip_is_ar8236(priv)) {
  1123. swdev->name = "Atheros AR8236";
  1124. swdev->vlans = AR8216_NUM_VLANS;
  1125. swdev->ports = AR8216_NUM_PORTS;
  1126. } else if (chip_is_ar8327(priv)) {
  1127. swdev->name = "Atheros AR8327";
  1128. swdev->vlans = AR8X16_MAX_VLANS;
  1129. swdev->ports = AR8327_NUM_PORTS;
  1130. } else {
  1131. swdev->name = "Atheros AR8216";
  1132. swdev->vlans = AR8216_NUM_VLANS;
  1133. }
  1134. ret = register_switch(&priv->dev, pdev->attached_dev);
  1135. if (ret)
  1136. goto err_free_priv;
  1137. printk(KERN_INFO "%s: %s switch driver attached.\n",
  1138. pdev->attached_dev->name, swdev->name);
  1139. priv->init = true;
  1140. ret = priv->chip->hw_init(priv);
  1141. if (ret)
  1142. goto err_free_priv;
  1143. ret = ar8216_sw_reset_switch(&priv->dev);
  1144. if (ret)
  1145. goto err_free_priv;
  1146. dev->phy_ptr = priv;
  1147. /* VID fixup only needed on ar8216 */
  1148. if (chip_is_ar8216(priv) && pdev->addr == 0) {
  1149. dev->priv_flags |= IFF_NO_IP_ALIGN;
  1150. dev->eth_mangle_rx = ar8216_mangle_rx;
  1151. dev->eth_mangle_tx = ar8216_mangle_tx;
  1152. }
  1153. priv->init = false;
  1154. return 0;
  1155. err_free_priv:
  1156. kfree(priv);
  1157. return ret;
  1158. }
  1159. static int
  1160. ar8216_read_status(struct phy_device *phydev)
  1161. {
  1162. struct ar8216_priv *priv = phydev->priv;
  1163. struct switch_port_link link;
  1164. int ret;
  1165. if (phydev->addr != 0)
  1166. return genphy_read_status(phydev);
  1167. ar8216_read_port_link(priv, phydev->addr, &link);
  1168. phydev->link = !!link.link;
  1169. if (!phydev->link)
  1170. return 0;
  1171. switch (link.speed) {
  1172. case SWITCH_PORT_SPEED_10:
  1173. phydev->speed = SPEED_10;
  1174. break;
  1175. case SWITCH_PORT_SPEED_100:
  1176. phydev->speed = SPEED_100;
  1177. break;
  1178. case SWITCH_PORT_SPEED_1000:
  1179. phydev->speed = SPEED_1000;
  1180. break;
  1181. default:
  1182. phydev->speed = 0;
  1183. }
  1184. phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
  1185. /* flush the address translation unit */
  1186. mutex_lock(&priv->reg_mutex);
  1187. ret = priv->chip->atu_flush(priv);
  1188. mutex_unlock(&priv->reg_mutex);
  1189. phydev->state = PHY_RUNNING;
  1190. netif_carrier_on(phydev->attached_dev);
  1191. phydev->adjust_link(phydev->attached_dev);
  1192. return ret;
  1193. }
  1194. static int
  1195. ar8216_config_aneg(struct phy_device *phydev)
  1196. {
  1197. if (phydev->addr == 0)
  1198. return 0;
  1199. return genphy_config_aneg(phydev);
  1200. }
  1201. static int
  1202. ar8216_probe(struct phy_device *pdev)
  1203. {
  1204. struct ar8216_priv priv;
  1205. priv.phy = pdev;
  1206. return ar8216_id_chip(&priv);
  1207. }
  1208. static void
  1209. ar8216_remove(struct phy_device *pdev)
  1210. {
  1211. struct ar8216_priv *priv = pdev->priv;
  1212. struct net_device *dev = pdev->attached_dev;
  1213. if (!priv)
  1214. return;
  1215. dev->priv_flags &= ~IFF_NO_IP_ALIGN;
  1216. dev->eth_mangle_rx = NULL;
  1217. dev->eth_mangle_tx = NULL;
  1218. if (pdev->addr == 0)
  1219. unregister_switch(&priv->dev);
  1220. kfree(priv);
  1221. }
  1222. static struct phy_driver ar8216_driver = {
  1223. .phy_id = 0x004d0000,
  1224. .name = "Atheros AR8216/AR8236/AR8316",
  1225. .phy_id_mask = 0xffff0000,
  1226. .features = PHY_BASIC_FEATURES,
  1227. .probe = ar8216_probe,
  1228. .remove = ar8216_remove,
  1229. .config_init = &ar8216_config_init,
  1230. .config_aneg = &ar8216_config_aneg,
  1231. .read_status = &ar8216_read_status,
  1232. .driver = { .owner = THIS_MODULE },
  1233. };
  1234. int __init
  1235. ar8216_init(void)
  1236. {
  1237. return phy_driver_register(&ar8216_driver);
  1238. }
  1239. void __exit
  1240. ar8216_exit(void)
  1241. {
  1242. phy_driver_unregister(&ar8216_driver);
  1243. }
  1244. module_init(ar8216_init);
  1245. module_exit(ar8216_exit);
  1246. MODULE_LICENSE("GPL");