adm6996.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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. return 0;
  868. }
  869. /*
  870. * Warning: phydev->priv is NULL if phydev->addr != 0
  871. */
  872. static int adm6996_config_aneg(struct phy_device *phydev)
  873. {
  874. return 0;
  875. }
  876. static int adm6996_fixup(struct phy_device *dev)
  877. {
  878. struct mii_bus *bus = dev->bus;
  879. u16 reg;
  880. /* Our custom registers are at PHY addresses 0-10. Claim those. */
  881. if (dev->addr > 10)
  882. return 0;
  883. /* look for the switch on the bus */
  884. reg = bus->read(bus, PHYADDR(ADM_SIG0)) & ADM_SIG0_MASK;
  885. if (reg != ADM_SIG0_VAL)
  886. return 0;
  887. reg = bus->read(bus, PHYADDR(ADM_SIG1)) & ADM_SIG1_MASK;
  888. if (reg != ADM_SIG1_VAL)
  889. return 0;
  890. dev->phy_id = (ADM_SIG0_VAL << 16) | ADM_SIG1_VAL;
  891. return 0;
  892. }
  893. static int adm6996_probe(struct phy_device *pdev)
  894. {
  895. return 0;
  896. }
  897. static void adm6996_remove(struct phy_device *pdev)
  898. {
  899. struct adm6996_priv *priv = phy_to_adm(pdev);
  900. if (priv && (priv->model == ADM6996M || priv->model == ADM6996L))
  901. unregister_switch(&priv->dev);
  902. }
  903. static int adm6996_soft_reset(struct phy_device *phydev)
  904. {
  905. /* we don't need an extra reset */
  906. return 0;
  907. }
  908. static struct phy_driver adm6996_phy_driver = {
  909. .name = "Infineon ADM6996",
  910. .phy_id = (ADM_SIG0_VAL << 16) | ADM_SIG1_VAL,
  911. .phy_id_mask = 0xffffffff,
  912. .features = PHY_BASIC_FEATURES,
  913. .probe = adm6996_probe,
  914. .remove = adm6996_remove,
  915. .config_init = &adm6996_config_init,
  916. .config_aneg = &adm6996_config_aneg,
  917. .read_status = &adm6996_read_status,
  918. .soft_reset = adm6996_soft_reset,
  919. .driver = { .owner = THIS_MODULE,},
  920. };
  921. static int adm6996_gpio_probe(struct platform_device *pdev)
  922. {
  923. struct adm6996_gpio_platform_data *pdata = pdev->dev.platform_data;
  924. struct adm6996_priv *priv;
  925. int ret;
  926. if (!pdata)
  927. return -EINVAL;
  928. priv = devm_kzalloc(&pdev->dev, sizeof(struct adm6996_priv), GFP_KERNEL);
  929. if (!priv)
  930. return -ENOMEM;
  931. mutex_init(&priv->reg_mutex);
  932. mutex_init(&priv->mib_lock);
  933. priv->eecs = pdata->eecs;
  934. priv->eedi = pdata->eedi;
  935. priv->eerc = pdata->eerc;
  936. priv->eesk = pdata->eesk;
  937. priv->model = pdata->model;
  938. priv->read = adm6996_read_gpio_reg;
  939. priv->write = adm6996_write_gpio_reg;
  940. ret = devm_gpio_request(&pdev->dev, priv->eecs, "adm_eecs");
  941. if (ret)
  942. return ret;
  943. ret = devm_gpio_request(&pdev->dev, priv->eedi, "adm_eedi");
  944. if (ret)
  945. return ret;
  946. ret = devm_gpio_request(&pdev->dev, priv->eerc, "adm_eerc");
  947. if (ret)
  948. return ret;
  949. ret = devm_gpio_request(&pdev->dev, priv->eesk, "adm_eesk");
  950. if (ret)
  951. return ret;
  952. ret = adm6996_switch_init(priv, dev_name(&pdev->dev), NULL);
  953. if (ret < 0)
  954. return ret;
  955. platform_set_drvdata(pdev, priv);
  956. return 0;
  957. }
  958. static int adm6996_gpio_remove(struct platform_device *pdev)
  959. {
  960. struct adm6996_priv *priv = platform_get_drvdata(pdev);
  961. if (priv && (priv->model == ADM6996M || priv->model == ADM6996L))
  962. unregister_switch(&priv->dev);
  963. return 0;
  964. }
  965. static struct platform_driver adm6996_gpio_driver = {
  966. .probe = adm6996_gpio_probe,
  967. .remove = adm6996_gpio_remove,
  968. .driver = {
  969. .name = "adm6996_gpio",
  970. },
  971. };
  972. static int __init adm6996_init(void)
  973. {
  974. int err;
  975. phy_register_fixup_for_id(PHY_ANY_ID, adm6996_fixup);
  976. err = phy_driver_register(&adm6996_phy_driver);
  977. if (err)
  978. return err;
  979. err = platform_driver_register(&adm6996_gpio_driver);
  980. if (err)
  981. phy_driver_unregister(&adm6996_phy_driver);
  982. return err;
  983. }
  984. static void __exit adm6996_exit(void)
  985. {
  986. platform_driver_unregister(&adm6996_gpio_driver);
  987. phy_driver_unregister(&adm6996_phy_driver);
  988. }
  989. module_init(adm6996_init);
  990. module_exit(adm6996_exit);