adm6996.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. /*
  2. * ADM6996 switch driver
  3. *
  4. * swconfig interface based on ar8216.c
  5. *
  6. * Copyright (c) 2008 Felix Fietkau <nbd@nbd.name>
  7. * VLAN support Copyright (c) 2010, 2011 Peter Lebbing <peter@digitalbrains.com>
  8. * Copyright (c) 2013 Hauke Mehrtens <hauke@hauke-m.de>
  9. * Copyright (c) 2014 Matti Laakso <malaakso@elisanet.fi>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License v2 as published by the
  13. * Free Software Foundation
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. /*#define DEBUG 1*/
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/errno.h>
  20. #include <linux/unistd.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/gpio.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/mm.h>
  31. #include <linux/module.h>
  32. #include <linux/mii.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/platform_data/adm6996-gpio.h>
  35. #include <linux/ethtool.h>
  36. #include <linux/phy.h>
  37. #include <linux/switch.h>
  38. #include <asm/io.h>
  39. #include <asm/irq.h>
  40. #include <asm/uaccess.h>
  41. #include "adm6996.h"
  42. MODULE_DESCRIPTION("Infineon ADM6996 Switch");
  43. MODULE_AUTHOR("Felix Fietkau, Peter Lebbing <peter@digitalbrains.com>");
  44. MODULE_LICENSE("GPL");
  45. static const char * const adm6996_model_name[] =
  46. {
  47. NULL,
  48. "ADM6996FC",
  49. "ADM6996M",
  50. "ADM6996L"
  51. };
  52. struct adm6996_mib_desc {
  53. unsigned int offset;
  54. const char *name;
  55. };
  56. struct adm6996_priv {
  57. struct switch_dev dev;
  58. void *priv;
  59. u8 eecs;
  60. u8 eesk;
  61. u8 eedi;
  62. u8 eerc;
  63. enum adm6996_model model;
  64. bool enable_vlan;
  65. bool vlan_enabled; /* Current hardware state */
  66. #ifdef DEBUG
  67. u16 addr; /* Debugging: register address to operate on */
  68. #endif
  69. u16 pvid[ADM_NUM_PORTS]; /* Primary VLAN ID */
  70. u8 tagged_ports;
  71. u16 vlan_id[ADM_NUM_VLANS];
  72. u8 vlan_table[ADM_NUM_VLANS]; /* bitmap, 1 = port is member */
  73. u8 vlan_tagged[ADM_NUM_VLANS]; /* bitmap, 1 = tagged member */
  74. struct mutex mib_lock;
  75. char buf[2048];
  76. struct mutex reg_mutex;
  77. /* use abstraction for regops, we want to add gpio support in the future */
  78. u16 (*read)(struct adm6996_priv *priv, enum admreg reg);
  79. void (*write)(struct adm6996_priv *priv, enum admreg reg, u16 val);
  80. };
  81. #define to_adm(_dev) container_of(_dev, struct adm6996_priv, dev)
  82. #define phy_to_adm(_phy) ((struct adm6996_priv *) (_phy)->priv)
  83. #define MIB_DESC(_o, _n) \
  84. { \
  85. .offset = (_o), \
  86. .name = (_n), \
  87. }
  88. static const struct adm6996_mib_desc adm6996_mibs[] = {
  89. MIB_DESC(ADM_CL0, "RxPacket"),
  90. MIB_DESC(ADM_CL6, "RxByte"),
  91. MIB_DESC(ADM_CL12, "TxPacket"),
  92. MIB_DESC(ADM_CL18, "TxByte"),
  93. MIB_DESC(ADM_CL24, "Collision"),
  94. MIB_DESC(ADM_CL30, "Error"),
  95. };
  96. static inline u16
  97. r16(struct adm6996_priv *priv, enum admreg reg)
  98. {
  99. return priv->read(priv, reg);
  100. }
  101. static inline void
  102. w16(struct adm6996_priv *priv, enum admreg reg, u16 val)
  103. {
  104. priv->write(priv, reg, val);
  105. }
  106. /* Minimum timing constants */
  107. #define EECK_EDGE_TIME 3 /* 3us - max(adm 2.5us, 93c 1us) */
  108. #define EEDI_SETUP_TIME 1 /* 1us - max(adm 10ns, 93c 400ns) */
  109. #define EECS_SETUP_TIME 1 /* 1us - max(adm no, 93c 200ns) */
  110. static void adm6996_gpio_write(struct adm6996_priv *priv, int cs, char *buf, unsigned int bits)
  111. {
  112. int i, len = (bits + 7) / 8;
  113. u8 mask;
  114. gpio_set_value(priv->eecs, cs);
  115. udelay(EECK_EDGE_TIME);
  116. /* Byte assemble from MSB to LSB */
  117. for (i = 0; i < len; i++) {
  118. /* Bit bang from MSB to LSB */
  119. for (mask = 0x80; mask && bits > 0; mask >>= 1, bits --) {
  120. /* Clock low */
  121. gpio_set_value(priv->eesk, 0);
  122. udelay(EECK_EDGE_TIME);
  123. /* Output on rising edge */
  124. gpio_set_value(priv->eedi, (mask & buf[i]));
  125. udelay(EEDI_SETUP_TIME);
  126. /* Clock high */
  127. gpio_set_value(priv->eesk, 1);
  128. udelay(EECK_EDGE_TIME);
  129. }
  130. }
  131. /* Clock low */
  132. gpio_set_value(priv->eesk, 0);
  133. udelay(EECK_EDGE_TIME);
  134. if (cs)
  135. gpio_set_value(priv->eecs, 0);
  136. }
  137. static void adm6996_gpio_read(struct adm6996_priv *priv, int cs, char *buf, unsigned int bits)
  138. {
  139. int i, len = (bits + 7) / 8;
  140. u8 mask;
  141. gpio_set_value(priv->eecs, cs);
  142. udelay(EECK_EDGE_TIME);
  143. /* Byte assemble from MSB to LSB */
  144. for (i = 0; i < len; i++) {
  145. u8 byte;
  146. /* Bit bang from MSB to LSB */
  147. for (mask = 0x80, byte = 0; mask && bits > 0; mask >>= 1, bits --) {
  148. u8 gp;
  149. /* Clock low */
  150. gpio_set_value(priv->eesk, 0);
  151. udelay(EECK_EDGE_TIME);
  152. /* Input on rising edge */
  153. gp = gpio_get_value(priv->eedi);
  154. if (gp)
  155. byte |= mask;
  156. /* Clock high */
  157. gpio_set_value(priv->eesk, 1);
  158. udelay(EECK_EDGE_TIME);
  159. }
  160. *buf++ = byte;
  161. }
  162. /* Clock low */
  163. gpio_set_value(priv->eesk, 0);
  164. udelay(EECK_EDGE_TIME);
  165. if (cs)
  166. gpio_set_value(priv->eecs, 0);
  167. }
  168. /* Advance clock(s) */
  169. static void adm6996_gpio_adclk(struct adm6996_priv *priv, int clocks)
  170. {
  171. int i;
  172. for (i = 0; i < clocks; i++) {
  173. /* Clock high */
  174. gpio_set_value(priv->eesk, 1);
  175. udelay(EECK_EDGE_TIME);
  176. /* Clock low */
  177. gpio_set_value(priv->eesk, 0);
  178. udelay(EECK_EDGE_TIME);
  179. }
  180. }
  181. static u16
  182. adm6996_read_gpio_reg(struct adm6996_priv *priv, enum admreg reg)
  183. {
  184. /* cmd: 01 10 T DD R RRRRRR */
  185. u8 bits[6] = {
  186. 0xFF, 0xFF, 0xFF, 0xFF,
  187. (0x06 << 4) | ((0 & 0x01) << 3 | (reg&64)>>6),
  188. ((reg&63)<<2)
  189. };
  190. u8 rbits[4];
  191. /* Enable GPIO outputs with all pins to 0 */
  192. gpio_direction_output(priv->eecs, 0);
  193. gpio_direction_output(priv->eesk, 0);
  194. gpio_direction_output(priv->eedi, 0);
  195. adm6996_gpio_write(priv, 0, bits, 46);
  196. gpio_direction_input(priv->eedi);
  197. adm6996_gpio_adclk(priv, 2);
  198. adm6996_gpio_read(priv, 0, rbits, 32);
  199. /* Extra clock(s) required per datasheet */
  200. adm6996_gpio_adclk(priv, 2);
  201. /* Disable GPIO outputs */
  202. gpio_direction_input(priv->eecs);
  203. gpio_direction_input(priv->eesk);
  204. /* EEPROM has 16-bit registers, but pumps out two registers in one request */
  205. return (reg & 0x01 ? (rbits[0]<<8) | rbits[1] : (rbits[2]<<8) | (rbits[3]));
  206. }
  207. /* Write chip configuration register */
  208. /* Follow 93c66 timing and chip's min EEPROM timing requirement */
  209. static void
  210. adm6996_write_gpio_reg(struct adm6996_priv *priv, enum admreg reg, u16 val)
  211. {
  212. /* cmd(27bits): sb(1) + opc(01) + addr(bbbbbbbb) + data(bbbbbbbbbbbbbbbb) */
  213. u8 bits[4] = {
  214. (0x05 << 5) | (reg >> 3),
  215. (reg << 5) | (u8)(val >> 11),
  216. (u8)(val >> 3),
  217. (u8)(val << 5)
  218. };
  219. /* Enable GPIO outputs with all pins to 0 */
  220. gpio_direction_output(priv->eecs, 0);
  221. gpio_direction_output(priv->eesk, 0);
  222. gpio_direction_output(priv->eedi, 0);
  223. /* Write cmd. Total 27 bits */
  224. adm6996_gpio_write(priv, 1, bits, 27);
  225. /* Extra clock(s) required per datasheet */
  226. adm6996_gpio_adclk(priv, 2);
  227. /* Disable GPIO outputs */
  228. gpio_direction_input(priv->eecs);
  229. gpio_direction_input(priv->eesk);
  230. gpio_direction_input(priv->eedi);
  231. }
  232. static u16
  233. adm6996_read_mii_reg(struct adm6996_priv *priv, enum admreg reg)
  234. {
  235. struct phy_device *phydev = priv->priv;
  236. struct mii_bus *bus = phydev->bus;
  237. return bus->read(bus, PHYADDR(reg));
  238. }
  239. static void
  240. adm6996_write_mii_reg(struct adm6996_priv *priv, enum admreg reg, u16 val)
  241. {
  242. struct phy_device *phydev = priv->priv;
  243. struct mii_bus *bus = phydev->bus;
  244. bus->write(bus, PHYADDR(reg), val);
  245. }
  246. static int
  247. adm6996_set_enable_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  248. struct switch_val *val)
  249. {
  250. struct adm6996_priv *priv = to_adm(dev);
  251. if (val->value.i > 1)
  252. return -EINVAL;
  253. priv->enable_vlan = val->value.i;
  254. return 0;
  255. };
  256. static int
  257. adm6996_get_enable_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  258. struct switch_val *val)
  259. {
  260. struct adm6996_priv *priv = to_adm(dev);
  261. val->value.i = priv->enable_vlan;
  262. return 0;
  263. };
  264. #ifdef DEBUG
  265. static int
  266. adm6996_set_addr(struct switch_dev *dev, const struct switch_attr *attr,
  267. struct switch_val *val)
  268. {
  269. struct adm6996_priv *priv = to_adm(dev);
  270. if (val->value.i > 1023)
  271. return -EINVAL;
  272. priv->addr = val->value.i;
  273. return 0;
  274. };
  275. static int
  276. adm6996_get_addr(struct switch_dev *dev, const struct switch_attr *attr,
  277. struct switch_val *val)
  278. {
  279. struct adm6996_priv *priv = to_adm(dev);
  280. val->value.i = priv->addr;
  281. return 0;
  282. };
  283. static int
  284. adm6996_set_data(struct switch_dev *dev, const struct switch_attr *attr,
  285. struct switch_val *val)
  286. {
  287. struct adm6996_priv *priv = to_adm(dev);
  288. if (val->value.i > 65535)
  289. return -EINVAL;
  290. w16(priv, priv->addr, val->value.i);
  291. return 0;
  292. };
  293. static int
  294. adm6996_get_data(struct switch_dev *dev, const struct switch_attr *attr,
  295. struct switch_val *val)
  296. {
  297. struct adm6996_priv *priv = to_adm(dev);
  298. val->value.i = r16(priv, priv->addr);
  299. return 0;
  300. };
  301. #endif /* def DEBUG */
  302. static int
  303. adm6996_set_pvid(struct switch_dev *dev, int port, int vlan)
  304. {
  305. struct adm6996_priv *priv = to_adm(dev);
  306. pr_devel("set_pvid port %d vlan %d\n", port, vlan);
  307. if (vlan > ADM_VLAN_MAX_ID)
  308. return -EINVAL;
  309. priv->pvid[port] = vlan;
  310. return 0;
  311. }
  312. static int
  313. adm6996_get_pvid(struct switch_dev *dev, int port, int *vlan)
  314. {
  315. struct adm6996_priv *priv = to_adm(dev);
  316. pr_devel("get_pvid port %d\n", port);
  317. *vlan = priv->pvid[port];
  318. return 0;
  319. }
  320. static int
  321. adm6996_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
  322. struct switch_val *val)
  323. {
  324. struct adm6996_priv *priv = to_adm(dev);
  325. pr_devel("set_vid port %d vid %d\n", val->port_vlan, val->value.i);
  326. if (val->value.i > ADM_VLAN_MAX_ID)
  327. return -EINVAL;
  328. priv->vlan_id[val->port_vlan] = val->value.i;
  329. return 0;
  330. };
  331. static int
  332. adm6996_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
  333. struct switch_val *val)
  334. {
  335. struct adm6996_priv *priv = to_adm(dev);
  336. pr_devel("get_vid port %d\n", val->port_vlan);
  337. val->value.i = priv->vlan_id[val->port_vlan];
  338. return 0;
  339. };
  340. static int
  341. adm6996_get_ports(struct switch_dev *dev, struct switch_val *val)
  342. {
  343. struct adm6996_priv *priv = to_adm(dev);
  344. u8 ports = priv->vlan_table[val->port_vlan];
  345. u8 tagged = priv->vlan_tagged[val->port_vlan];
  346. int i;
  347. pr_devel("get_ports port_vlan %d\n", val->port_vlan);
  348. val->len = 0;
  349. for (i = 0; i < ADM_NUM_PORTS; i++) {
  350. struct switch_port *p;
  351. if (!(ports & (1 << i)))
  352. continue;
  353. p = &val->value.ports[val->len++];
  354. p->id = i;
  355. if (tagged & (1 << i))
  356. p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
  357. else
  358. p->flags = 0;
  359. }
  360. return 0;
  361. };
  362. static int
  363. adm6996_set_ports(struct switch_dev *dev, struct switch_val *val)
  364. {
  365. struct adm6996_priv *priv = to_adm(dev);
  366. u8 *ports = &priv->vlan_table[val->port_vlan];
  367. u8 *tagged = &priv->vlan_tagged[val->port_vlan];
  368. int i;
  369. pr_devel("set_ports port_vlan %d ports", val->port_vlan);
  370. *ports = 0;
  371. *tagged = 0;
  372. for (i = 0; i < val->len; i++) {
  373. struct switch_port *p = &val->value.ports[i];
  374. #ifdef DEBUG
  375. pr_cont(" %d%s", p->id,
  376. ((p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) ? "T" :
  377. ""));
  378. #endif
  379. if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
  380. *tagged |= (1 << p->id);
  381. priv->tagged_ports |= (1 << p->id);
  382. }
  383. *ports |= (1 << p->id);
  384. }
  385. #ifdef DEBUG
  386. pr_cont("\n");
  387. #endif
  388. return 0;
  389. };
  390. /*
  391. * Precondition: reg_mutex must be held
  392. */
  393. static void
  394. adm6996_enable_vlan(struct adm6996_priv *priv)
  395. {
  396. u16 reg;
  397. reg = r16(priv, ADM_OTBE_P2_PVID);
  398. reg &= ~(ADM_OTBE_MASK);
  399. w16(priv, ADM_OTBE_P2_PVID, reg);
  400. reg = r16(priv, ADM_IFNTE);
  401. reg &= ~(ADM_IFNTE_MASK);
  402. w16(priv, ADM_IFNTE, reg);
  403. reg = r16(priv, ADM_VID_CHECK);
  404. reg |= ADM_VID_CHECK_MASK;
  405. w16(priv, ADM_VID_CHECK, reg);
  406. reg = r16(priv, ADM_SYSC0);
  407. reg |= ADM_NTTE;
  408. reg &= ~(ADM_RVID1);
  409. w16(priv, ADM_SYSC0, reg);
  410. reg = r16(priv, ADM_SYSC3);
  411. reg |= ADM_TBV;
  412. w16(priv, ADM_SYSC3, reg);
  413. }
  414. static void
  415. adm6996_enable_vlan_6996l(struct adm6996_priv *priv)
  416. {
  417. u16 reg;
  418. reg = r16(priv, ADM_SYSC3);
  419. reg |= ADM_TBV;
  420. reg |= ADM_MAC_CLONE;
  421. w16(priv, ADM_SYSC3, reg);
  422. }
  423. /*
  424. * Disable VLANs
  425. *
  426. * Sets VLAN mapping for port-based VLAN with all ports connected to
  427. * eachother (this is also the power-on default).
  428. *
  429. * Precondition: reg_mutex must be held
  430. */
  431. static void
  432. adm6996_disable_vlan(struct adm6996_priv *priv)
  433. {
  434. u16 reg;
  435. int i;
  436. for (i = 0; i < ADM_NUM_VLANS; i++) {
  437. reg = ADM_VLAN_FILT_MEMBER_MASK;
  438. w16(priv, ADM_VLAN_FILT_L(i), reg);
  439. reg = ADM_VLAN_FILT_VALID | ADM_VLAN_FILT_VID(1);
  440. w16(priv, ADM_VLAN_FILT_H(i), reg);
  441. }
  442. reg = r16(priv, ADM_OTBE_P2_PVID);
  443. reg |= ADM_OTBE_MASK;
  444. w16(priv, ADM_OTBE_P2_PVID, reg);
  445. reg = r16(priv, ADM_IFNTE);
  446. reg |= ADM_IFNTE_MASK;
  447. w16(priv, ADM_IFNTE, reg);
  448. reg = r16(priv, ADM_VID_CHECK);
  449. reg &= ~(ADM_VID_CHECK_MASK);
  450. w16(priv, ADM_VID_CHECK, reg);
  451. reg = r16(priv, ADM_SYSC0);
  452. reg &= ~(ADM_NTTE);
  453. reg |= ADM_RVID1;
  454. w16(priv, ADM_SYSC0, reg);
  455. reg = r16(priv, ADM_SYSC3);
  456. reg &= ~(ADM_TBV);
  457. w16(priv, ADM_SYSC3, reg);
  458. }
  459. /*
  460. * Disable VLANs
  461. *
  462. * Sets VLAN mapping for port-based VLAN with all ports connected to
  463. * eachother (this is also the power-on default).
  464. *
  465. * Precondition: reg_mutex must be held
  466. */
  467. static void
  468. adm6996_disable_vlan_6996l(struct adm6996_priv *priv)
  469. {
  470. u16 reg;
  471. int i;
  472. for (i = 0; i < ADM_NUM_VLANS; i++) {
  473. w16(priv, ADM_VLAN_MAP(i), 0);
  474. }
  475. reg = r16(priv, ADM_SYSC3);
  476. reg &= ~(ADM_TBV);
  477. reg &= ~(ADM_MAC_CLONE);
  478. w16(priv, ADM_SYSC3, reg);
  479. }
  480. /*
  481. * Precondition: reg_mutex must be held
  482. */
  483. static void
  484. adm6996_apply_port_pvids(struct adm6996_priv *priv)
  485. {
  486. u16 reg;
  487. int i;
  488. for (i = 0; i < ADM_NUM_PORTS; i++) {
  489. reg = r16(priv, adm_portcfg[i]);
  490. reg &= ~(ADM_PORTCFG_PVID_MASK);
  491. reg |= ADM_PORTCFG_PVID(priv->pvid[i]);
  492. if (priv->model == ADM6996L) {
  493. if (priv->tagged_ports & (1 << i))
  494. reg |= (1 << 4);
  495. else
  496. reg &= ~(1 << 4);
  497. }
  498. w16(priv, adm_portcfg[i], reg);
  499. }
  500. w16(priv, ADM_P0_PVID, ADM_P0_PVID_VAL(priv->pvid[0]));
  501. w16(priv, ADM_P1_PVID, ADM_P1_PVID_VAL(priv->pvid[1]));
  502. reg = r16(priv, ADM_OTBE_P2_PVID);
  503. reg &= ~(ADM_P2_PVID_MASK);
  504. reg |= ADM_P2_PVID_VAL(priv->pvid[2]);
  505. w16(priv, ADM_OTBE_P2_PVID, reg);
  506. reg = ADM_P3_PVID_VAL(priv->pvid[3]);
  507. reg |= ADM_P4_PVID_VAL(priv->pvid[4]);
  508. w16(priv, ADM_P3_P4_PVID, reg);
  509. reg = r16(priv, ADM_P5_PVID);
  510. reg &= ~(ADM_P2_PVID_MASK);
  511. reg |= ADM_P5_PVID_VAL(priv->pvid[5]);
  512. w16(priv, ADM_P5_PVID, reg);
  513. }
  514. /*
  515. * Precondition: reg_mutex must be held
  516. */
  517. static void
  518. adm6996_apply_vlan_filters(struct adm6996_priv *priv)
  519. {
  520. u8 ports, tagged;
  521. u16 vid, reg;
  522. int i;
  523. for (i = 0; i < ADM_NUM_VLANS; i++) {
  524. vid = priv->vlan_id[i];
  525. ports = priv->vlan_table[i];
  526. tagged = priv->vlan_tagged[i];
  527. if (ports == 0) {
  528. /* Disable VLAN entry */
  529. w16(priv, ADM_VLAN_FILT_H(i), 0);
  530. w16(priv, ADM_VLAN_FILT_L(i), 0);
  531. continue;
  532. }
  533. reg = ADM_VLAN_FILT_MEMBER(ports);
  534. reg |= ADM_VLAN_FILT_TAGGED(tagged);
  535. w16(priv, ADM_VLAN_FILT_L(i), reg);
  536. reg = ADM_VLAN_FILT_VALID | ADM_VLAN_FILT_VID(vid);
  537. w16(priv, ADM_VLAN_FILT_H(i), reg);
  538. }
  539. }
  540. static void
  541. adm6996_apply_vlan_filters_6996l(struct adm6996_priv *priv)
  542. {
  543. u8 ports;
  544. u16 reg;
  545. int i;
  546. for (i = 0; i < ADM_NUM_VLANS; i++) {
  547. ports = priv->vlan_table[i];
  548. if (ports == 0) {
  549. /* Disable VLAN entry */
  550. w16(priv, ADM_VLAN_MAP(i), 0);
  551. continue;
  552. } else {
  553. reg = ADM_VLAN_FILT(ports);
  554. w16(priv, ADM_VLAN_MAP(i), reg);
  555. }
  556. }
  557. }
  558. static int
  559. adm6996_hw_apply(struct switch_dev *dev)
  560. {
  561. struct adm6996_priv *priv = to_adm(dev);
  562. pr_devel("hw_apply\n");
  563. mutex_lock(&priv->reg_mutex);
  564. if (!priv->enable_vlan) {
  565. if (priv->vlan_enabled) {
  566. if (priv->model == ADM6996L)
  567. adm6996_disable_vlan_6996l(priv);
  568. else
  569. adm6996_disable_vlan(priv);
  570. priv->vlan_enabled = 0;
  571. }
  572. goto out;
  573. }
  574. if (!priv->vlan_enabled) {
  575. if (priv->model == ADM6996L)
  576. adm6996_enable_vlan_6996l(priv);
  577. else
  578. adm6996_enable_vlan(priv);
  579. priv->vlan_enabled = 1;
  580. }
  581. adm6996_apply_port_pvids(priv);
  582. if (priv->model == ADM6996L)
  583. adm6996_apply_vlan_filters_6996l(priv);
  584. else
  585. adm6996_apply_vlan_filters(priv);
  586. out:
  587. mutex_unlock(&priv->reg_mutex);
  588. return 0;
  589. }
  590. /*
  591. * Reset the switch
  592. *
  593. * The ADM6996 can't do a software-initiated reset, so we just initialise the
  594. * registers we support in this driver.
  595. *
  596. * Precondition: reg_mutex must be held
  597. */
  598. static void
  599. adm6996_perform_reset (struct adm6996_priv *priv)
  600. {
  601. int i;
  602. /* initialize port and vlan settings */
  603. for (i = 0; i < ADM_NUM_PORTS - 1; i++) {
  604. w16(priv, adm_portcfg[i], ADM_PORTCFG_INIT |
  605. ADM_PORTCFG_PVID(0));
  606. }
  607. w16(priv, adm_portcfg[5], ADM_PORTCFG_CPU);
  608. if (priv->model == ADM6996M || priv->model == ADM6996FC) {
  609. /* reset all PHY ports */
  610. for (i = 0; i < ADM_PHY_PORTS; i++) {
  611. w16(priv, ADM_PHY_PORT(i), ADM_PHYCFG_INIT);
  612. }
  613. }
  614. priv->enable_vlan = 0;
  615. priv->vlan_enabled = 0;
  616. for (i = 0; i < ADM_NUM_PORTS; i++) {
  617. priv->pvid[i] = 0;
  618. }
  619. for (i = 0; i < ADM_NUM_VLANS; i++) {
  620. priv->vlan_id[i] = i;
  621. priv->vlan_table[i] = 0;
  622. priv->vlan_tagged[i] = 0;
  623. }
  624. if (priv->model == ADM6996M) {
  625. /* Clear VLAN priority map so prio's are unused */
  626. w16 (priv, ADM_VLAN_PRIOMAP, 0);
  627. adm6996_disable_vlan(priv);
  628. adm6996_apply_port_pvids(priv);
  629. } else if (priv->model == ADM6996L) {
  630. /* Clear VLAN priority map so prio's are unused */
  631. w16 (priv, ADM_VLAN_PRIOMAP, 0);
  632. adm6996_disable_vlan_6996l(priv);
  633. adm6996_apply_port_pvids(priv);
  634. }
  635. }
  636. static int
  637. adm6996_reset_switch(struct switch_dev *dev)
  638. {
  639. struct adm6996_priv *priv = to_adm(dev);
  640. pr_devel("reset\n");
  641. mutex_lock(&priv->reg_mutex);
  642. adm6996_perform_reset (priv);
  643. mutex_unlock(&priv->reg_mutex);
  644. return 0;
  645. }
  646. static int
  647. adm6996_get_port_link(struct switch_dev *dev, int port,
  648. struct switch_port_link *link)
  649. {
  650. struct adm6996_priv *priv = to_adm(dev);
  651. u16 reg = 0;
  652. if (port >= ADM_NUM_PORTS)
  653. return -EINVAL;
  654. switch (port) {
  655. case 0:
  656. reg = r16(priv, ADM_PS0);
  657. break;
  658. case 1:
  659. reg = r16(priv, ADM_PS0);
  660. reg = reg >> 8;
  661. break;
  662. case 2:
  663. reg = r16(priv, ADM_PS1);
  664. break;
  665. case 3:
  666. reg = r16(priv, ADM_PS1);
  667. reg = reg >> 8;
  668. break;
  669. case 4:
  670. reg = r16(priv, ADM_PS1);
  671. reg = reg >> 12;
  672. break;
  673. case 5:
  674. reg = r16(priv, ADM_PS2);
  675. /* Bits 0, 1, 3 and 4. */
  676. reg = (reg & 3) | ((reg & 24) >> 1);
  677. break;
  678. default:
  679. return -EINVAL;
  680. }
  681. link->link = reg & ADM_PS_LS;
  682. if (!link->link)
  683. return 0;
  684. link->aneg = true;
  685. link->duplex = reg & ADM_PS_DS;
  686. link->tx_flow = reg & ADM_PS_FCS;
  687. link->rx_flow = reg & ADM_PS_FCS;
  688. if (reg & ADM_PS_SS)
  689. link->speed = SWITCH_PORT_SPEED_100;
  690. else
  691. link->speed = SWITCH_PORT_SPEED_10;
  692. return 0;
  693. }
  694. static int
  695. adm6996_sw_get_port_mib(struct switch_dev *dev,
  696. const struct switch_attr *attr,
  697. struct switch_val *val)
  698. {
  699. struct adm6996_priv *priv = to_adm(dev);
  700. int port;
  701. char *buf = priv->buf;
  702. int i, len = 0;
  703. u32 reg = 0;
  704. port = val->port_vlan;
  705. if (port >= ADM_NUM_PORTS)
  706. return -EINVAL;
  707. mutex_lock(&priv->mib_lock);
  708. len += snprintf(buf + len, sizeof(priv->buf) - len,
  709. "Port %d MIB counters\n",
  710. port);
  711. for (i = 0; i < ARRAY_SIZE(adm6996_mibs); i++) {
  712. reg = r16(priv, adm6996_mibs[i].offset + ADM_OFFSET_PORT(port));
  713. reg += r16(priv, adm6996_mibs[i].offset + ADM_OFFSET_PORT(port) + 1) << 16;
  714. len += snprintf(buf + len, sizeof(priv->buf) - len,
  715. "%-12s: %u\n",
  716. adm6996_mibs[i].name,
  717. reg);
  718. }
  719. mutex_unlock(&priv->mib_lock);
  720. val->value.s = buf;
  721. val->len = len;
  722. return 0;
  723. }
  724. static struct switch_attr adm6996_globals[] = {
  725. {
  726. .type = SWITCH_TYPE_INT,
  727. .name = "enable_vlan",
  728. .description = "Enable VLANs",
  729. .set = adm6996_set_enable_vlan,
  730. .get = adm6996_get_enable_vlan,
  731. },
  732. #ifdef DEBUG
  733. {
  734. .type = SWITCH_TYPE_INT,
  735. .name = "addr",
  736. .description =
  737. "Direct register access: set register address (0 - 1023)",
  738. .set = adm6996_set_addr,
  739. .get = adm6996_get_addr,
  740. },
  741. {
  742. .type = SWITCH_TYPE_INT,
  743. .name = "data",
  744. .description =
  745. "Direct register access: read/write to register (0 - 65535)",
  746. .set = adm6996_set_data,
  747. .get = adm6996_get_data,
  748. },
  749. #endif /* def DEBUG */
  750. };
  751. static struct switch_attr adm6996_port[] = {
  752. {
  753. .type = SWITCH_TYPE_STRING,
  754. .name = "mib",
  755. .description = "Get port's MIB counters",
  756. .set = NULL,
  757. .get = adm6996_sw_get_port_mib,
  758. },
  759. };
  760. static struct switch_attr adm6996_vlan[] = {
  761. {
  762. .type = SWITCH_TYPE_INT,
  763. .name = "vid",
  764. .description = "VLAN ID",
  765. .set = adm6996_set_vid,
  766. .get = adm6996_get_vid,
  767. },
  768. };
  769. static struct switch_dev_ops adm6996_ops = {
  770. .attr_global = {
  771. .attr = adm6996_globals,
  772. .n_attr = ARRAY_SIZE(adm6996_globals),
  773. },
  774. .attr_port = {
  775. .attr = adm6996_port,
  776. .n_attr = ARRAY_SIZE(adm6996_port),
  777. },
  778. .attr_vlan = {
  779. .attr = adm6996_vlan,
  780. .n_attr = ARRAY_SIZE(adm6996_vlan),
  781. },
  782. .get_port_pvid = adm6996_get_pvid,
  783. .set_port_pvid = adm6996_set_pvid,
  784. .get_vlan_ports = adm6996_get_ports,
  785. .set_vlan_ports = adm6996_set_ports,
  786. .apply_config = adm6996_hw_apply,
  787. .reset_switch = adm6996_reset_switch,
  788. .get_port_link = adm6996_get_port_link,
  789. };
  790. static int adm6996_switch_init(struct adm6996_priv *priv, const char *alias, struct net_device *netdev)
  791. {
  792. struct switch_dev *swdev;
  793. u16 test, old;
  794. if (!priv->model) {
  795. /* Detect type of chip */
  796. old = r16(priv, ADM_VID_CHECK);
  797. test = old ^ (1 << 12);
  798. w16(priv, ADM_VID_CHECK, test);
  799. test ^= r16(priv, ADM_VID_CHECK);
  800. if (test & (1 << 12)) {
  801. /*
  802. * Bit 12 of this register is read-only.
  803. * This is the FC model.
  804. */
  805. priv->model = ADM6996FC;
  806. } else {
  807. /* Bit 12 is read-write. This is the M model. */
  808. priv->model = ADM6996M;
  809. w16(priv, ADM_VID_CHECK, old);
  810. }
  811. }
  812. swdev = &priv->dev;
  813. swdev->name = (adm6996_model_name[priv->model]);
  814. swdev->cpu_port = ADM_CPU_PORT;
  815. swdev->ports = ADM_NUM_PORTS;
  816. swdev->vlans = ADM_NUM_VLANS;
  817. swdev->ops = &adm6996_ops;
  818. swdev->alias = alias;
  819. /* The ADM6996L connected through GPIOs does not support any switch
  820. status calls */
  821. if (priv->model == ADM6996L) {
  822. adm6996_ops.attr_port.n_attr = 0;
  823. adm6996_ops.get_port_link = NULL;
  824. }
  825. pr_info ("%s: %s model PHY found.\n", alias, swdev->name);
  826. mutex_lock(&priv->reg_mutex);
  827. adm6996_perform_reset (priv);
  828. mutex_unlock(&priv->reg_mutex);
  829. if (priv->model == ADM6996M || priv->model == ADM6996L) {
  830. return register_switch(swdev, netdev);
  831. }
  832. return -ENODEV;
  833. }
  834. static int adm6996_config_init(struct phy_device *pdev)
  835. {
  836. struct adm6996_priv *priv;
  837. int ret;
  838. pdev->supported = ADVERTISED_100baseT_Full;
  839. pdev->advertising = ADVERTISED_100baseT_Full;
  840. if (pdev->addr != 0) {
  841. pr_info ("%s: PHY overlaps ADM6996, providing fixed PHY 0x%x.\n"
  842. , pdev->attached_dev->name, pdev->addr);
  843. return 0;
  844. }
  845. priv = devm_kzalloc(&pdev->dev, sizeof(struct adm6996_priv), GFP_KERNEL);
  846. if (!priv)
  847. return -ENOMEM;
  848. mutex_init(&priv->reg_mutex);
  849. mutex_init(&priv->mib_lock);
  850. priv->priv = pdev;
  851. priv->read = adm6996_read_mii_reg;
  852. priv->write = adm6996_write_mii_reg;
  853. ret = adm6996_switch_init(priv, pdev->attached_dev->name, pdev->attached_dev);
  854. if (ret < 0)
  855. return ret;
  856. pdev->priv = priv;
  857. return 0;
  858. }
  859. /*
  860. * Warning: phydev->priv is NULL if phydev->addr != 0
  861. */
  862. static int adm6996_read_status(struct phy_device *phydev)
  863. {
  864. phydev->speed = SPEED_100;
  865. phydev->duplex = DUPLEX_FULL;
  866. phydev->link = 1;
  867. phydev->state = PHY_RUNNING;
  868. netif_carrier_on(phydev->attached_dev);
  869. phydev->adjust_link(phydev->attached_dev);
  870. return 0;
  871. }
  872. /*
  873. * Warning: phydev->priv is NULL if phydev->addr != 0
  874. */
  875. static int adm6996_config_aneg(struct phy_device *phydev)
  876. {
  877. return 0;
  878. }
  879. static int adm6996_fixup(struct phy_device *dev)
  880. {
  881. struct mii_bus *bus = dev->bus;
  882. u16 reg;
  883. /* Our custom registers are at PHY addresses 0-10. Claim those. */
  884. if (dev->addr > 10)
  885. return 0;
  886. /* look for the switch on the bus */
  887. reg = bus->read(bus, PHYADDR(ADM_SIG0)) & ADM_SIG0_MASK;
  888. if (reg != ADM_SIG0_VAL)
  889. return 0;
  890. reg = bus->read(bus, PHYADDR(ADM_SIG1)) & ADM_SIG1_MASK;
  891. if (reg != ADM_SIG1_VAL)
  892. return 0;
  893. dev->phy_id = (ADM_SIG0_VAL << 16) | ADM_SIG1_VAL;
  894. return 0;
  895. }
  896. static int adm6996_probe(struct phy_device *pdev)
  897. {
  898. return 0;
  899. }
  900. static void adm6996_remove(struct phy_device *pdev)
  901. {
  902. struct adm6996_priv *priv = phy_to_adm(pdev);
  903. if (priv && (priv->model == ADM6996M || priv->model == ADM6996L))
  904. unregister_switch(&priv->dev);
  905. }
  906. static int adm6996_soft_reset(struct phy_device *phydev)
  907. {
  908. /* we don't need an extra reset */
  909. return 0;
  910. }
  911. static struct phy_driver adm6996_phy_driver = {
  912. .name = "Infineon ADM6996",
  913. .phy_id = (ADM_SIG0_VAL << 16) | ADM_SIG1_VAL,
  914. .phy_id_mask = 0xffffffff,
  915. .features = PHY_BASIC_FEATURES,
  916. .probe = adm6996_probe,
  917. .remove = adm6996_remove,
  918. .config_init = &adm6996_config_init,
  919. .config_aneg = &adm6996_config_aneg,
  920. .read_status = &adm6996_read_status,
  921. .soft_reset = adm6996_soft_reset,
  922. .driver = { .owner = THIS_MODULE,},
  923. };
  924. static int adm6996_gpio_probe(struct platform_device *pdev)
  925. {
  926. struct adm6996_gpio_platform_data *pdata = pdev->dev.platform_data;
  927. struct adm6996_priv *priv;
  928. int ret;
  929. if (!pdata)
  930. return -EINVAL;
  931. priv = devm_kzalloc(&pdev->dev, sizeof(struct adm6996_priv), GFP_KERNEL);
  932. if (!priv)
  933. return -ENOMEM;
  934. mutex_init(&priv->reg_mutex);
  935. mutex_init(&priv->mib_lock);
  936. priv->eecs = pdata->eecs;
  937. priv->eedi = pdata->eedi;
  938. priv->eerc = pdata->eerc;
  939. priv->eesk = pdata->eesk;
  940. priv->model = pdata->model;
  941. priv->read = adm6996_read_gpio_reg;
  942. priv->write = adm6996_write_gpio_reg;
  943. ret = devm_gpio_request(&pdev->dev, priv->eecs, "adm_eecs");
  944. if (ret)
  945. return ret;
  946. ret = devm_gpio_request(&pdev->dev, priv->eedi, "adm_eedi");
  947. if (ret)
  948. return ret;
  949. ret = devm_gpio_request(&pdev->dev, priv->eerc, "adm_eerc");
  950. if (ret)
  951. return ret;
  952. ret = devm_gpio_request(&pdev->dev, priv->eesk, "adm_eesk");
  953. if (ret)
  954. return ret;
  955. ret = adm6996_switch_init(priv, dev_name(&pdev->dev), NULL);
  956. if (ret < 0)
  957. return ret;
  958. platform_set_drvdata(pdev, priv);
  959. return 0;
  960. }
  961. static int adm6996_gpio_remove(struct platform_device *pdev)
  962. {
  963. struct adm6996_priv *priv = platform_get_drvdata(pdev);
  964. if (priv && (priv->model == ADM6996M || priv->model == ADM6996L))
  965. unregister_switch(&priv->dev);
  966. return 0;
  967. }
  968. static struct platform_driver adm6996_gpio_driver = {
  969. .probe = adm6996_gpio_probe,
  970. .remove = adm6996_gpio_remove,
  971. .driver = {
  972. .name = "adm6996_gpio",
  973. },
  974. };
  975. static int __init adm6996_init(void)
  976. {
  977. int err;
  978. phy_register_fixup_for_id(PHY_ANY_ID, adm6996_fixup);
  979. err = phy_driver_register(&adm6996_phy_driver);
  980. if (err)
  981. return err;
  982. err = platform_driver_register(&adm6996_gpio_driver);
  983. if (err)
  984. phy_driver_unregister(&adm6996_phy_driver);
  985. return err;
  986. }
  987. static void __exit adm6996_exit(void)
  988. {
  989. platform_driver_unregister(&adm6996_gpio_driver);
  990. phy_driver_unregister(&adm6996_phy_driver);
  991. }
  992. module_init(adm6996_init);
  993. module_exit(adm6996_exit);