ar8327.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. /*
  2. * ar8327.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/list.h>
  18. #include <linux/bitops.h>
  19. #include <linux/switch.h>
  20. #include <linux/delay.h>
  21. #include <linux/phy.h>
  22. #include <linux/lockdep.h>
  23. #include <linux/ar8216_platform.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/of_device.h>
  26. #include <linux/leds.h>
  27. #include <linux/mdio.h>
  28. #include "ar8216.h"
  29. #include "ar8327.h"
  30. extern const struct ar8xxx_mib_desc ar8236_mibs[39];
  31. extern const struct switch_attr ar8xxx_sw_attr_vlan[1];
  32. static u32
  33. ar8327_get_pad_cfg(struct ar8327_pad_cfg *cfg)
  34. {
  35. u32 t;
  36. if (!cfg)
  37. return 0;
  38. t = 0;
  39. switch (cfg->mode) {
  40. case AR8327_PAD_NC:
  41. break;
  42. case AR8327_PAD_MAC2MAC_MII:
  43. t = AR8327_PAD_MAC_MII_EN;
  44. if (cfg->rxclk_sel)
  45. t |= AR8327_PAD_MAC_MII_RXCLK_SEL;
  46. if (cfg->txclk_sel)
  47. t |= AR8327_PAD_MAC_MII_TXCLK_SEL;
  48. break;
  49. case AR8327_PAD_MAC2MAC_GMII:
  50. t = AR8327_PAD_MAC_GMII_EN;
  51. if (cfg->rxclk_sel)
  52. t |= AR8327_PAD_MAC_GMII_RXCLK_SEL;
  53. if (cfg->txclk_sel)
  54. t |= AR8327_PAD_MAC_GMII_TXCLK_SEL;
  55. break;
  56. case AR8327_PAD_MAC_SGMII:
  57. t = AR8327_PAD_SGMII_EN;
  58. /*
  59. * WAR for the QUalcomm Atheros AP136 board.
  60. * It seems that RGMII TX/RX delay settings needs to be
  61. * applied for SGMII mode as well, The ethernet is not
  62. * reliable without this.
  63. */
  64. t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
  65. t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
  66. if (cfg->rxclk_delay_en)
  67. t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
  68. if (cfg->txclk_delay_en)
  69. t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
  70. if (cfg->sgmii_delay_en)
  71. t |= AR8327_PAD_SGMII_DELAY_EN;
  72. break;
  73. case AR8327_PAD_MAC2PHY_MII:
  74. t = AR8327_PAD_PHY_MII_EN;
  75. if (cfg->rxclk_sel)
  76. t |= AR8327_PAD_PHY_MII_RXCLK_SEL;
  77. if (cfg->txclk_sel)
  78. t |= AR8327_PAD_PHY_MII_TXCLK_SEL;
  79. break;
  80. case AR8327_PAD_MAC2PHY_GMII:
  81. t = AR8327_PAD_PHY_GMII_EN;
  82. if (cfg->pipe_rxclk_sel)
  83. t |= AR8327_PAD_PHY_GMII_PIPE_RXCLK_SEL;
  84. if (cfg->rxclk_sel)
  85. t |= AR8327_PAD_PHY_GMII_RXCLK_SEL;
  86. if (cfg->txclk_sel)
  87. t |= AR8327_PAD_PHY_GMII_TXCLK_SEL;
  88. break;
  89. case AR8327_PAD_MAC_RGMII:
  90. t = AR8327_PAD_RGMII_EN;
  91. t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
  92. t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
  93. if (cfg->rxclk_delay_en)
  94. t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
  95. if (cfg->txclk_delay_en)
  96. t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
  97. break;
  98. case AR8327_PAD_PHY_GMII:
  99. t = AR8327_PAD_PHYX_GMII_EN;
  100. break;
  101. case AR8327_PAD_PHY_RGMII:
  102. t = AR8327_PAD_PHYX_RGMII_EN;
  103. break;
  104. case AR8327_PAD_PHY_MII:
  105. t = AR8327_PAD_PHYX_MII_EN;
  106. break;
  107. }
  108. return t;
  109. }
  110. static void
  111. ar8327_phy_fixup(struct ar8xxx_priv *priv, int phy)
  112. {
  113. switch (priv->chip_rev) {
  114. case 1:
  115. /* For 100M waveform */
  116. ar8xxx_phy_dbg_write(priv, phy, 0, 0x02ea);
  117. /* Turn on Gigabit clock */
  118. ar8xxx_phy_dbg_write(priv, phy, 0x3d, 0x68a0);
  119. break;
  120. case 2:
  121. ar8xxx_phy_mmd_write(priv, phy, 0x7, 0x3c);
  122. ar8xxx_phy_mmd_write(priv, phy, 0x4007, 0x0);
  123. /* fallthrough */
  124. case 4:
  125. ar8xxx_phy_mmd_write(priv, phy, 0x3, 0x800d);
  126. ar8xxx_phy_mmd_write(priv, phy, 0x4003, 0x803f);
  127. ar8xxx_phy_dbg_write(priv, phy, 0x3d, 0x6860);
  128. ar8xxx_phy_dbg_write(priv, phy, 0x5, 0x2c46);
  129. ar8xxx_phy_dbg_write(priv, phy, 0x3c, 0x6000);
  130. break;
  131. }
  132. }
  133. static u32
  134. ar8327_get_port_init_status(struct ar8327_port_cfg *cfg)
  135. {
  136. u32 t;
  137. if (!cfg->force_link)
  138. return AR8216_PORT_STATUS_LINK_AUTO;
  139. t = AR8216_PORT_STATUS_TXMAC | AR8216_PORT_STATUS_RXMAC;
  140. t |= cfg->duplex ? AR8216_PORT_STATUS_DUPLEX : 0;
  141. t |= cfg->rxpause ? AR8216_PORT_STATUS_RXFLOW : 0;
  142. t |= cfg->txpause ? AR8216_PORT_STATUS_TXFLOW : 0;
  143. switch (cfg->speed) {
  144. case AR8327_PORT_SPEED_10:
  145. t |= AR8216_PORT_SPEED_10M;
  146. break;
  147. case AR8327_PORT_SPEED_100:
  148. t |= AR8216_PORT_SPEED_100M;
  149. break;
  150. case AR8327_PORT_SPEED_1000:
  151. t |= AR8216_PORT_SPEED_1000M;
  152. break;
  153. }
  154. return t;
  155. }
  156. #define AR8327_LED_ENTRY(_num, _reg, _shift) \
  157. [_num] = { .reg = (_reg), .shift = (_shift) }
  158. static const struct ar8327_led_entry
  159. ar8327_led_map[AR8327_NUM_LEDS] = {
  160. AR8327_LED_ENTRY(AR8327_LED_PHY0_0, 0, 14),
  161. AR8327_LED_ENTRY(AR8327_LED_PHY0_1, 1, 14),
  162. AR8327_LED_ENTRY(AR8327_LED_PHY0_2, 2, 14),
  163. AR8327_LED_ENTRY(AR8327_LED_PHY1_0, 3, 8),
  164. AR8327_LED_ENTRY(AR8327_LED_PHY1_1, 3, 10),
  165. AR8327_LED_ENTRY(AR8327_LED_PHY1_2, 3, 12),
  166. AR8327_LED_ENTRY(AR8327_LED_PHY2_0, 3, 14),
  167. AR8327_LED_ENTRY(AR8327_LED_PHY2_1, 3, 16),
  168. AR8327_LED_ENTRY(AR8327_LED_PHY2_2, 3, 18),
  169. AR8327_LED_ENTRY(AR8327_LED_PHY3_0, 3, 20),
  170. AR8327_LED_ENTRY(AR8327_LED_PHY3_1, 3, 22),
  171. AR8327_LED_ENTRY(AR8327_LED_PHY3_2, 3, 24),
  172. AR8327_LED_ENTRY(AR8327_LED_PHY4_0, 0, 30),
  173. AR8327_LED_ENTRY(AR8327_LED_PHY4_1, 1, 30),
  174. AR8327_LED_ENTRY(AR8327_LED_PHY4_2, 2, 30),
  175. };
  176. static void
  177. ar8327_set_led_pattern(struct ar8xxx_priv *priv, unsigned int led_num,
  178. enum ar8327_led_pattern pattern)
  179. {
  180. const struct ar8327_led_entry *entry;
  181. entry = &ar8327_led_map[led_num];
  182. ar8xxx_rmw(priv, AR8327_REG_LED_CTRL(entry->reg),
  183. (3 << entry->shift), pattern << entry->shift);
  184. }
  185. static void
  186. ar8327_led_work_func(struct work_struct *work)
  187. {
  188. struct ar8327_led *aled;
  189. u8 pattern;
  190. aled = container_of(work, struct ar8327_led, led_work);
  191. spin_lock(&aled->lock);
  192. pattern = aled->pattern;
  193. spin_unlock(&aled->lock);
  194. ar8327_set_led_pattern(aled->sw_priv, aled->led_num,
  195. pattern);
  196. }
  197. static void
  198. ar8327_led_schedule_change(struct ar8327_led *aled, u8 pattern)
  199. {
  200. if (aled->pattern == pattern)
  201. return;
  202. aled->pattern = pattern;
  203. schedule_work(&aled->led_work);
  204. }
  205. static inline struct ar8327_led *
  206. led_cdev_to_ar8327_led(struct led_classdev *led_cdev)
  207. {
  208. return container_of(led_cdev, struct ar8327_led, cdev);
  209. }
  210. static int
  211. ar8327_led_blink_set(struct led_classdev *led_cdev,
  212. unsigned long *delay_on,
  213. unsigned long *delay_off)
  214. {
  215. struct ar8327_led *aled = led_cdev_to_ar8327_led(led_cdev);
  216. if (*delay_on == 0 && *delay_off == 0) {
  217. *delay_on = 125;
  218. *delay_off = 125;
  219. }
  220. if (*delay_on != 125 || *delay_off != 125) {
  221. /*
  222. * The hardware only supports blinking at 4Hz. Fall back
  223. * to software implementation in other cases.
  224. */
  225. return -EINVAL;
  226. }
  227. spin_lock(&aled->lock);
  228. aled->enable_hw_mode = false;
  229. ar8327_led_schedule_change(aled, AR8327_LED_PATTERN_BLINK);
  230. spin_unlock(&aled->lock);
  231. return 0;
  232. }
  233. static void
  234. ar8327_led_set_brightness(struct led_classdev *led_cdev,
  235. enum led_brightness brightness)
  236. {
  237. struct ar8327_led *aled = led_cdev_to_ar8327_led(led_cdev);
  238. u8 pattern;
  239. bool active;
  240. active = (brightness != LED_OFF);
  241. active ^= aled->active_low;
  242. pattern = (active) ? AR8327_LED_PATTERN_ON :
  243. AR8327_LED_PATTERN_OFF;
  244. spin_lock(&aled->lock);
  245. aled->enable_hw_mode = false;
  246. ar8327_led_schedule_change(aled, pattern);
  247. spin_unlock(&aled->lock);
  248. }
  249. static ssize_t
  250. ar8327_led_enable_hw_mode_show(struct device *dev,
  251. struct device_attribute *attr,
  252. char *buf)
  253. {
  254. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  255. struct ar8327_led *aled = led_cdev_to_ar8327_led(led_cdev);
  256. ssize_t ret = 0;
  257. spin_lock(&aled->lock);
  258. ret += sprintf(buf, "%d\n", aled->enable_hw_mode);
  259. spin_unlock(&aled->lock);
  260. return ret;
  261. }
  262. static ssize_t
  263. ar8327_led_enable_hw_mode_store(struct device *dev,
  264. struct device_attribute *attr,
  265. const char *buf,
  266. size_t size)
  267. {
  268. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  269. struct ar8327_led *aled = led_cdev_to_ar8327_led(led_cdev);
  270. u8 pattern;
  271. u8 value;
  272. int ret;
  273. ret = kstrtou8(buf, 10, &value);
  274. if (ret < 0)
  275. return -EINVAL;
  276. spin_lock(&aled->lock);
  277. aled->enable_hw_mode = !!value;
  278. if (aled->enable_hw_mode)
  279. pattern = AR8327_LED_PATTERN_RULE;
  280. else
  281. pattern = AR8327_LED_PATTERN_OFF;
  282. ar8327_led_schedule_change(aled, pattern);
  283. spin_unlock(&aled->lock);
  284. return size;
  285. }
  286. static DEVICE_ATTR(enable_hw_mode, S_IRUGO | S_IWUSR,
  287. ar8327_led_enable_hw_mode_show,
  288. ar8327_led_enable_hw_mode_store);
  289. static int
  290. ar8327_led_register(struct ar8327_led *aled)
  291. {
  292. int ret;
  293. ret = led_classdev_register(NULL, &aled->cdev);
  294. if (ret < 0)
  295. return ret;
  296. if (aled->mode == AR8327_LED_MODE_HW) {
  297. ret = device_create_file(aled->cdev.dev,
  298. &dev_attr_enable_hw_mode);
  299. if (ret)
  300. goto err_unregister;
  301. }
  302. return 0;
  303. err_unregister:
  304. led_classdev_unregister(&aled->cdev);
  305. return ret;
  306. }
  307. static void
  308. ar8327_led_unregister(struct ar8327_led *aled)
  309. {
  310. if (aled->mode == AR8327_LED_MODE_HW)
  311. device_remove_file(aled->cdev.dev, &dev_attr_enable_hw_mode);
  312. led_classdev_unregister(&aled->cdev);
  313. cancel_work_sync(&aled->led_work);
  314. }
  315. static int
  316. ar8327_led_create(struct ar8xxx_priv *priv,
  317. const struct ar8327_led_info *led_info)
  318. {
  319. struct ar8327_data *data = priv->chip_data;
  320. struct ar8327_led *aled;
  321. int ret;
  322. if (!IS_ENABLED(CONFIG_AR8216_PHY_LEDS))
  323. return 0;
  324. if (!led_info->name)
  325. return -EINVAL;
  326. if (led_info->led_num >= AR8327_NUM_LEDS)
  327. return -EINVAL;
  328. aled = kzalloc(sizeof(*aled) + strlen(led_info->name) + 1,
  329. GFP_KERNEL);
  330. if (!aled)
  331. return -ENOMEM;
  332. aled->sw_priv = priv;
  333. aled->led_num = led_info->led_num;
  334. aled->active_low = led_info->active_low;
  335. aled->mode = led_info->mode;
  336. if (aled->mode == AR8327_LED_MODE_HW)
  337. aled->enable_hw_mode = true;
  338. aled->name = (char *)(aled + 1);
  339. strcpy(aled->name, led_info->name);
  340. aled->cdev.name = aled->name;
  341. aled->cdev.brightness_set = ar8327_led_set_brightness;
  342. aled->cdev.blink_set = ar8327_led_blink_set;
  343. aled->cdev.default_trigger = led_info->default_trigger;
  344. spin_lock_init(&aled->lock);
  345. mutex_init(&aled->mutex);
  346. INIT_WORK(&aled->led_work, ar8327_led_work_func);
  347. ret = ar8327_led_register(aled);
  348. if (ret)
  349. goto err_free;
  350. data->leds[data->num_leds++] = aled;
  351. return 0;
  352. err_free:
  353. kfree(aled);
  354. return ret;
  355. }
  356. static void
  357. ar8327_led_destroy(struct ar8327_led *aled)
  358. {
  359. ar8327_led_unregister(aled);
  360. kfree(aled);
  361. }
  362. static void
  363. ar8327_leds_init(struct ar8xxx_priv *priv)
  364. {
  365. struct ar8327_data *data = priv->chip_data;
  366. unsigned i;
  367. if (!IS_ENABLED(CONFIG_AR8216_PHY_LEDS))
  368. return;
  369. for (i = 0; i < data->num_leds; i++) {
  370. struct ar8327_led *aled;
  371. aled = data->leds[i];
  372. if (aled->enable_hw_mode)
  373. aled->pattern = AR8327_LED_PATTERN_RULE;
  374. else
  375. aled->pattern = AR8327_LED_PATTERN_OFF;
  376. ar8327_set_led_pattern(priv, aled->led_num, aled->pattern);
  377. }
  378. }
  379. static void
  380. ar8327_leds_cleanup(struct ar8xxx_priv *priv)
  381. {
  382. struct ar8327_data *data = priv->chip_data;
  383. unsigned i;
  384. if (!IS_ENABLED(CONFIG_AR8216_PHY_LEDS))
  385. return;
  386. for (i = 0; i < data->num_leds; i++) {
  387. struct ar8327_led *aled;
  388. aled = data->leds[i];
  389. ar8327_led_destroy(aled);
  390. }
  391. kfree(data->leds);
  392. }
  393. static int
  394. ar8327_hw_config_pdata(struct ar8xxx_priv *priv,
  395. struct ar8327_platform_data *pdata)
  396. {
  397. struct ar8327_led_cfg *led_cfg;
  398. struct ar8327_data *data = priv->chip_data;
  399. u32 pos, new_pos;
  400. u32 t;
  401. if (!pdata)
  402. return -EINVAL;
  403. priv->get_port_link = pdata->get_port_link;
  404. data->port0_status = ar8327_get_port_init_status(&pdata->port0_cfg);
  405. data->port6_status = ar8327_get_port_init_status(&pdata->port6_cfg);
  406. t = ar8327_get_pad_cfg(pdata->pad0_cfg);
  407. if (chip_is_ar8337(priv) && !pdata->pad0_cfg->mac06_exchange_dis)
  408. t |= AR8337_PAD_MAC06_EXCHANGE_EN;
  409. ar8xxx_write(priv, AR8327_REG_PAD0_MODE, t);
  410. t = ar8327_get_pad_cfg(pdata->pad5_cfg);
  411. ar8xxx_write(priv, AR8327_REG_PAD5_MODE, t);
  412. t = ar8327_get_pad_cfg(pdata->pad6_cfg);
  413. ar8xxx_write(priv, AR8327_REG_PAD6_MODE, t);
  414. pos = ar8xxx_read(priv, AR8327_REG_POWER_ON_STRIP);
  415. new_pos = pos;
  416. led_cfg = pdata->led_cfg;
  417. if (led_cfg) {
  418. if (led_cfg->open_drain)
  419. new_pos |= AR8327_POWER_ON_STRIP_LED_OPEN_EN;
  420. else
  421. new_pos &= ~AR8327_POWER_ON_STRIP_LED_OPEN_EN;
  422. ar8xxx_write(priv, AR8327_REG_LED_CTRL0, led_cfg->led_ctrl0);
  423. ar8xxx_write(priv, AR8327_REG_LED_CTRL1, led_cfg->led_ctrl1);
  424. ar8xxx_write(priv, AR8327_REG_LED_CTRL2, led_cfg->led_ctrl2);
  425. ar8xxx_write(priv, AR8327_REG_LED_CTRL3, led_cfg->led_ctrl3);
  426. if (new_pos != pos)
  427. new_pos |= AR8327_POWER_ON_STRIP_POWER_ON_SEL;
  428. }
  429. if (pdata->sgmii_cfg) {
  430. t = pdata->sgmii_cfg->sgmii_ctrl;
  431. if (priv->chip_rev == 1)
  432. t |= AR8327_SGMII_CTRL_EN_PLL |
  433. AR8327_SGMII_CTRL_EN_RX |
  434. AR8327_SGMII_CTRL_EN_TX;
  435. else
  436. t &= ~(AR8327_SGMII_CTRL_EN_PLL |
  437. AR8327_SGMII_CTRL_EN_RX |
  438. AR8327_SGMII_CTRL_EN_TX);
  439. ar8xxx_write(priv, AR8327_REG_SGMII_CTRL, t);
  440. if (pdata->sgmii_cfg->serdes_aen)
  441. new_pos &= ~AR8327_POWER_ON_STRIP_SERDES_AEN;
  442. else
  443. new_pos |= AR8327_POWER_ON_STRIP_SERDES_AEN;
  444. }
  445. ar8xxx_write(priv, AR8327_REG_POWER_ON_STRIP, new_pos);
  446. if (pdata->leds && pdata->num_leds) {
  447. int i;
  448. data->leds = kzalloc(pdata->num_leds * sizeof(void *),
  449. GFP_KERNEL);
  450. if (!data->leds)
  451. return -ENOMEM;
  452. for (i = 0; i < pdata->num_leds; i++)
  453. ar8327_led_create(priv, &pdata->leds[i]);
  454. }
  455. return 0;
  456. }
  457. #ifdef CONFIG_OF
  458. static int
  459. ar8327_hw_config_of(struct ar8xxx_priv *priv, struct device_node *np)
  460. {
  461. struct ar8327_data *data = priv->chip_data;
  462. const __be32 *paddr;
  463. int len;
  464. int i;
  465. paddr = of_get_property(np, "qca,ar8327-initvals", &len);
  466. if (!paddr || len < (2 * sizeof(*paddr)))
  467. return -EINVAL;
  468. len /= sizeof(*paddr);
  469. for (i = 0; i < len - 1; i += 2) {
  470. u32 reg;
  471. u32 val;
  472. reg = be32_to_cpup(paddr + i);
  473. val = be32_to_cpup(paddr + i + 1);
  474. switch (reg) {
  475. case AR8327_REG_PORT_STATUS(0):
  476. data->port0_status = val;
  477. break;
  478. case AR8327_REG_PORT_STATUS(6):
  479. data->port6_status = val;
  480. break;
  481. default:
  482. ar8xxx_write(priv, reg, val);
  483. break;
  484. }
  485. }
  486. return 0;
  487. }
  488. #else
  489. static inline int
  490. ar8327_hw_config_of(struct ar8xxx_priv *priv, struct device_node *np)
  491. {
  492. return -EINVAL;
  493. }
  494. #endif
  495. static int
  496. ar8327_hw_init(struct ar8xxx_priv *priv)
  497. {
  498. int ret;
  499. priv->chip_data = kzalloc(sizeof(struct ar8327_data), GFP_KERNEL);
  500. if (!priv->chip_data)
  501. return -ENOMEM;
  502. if (priv->phy->dev.of_node)
  503. ret = ar8327_hw_config_of(priv, priv->phy->dev.of_node);
  504. else
  505. ret = ar8327_hw_config_pdata(priv,
  506. priv->phy->dev.platform_data);
  507. if (ret)
  508. return ret;
  509. ar8327_leds_init(priv);
  510. ar8xxx_phy_init(priv);
  511. return 0;
  512. }
  513. static void
  514. ar8327_cleanup(struct ar8xxx_priv *priv)
  515. {
  516. ar8327_leds_cleanup(priv);
  517. }
  518. static void
  519. ar8327_init_globals(struct ar8xxx_priv *priv)
  520. {
  521. struct ar8327_data *data = priv->chip_data;
  522. u32 t;
  523. int i;
  524. /* enable CPU port and disable mirror port */
  525. t = AR8327_FWD_CTRL0_CPU_PORT_EN |
  526. AR8327_FWD_CTRL0_MIRROR_PORT;
  527. ar8xxx_write(priv, AR8327_REG_FWD_CTRL0, t);
  528. /* forward multicast and broadcast frames to CPU */
  529. t = (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_UC_FLOOD_S) |
  530. (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_MC_FLOOD_S) |
  531. (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_BC_FLOOD_S);
  532. ar8xxx_write(priv, AR8327_REG_FWD_CTRL1, t);
  533. /* enable jumbo frames */
  534. ar8xxx_rmw(priv, AR8327_REG_MAX_FRAME_SIZE,
  535. AR8327_MAX_FRAME_SIZE_MTU, 9018 + 8 + 2);
  536. /* Enable MIB counters */
  537. ar8xxx_reg_set(priv, AR8327_REG_MODULE_EN,
  538. AR8327_MODULE_EN_MIB);
  539. /* Disable EEE on all phy's due to stability issues */
  540. for (i = 0; i < AR8XXX_NUM_PHYS; i++)
  541. data->eee[i] = false;
  542. }
  543. static void
  544. ar8327_init_port(struct ar8xxx_priv *priv, int port)
  545. {
  546. struct ar8327_data *data = priv->chip_data;
  547. u32 t;
  548. if (port == AR8216_PORT_CPU)
  549. t = data->port0_status;
  550. else if (port == 6)
  551. t = data->port6_status;
  552. else
  553. t = AR8216_PORT_STATUS_LINK_AUTO;
  554. ar8xxx_write(priv, AR8327_REG_PORT_STATUS(port), t);
  555. ar8xxx_write(priv, AR8327_REG_PORT_HEADER(port), 0);
  556. t = 1 << AR8327_PORT_VLAN0_DEF_SVID_S;
  557. t |= 1 << AR8327_PORT_VLAN0_DEF_CVID_S;
  558. ar8xxx_write(priv, AR8327_REG_PORT_VLAN0(port), t);
  559. t = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH << AR8327_PORT_VLAN1_OUT_MODE_S;
  560. ar8xxx_write(priv, AR8327_REG_PORT_VLAN1(port), t);
  561. t = AR8327_PORT_LOOKUP_LEARN;
  562. t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
  563. ar8xxx_write(priv, AR8327_REG_PORT_LOOKUP(port), t);
  564. }
  565. static u32
  566. ar8327_read_port_status(struct ar8xxx_priv *priv, int port)
  567. {
  568. u32 t;
  569. t = ar8xxx_read(priv, AR8327_REG_PORT_STATUS(port));
  570. /* map the flow control autoneg result bits to the flow control bits
  571. * used in forced mode to allow ar8216_read_port_link detect
  572. * flow control properly if autoneg is used
  573. */
  574. if (t & AR8216_PORT_STATUS_LINK_UP &&
  575. t & AR8216_PORT_STATUS_LINK_AUTO) {
  576. t &= ~(AR8216_PORT_STATUS_TXFLOW | AR8216_PORT_STATUS_RXFLOW);
  577. if (t & AR8327_PORT_STATUS_TXFLOW_AUTO)
  578. t |= AR8216_PORT_STATUS_TXFLOW;
  579. if (t & AR8327_PORT_STATUS_RXFLOW_AUTO)
  580. t |= AR8216_PORT_STATUS_RXFLOW;
  581. }
  582. return t;
  583. }
  584. static u32
  585. ar8327_read_port_eee_status(struct ar8xxx_priv *priv, int port)
  586. {
  587. int phy;
  588. u16 t;
  589. if (port >= priv->dev.ports)
  590. return 0;
  591. if (port == 0 || port == 6)
  592. return 0;
  593. phy = port - 1;
  594. /* EEE Ability Auto-negotiation Result */
  595. ar8xxx_phy_mmd_write(priv, phy, 0x7, 0x8000);
  596. t = ar8xxx_phy_mmd_read(priv, phy, 0x4007);
  597. return mmd_eee_adv_to_ethtool_adv_t(t);
  598. }
  599. static int
  600. ar8327_atu_flush(struct ar8xxx_priv *priv)
  601. {
  602. int ret;
  603. ret = ar8216_wait_bit(priv, AR8327_REG_ATU_FUNC,
  604. AR8327_ATU_FUNC_BUSY, 0);
  605. if (!ret)
  606. ar8xxx_write(priv, AR8327_REG_ATU_FUNC,
  607. AR8327_ATU_FUNC_OP_FLUSH |
  608. AR8327_ATU_FUNC_BUSY);
  609. return ret;
  610. }
  611. static int
  612. ar8327_atu_flush_port(struct ar8xxx_priv *priv, int port)
  613. {
  614. u32 t;
  615. int ret;
  616. ret = ar8216_wait_bit(priv, AR8327_REG_ATU_FUNC,
  617. AR8327_ATU_FUNC_BUSY, 0);
  618. if (!ret) {
  619. t = (port << AR8327_ATU_PORT_NUM_S);
  620. t |= AR8327_ATU_FUNC_OP_FLUSH_PORT;
  621. t |= AR8327_ATU_FUNC_BUSY;
  622. ar8xxx_write(priv, AR8327_REG_ATU_FUNC, t);
  623. }
  624. return ret;
  625. }
  626. static int
  627. ar8327_get_port_igmp(struct ar8xxx_priv *priv, int port)
  628. {
  629. u32 fwd_ctrl, frame_ack;
  630. fwd_ctrl = (BIT(port) << AR8327_FWD_CTRL1_IGMP_S);
  631. frame_ack = ((AR8327_FRAME_ACK_CTRL_IGMP_MLD |
  632. AR8327_FRAME_ACK_CTRL_IGMP_JOIN |
  633. AR8327_FRAME_ACK_CTRL_IGMP_LEAVE) <<
  634. AR8327_FRAME_ACK_CTRL_S(port));
  635. return (ar8xxx_read(priv, AR8327_REG_FWD_CTRL1) &
  636. fwd_ctrl) == fwd_ctrl &&
  637. (ar8xxx_read(priv, AR8327_REG_FRAME_ACK_CTRL(port)) &
  638. frame_ack) == frame_ack;
  639. }
  640. static void
  641. ar8327_set_port_igmp(struct ar8xxx_priv *priv, int port, int enable)
  642. {
  643. int reg_frame_ack = AR8327_REG_FRAME_ACK_CTRL(port);
  644. u32 val_frame_ack = (AR8327_FRAME_ACK_CTRL_IGMP_MLD |
  645. AR8327_FRAME_ACK_CTRL_IGMP_JOIN |
  646. AR8327_FRAME_ACK_CTRL_IGMP_LEAVE) <<
  647. AR8327_FRAME_ACK_CTRL_S(port);
  648. if (enable) {
  649. ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL1,
  650. BIT(port) << AR8327_FWD_CTRL1_MC_FLOOD_S,
  651. BIT(port) << AR8327_FWD_CTRL1_IGMP_S);
  652. ar8xxx_reg_set(priv, reg_frame_ack, val_frame_ack);
  653. } else {
  654. ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL1,
  655. BIT(port) << AR8327_FWD_CTRL1_IGMP_S,
  656. BIT(port) << AR8327_FWD_CTRL1_MC_FLOOD_S);
  657. ar8xxx_reg_clear(priv, reg_frame_ack, val_frame_ack);
  658. }
  659. }
  660. static void
  661. ar8327_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
  662. {
  663. if (ar8216_wait_bit(priv, AR8327_REG_VTU_FUNC1,
  664. AR8327_VTU_FUNC1_BUSY, 0))
  665. return;
  666. if ((op & AR8327_VTU_FUNC1_OP) == AR8327_VTU_FUNC1_OP_LOAD)
  667. ar8xxx_write(priv, AR8327_REG_VTU_FUNC0, val);
  668. op |= AR8327_VTU_FUNC1_BUSY;
  669. ar8xxx_write(priv, AR8327_REG_VTU_FUNC1, op);
  670. }
  671. static void
  672. ar8327_vtu_flush(struct ar8xxx_priv *priv)
  673. {
  674. ar8327_vtu_op(priv, AR8327_VTU_FUNC1_OP_FLUSH, 0);
  675. }
  676. static void
  677. ar8327_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask)
  678. {
  679. u32 op;
  680. u32 val;
  681. int i;
  682. op = AR8327_VTU_FUNC1_OP_LOAD | (vid << AR8327_VTU_FUNC1_VID_S);
  683. val = AR8327_VTU_FUNC0_VALID | AR8327_VTU_FUNC0_IVL;
  684. for (i = 0; i < AR8327_NUM_PORTS; i++) {
  685. u32 mode;
  686. if ((port_mask & BIT(i)) == 0)
  687. mode = AR8327_VTU_FUNC0_EG_MODE_NOT;
  688. else if (priv->vlan == 0)
  689. mode = AR8327_VTU_FUNC0_EG_MODE_KEEP;
  690. else if ((priv->vlan_tagged & BIT(i)) || (priv->vlan_id[priv->pvid[i]] != vid))
  691. mode = AR8327_VTU_FUNC0_EG_MODE_TAG;
  692. else
  693. mode = AR8327_VTU_FUNC0_EG_MODE_UNTAG;
  694. val |= mode << AR8327_VTU_FUNC0_EG_MODE_S(i);
  695. }
  696. ar8327_vtu_op(priv, op, val);
  697. }
  698. static void
  699. ar8327_setup_port(struct ar8xxx_priv *priv, int port, u32 members)
  700. {
  701. u32 t;
  702. u32 egress, ingress;
  703. u32 pvid = priv->vlan_id[priv->pvid[port]];
  704. if (priv->vlan) {
  705. egress = AR8327_PORT_VLAN1_OUT_MODE_UNMOD;
  706. ingress = AR8216_IN_SECURE;
  707. } else {
  708. egress = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH;
  709. ingress = AR8216_IN_PORT_ONLY;
  710. }
  711. t = pvid << AR8327_PORT_VLAN0_DEF_SVID_S;
  712. t |= pvid << AR8327_PORT_VLAN0_DEF_CVID_S;
  713. ar8xxx_write(priv, AR8327_REG_PORT_VLAN0(port), t);
  714. t = AR8327_PORT_VLAN1_PORT_VLAN_PROP;
  715. t |= egress << AR8327_PORT_VLAN1_OUT_MODE_S;
  716. ar8xxx_write(priv, AR8327_REG_PORT_VLAN1(port), t);
  717. t = members;
  718. t |= AR8327_PORT_LOOKUP_LEARN;
  719. t |= ingress << AR8327_PORT_LOOKUP_IN_MODE_S;
  720. t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
  721. ar8xxx_write(priv, AR8327_REG_PORT_LOOKUP(port), t);
  722. }
  723. static int
  724. ar8327_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
  725. {
  726. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  727. u8 ports = priv->vlan_table[val->port_vlan];
  728. int i;
  729. val->len = 0;
  730. for (i = 0; i < dev->ports; i++) {
  731. struct switch_port *p;
  732. if (!(ports & (1 << i)))
  733. continue;
  734. p = &val->value.ports[val->len++];
  735. p->id = i;
  736. if ((priv->vlan_tagged & (1 << i)) || (priv->pvid[i] != val->port_vlan))
  737. p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
  738. else
  739. p->flags = 0;
  740. }
  741. return 0;
  742. }
  743. static int
  744. ar8327_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
  745. {
  746. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  747. u8 *vt = &priv->vlan_table[val->port_vlan];
  748. int i;
  749. *vt = 0;
  750. for (i = 0; i < val->len; i++) {
  751. struct switch_port *p = &val->value.ports[i];
  752. if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
  753. if (val->port_vlan == priv->pvid[p->id]) {
  754. priv->vlan_tagged |= (1 << p->id);
  755. }
  756. } else {
  757. priv->vlan_tagged &= ~(1 << p->id);
  758. priv->pvid[p->id] = val->port_vlan;
  759. }
  760. *vt |= 1 << p->id;
  761. }
  762. return 0;
  763. }
  764. static void
  765. ar8327_set_mirror_regs(struct ar8xxx_priv *priv)
  766. {
  767. int port;
  768. /* reset all mirror registers */
  769. ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
  770. AR8327_FWD_CTRL0_MIRROR_PORT,
  771. (0xF << AR8327_FWD_CTRL0_MIRROR_PORT_S));
  772. for (port = 0; port < AR8327_NUM_PORTS; port++) {
  773. ar8xxx_reg_clear(priv, AR8327_REG_PORT_LOOKUP(port),
  774. AR8327_PORT_LOOKUP_ING_MIRROR_EN);
  775. ar8xxx_reg_clear(priv, AR8327_REG_PORT_HOL_CTRL1(port),
  776. AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN);
  777. }
  778. /* now enable mirroring if necessary */
  779. if (priv->source_port >= AR8327_NUM_PORTS ||
  780. priv->monitor_port >= AR8327_NUM_PORTS ||
  781. priv->source_port == priv->monitor_port) {
  782. return;
  783. }
  784. ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
  785. AR8327_FWD_CTRL0_MIRROR_PORT,
  786. (priv->monitor_port << AR8327_FWD_CTRL0_MIRROR_PORT_S));
  787. if (priv->mirror_rx)
  788. ar8xxx_reg_set(priv, AR8327_REG_PORT_LOOKUP(priv->source_port),
  789. AR8327_PORT_LOOKUP_ING_MIRROR_EN);
  790. if (priv->mirror_tx)
  791. ar8xxx_reg_set(priv, AR8327_REG_PORT_HOL_CTRL1(priv->source_port),
  792. AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN);
  793. }
  794. static int
  795. ar8327_sw_set_eee(struct switch_dev *dev,
  796. const struct switch_attr *attr,
  797. struct switch_val *val)
  798. {
  799. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  800. struct ar8327_data *data = priv->chip_data;
  801. int port = val->port_vlan;
  802. int phy;
  803. if (port >= dev->ports)
  804. return -EINVAL;
  805. if (port == 0 || port == 6)
  806. return -EOPNOTSUPP;
  807. phy = port - 1;
  808. data->eee[phy] = !!(val->value.i);
  809. return 0;
  810. }
  811. static int
  812. ar8327_sw_get_eee(struct switch_dev *dev,
  813. const struct switch_attr *attr,
  814. struct switch_val *val)
  815. {
  816. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  817. const struct ar8327_data *data = priv->chip_data;
  818. int port = val->port_vlan;
  819. int phy;
  820. if (port >= dev->ports)
  821. return -EINVAL;
  822. if (port == 0 || port == 6)
  823. return -EOPNOTSUPP;
  824. phy = port - 1;
  825. val->value.i = data->eee[phy];
  826. return 0;
  827. }
  828. static void
  829. ar8327_wait_atu_ready(struct ar8xxx_priv *priv, u16 r2, u16 r1)
  830. {
  831. int timeout = 20;
  832. while (ar8xxx_mii_read32(priv, r2, r1) & AR8327_ATU_FUNC_BUSY && --timeout)
  833. udelay(10);
  834. if (!timeout)
  835. pr_err("ar8327: timeout waiting for atu to become ready\n");
  836. }
  837. static void ar8327_get_arl_entry(struct ar8xxx_priv *priv,
  838. struct arl_entry *a, u32 *status, enum arl_op op)
  839. {
  840. struct mii_bus *bus = priv->mii_bus;
  841. u16 r2, page;
  842. u16 r1_data0, r1_data1, r1_data2, r1_func;
  843. u32 t, val0, val1, val2;
  844. int i;
  845. split_addr(AR8327_REG_ATU_DATA0, &r1_data0, &r2, &page);
  846. r2 |= 0x10;
  847. r1_data1 = (AR8327_REG_ATU_DATA1 >> 1) & 0x1e;
  848. r1_data2 = (AR8327_REG_ATU_DATA2 >> 1) & 0x1e;
  849. r1_func = (AR8327_REG_ATU_FUNC >> 1) & 0x1e;
  850. switch (op) {
  851. case AR8XXX_ARL_INITIALIZE:
  852. /* all ATU registers are on the same page
  853. * therefore set page only once
  854. */
  855. bus->write(bus, 0x18, 0, page);
  856. wait_for_page_switch();
  857. ar8327_wait_atu_ready(priv, r2, r1_func);
  858. ar8xxx_mii_write32(priv, r2, r1_data0, 0);
  859. ar8xxx_mii_write32(priv, r2, r1_data1, 0);
  860. ar8xxx_mii_write32(priv, r2, r1_data2, 0);
  861. break;
  862. case AR8XXX_ARL_GET_NEXT:
  863. ar8xxx_mii_write32(priv, r2, r1_func,
  864. AR8327_ATU_FUNC_OP_GET_NEXT |
  865. AR8327_ATU_FUNC_BUSY);
  866. ar8327_wait_atu_ready(priv, r2, r1_func);
  867. val0 = ar8xxx_mii_read32(priv, r2, r1_data0);
  868. val1 = ar8xxx_mii_read32(priv, r2, r1_data1);
  869. val2 = ar8xxx_mii_read32(priv, r2, r1_data2);
  870. *status = val2 & AR8327_ATU_STATUS;
  871. if (!*status)
  872. break;
  873. i = 0;
  874. t = AR8327_ATU_PORT0;
  875. while (!(val1 & t) && ++i < AR8327_NUM_PORTS)
  876. t <<= 1;
  877. a->port = i;
  878. a->mac[0] = (val0 & AR8327_ATU_ADDR0) >> AR8327_ATU_ADDR0_S;
  879. a->mac[1] = (val0 & AR8327_ATU_ADDR1) >> AR8327_ATU_ADDR1_S;
  880. a->mac[2] = (val0 & AR8327_ATU_ADDR2) >> AR8327_ATU_ADDR2_S;
  881. a->mac[3] = (val0 & AR8327_ATU_ADDR3) >> AR8327_ATU_ADDR3_S;
  882. a->mac[4] = (val1 & AR8327_ATU_ADDR4) >> AR8327_ATU_ADDR4_S;
  883. a->mac[5] = (val1 & AR8327_ATU_ADDR5) >> AR8327_ATU_ADDR5_S;
  884. break;
  885. }
  886. }
  887. static int
  888. ar8327_sw_hw_apply(struct switch_dev *dev)
  889. {
  890. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  891. const struct ar8327_data *data = priv->chip_data;
  892. int ret, i;
  893. ret = ar8xxx_sw_hw_apply(dev);
  894. if (ret)
  895. return ret;
  896. for (i=0; i < AR8XXX_NUM_PHYS; i++) {
  897. if (data->eee[i])
  898. ar8xxx_reg_clear(priv, AR8327_REG_EEE_CTRL,
  899. AR8327_EEE_CTRL_DISABLE_PHY(i));
  900. else
  901. ar8xxx_reg_set(priv, AR8327_REG_EEE_CTRL,
  902. AR8327_EEE_CTRL_DISABLE_PHY(i));
  903. }
  904. return 0;
  905. }
  906. int
  907. ar8327_sw_get_port_igmp_snooping(struct switch_dev *dev,
  908. const struct switch_attr *attr,
  909. struct switch_val *val)
  910. {
  911. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  912. int port = val->port_vlan;
  913. if (port >= dev->ports)
  914. return -EINVAL;
  915. mutex_lock(&priv->reg_mutex);
  916. val->value.i = ar8327_get_port_igmp(priv, port);
  917. mutex_unlock(&priv->reg_mutex);
  918. return 0;
  919. }
  920. int
  921. ar8327_sw_set_port_igmp_snooping(struct switch_dev *dev,
  922. const struct switch_attr *attr,
  923. struct switch_val *val)
  924. {
  925. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  926. int port = val->port_vlan;
  927. if (port >= dev->ports)
  928. return -EINVAL;
  929. mutex_lock(&priv->reg_mutex);
  930. ar8327_set_port_igmp(priv, port, val->value.i);
  931. mutex_unlock(&priv->reg_mutex);
  932. return 0;
  933. }
  934. int
  935. ar8327_sw_get_igmp_snooping(struct switch_dev *dev,
  936. const struct switch_attr *attr,
  937. struct switch_val *val)
  938. {
  939. int port;
  940. for (port = 0; port < dev->ports; port++) {
  941. val->port_vlan = port;
  942. if (ar8327_sw_get_port_igmp_snooping(dev, attr, val) ||
  943. !val->value.i)
  944. break;
  945. }
  946. return 0;
  947. }
  948. int
  949. ar8327_sw_set_igmp_snooping(struct switch_dev *dev,
  950. const struct switch_attr *attr,
  951. struct switch_val *val)
  952. {
  953. int port;
  954. for (port = 0; port < dev->ports; port++) {
  955. val->port_vlan = port;
  956. if (ar8327_sw_set_port_igmp_snooping(dev, attr, val))
  957. break;
  958. }
  959. return 0;
  960. }
  961. int
  962. ar8327_sw_get_igmp_v3(struct switch_dev *dev,
  963. const struct switch_attr *attr,
  964. struct switch_val *val)
  965. {
  966. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  967. u32 val_reg;
  968. mutex_lock(&priv->reg_mutex);
  969. val_reg = ar8xxx_read(priv, AR8327_REG_FRAME_ACK_CTRL1);
  970. val->value.i = ((val_reg & AR8327_FRAME_ACK_CTRL_IGMP_V3_EN) != 0);
  971. mutex_unlock(&priv->reg_mutex);
  972. return 0;
  973. }
  974. int
  975. ar8327_sw_set_igmp_v3(struct switch_dev *dev,
  976. const struct switch_attr *attr,
  977. struct switch_val *val)
  978. {
  979. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  980. mutex_lock(&priv->reg_mutex);
  981. if (val->value.i)
  982. ar8xxx_reg_set(priv, AR8327_REG_FRAME_ACK_CTRL1,
  983. AR8327_FRAME_ACK_CTRL_IGMP_V3_EN);
  984. else
  985. ar8xxx_reg_clear(priv, AR8327_REG_FRAME_ACK_CTRL1,
  986. AR8327_FRAME_ACK_CTRL_IGMP_V3_EN);
  987. mutex_unlock(&priv->reg_mutex);
  988. return 0;
  989. }
  990. static const struct switch_attr ar8327_sw_attr_globals[] = {
  991. {
  992. .type = SWITCH_TYPE_INT,
  993. .name = "enable_vlan",
  994. .description = "Enable VLAN mode",
  995. .set = ar8xxx_sw_set_vlan,
  996. .get = ar8xxx_sw_get_vlan,
  997. .max = 1
  998. },
  999. {
  1000. .type = SWITCH_TYPE_NOVAL,
  1001. .name = "reset_mibs",
  1002. .description = "Reset all MIB counters",
  1003. .set = ar8xxx_sw_set_reset_mibs,
  1004. },
  1005. {
  1006. .type = SWITCH_TYPE_INT,
  1007. .name = "enable_mirror_rx",
  1008. .description = "Enable mirroring of RX packets",
  1009. .set = ar8xxx_sw_set_mirror_rx_enable,
  1010. .get = ar8xxx_sw_get_mirror_rx_enable,
  1011. .max = 1
  1012. },
  1013. {
  1014. .type = SWITCH_TYPE_INT,
  1015. .name = "enable_mirror_tx",
  1016. .description = "Enable mirroring of TX packets",
  1017. .set = ar8xxx_sw_set_mirror_tx_enable,
  1018. .get = ar8xxx_sw_get_mirror_tx_enable,
  1019. .max = 1
  1020. },
  1021. {
  1022. .type = SWITCH_TYPE_INT,
  1023. .name = "mirror_monitor_port",
  1024. .description = "Mirror monitor port",
  1025. .set = ar8xxx_sw_set_mirror_monitor_port,
  1026. .get = ar8xxx_sw_get_mirror_monitor_port,
  1027. .max = AR8327_NUM_PORTS - 1
  1028. },
  1029. {
  1030. .type = SWITCH_TYPE_INT,
  1031. .name = "mirror_source_port",
  1032. .description = "Mirror source port",
  1033. .set = ar8xxx_sw_set_mirror_source_port,
  1034. .get = ar8xxx_sw_get_mirror_source_port,
  1035. .max = AR8327_NUM_PORTS - 1
  1036. },
  1037. {
  1038. .type = SWITCH_TYPE_STRING,
  1039. .name = "arl_table",
  1040. .description = "Get ARL table",
  1041. .set = NULL,
  1042. .get = ar8xxx_sw_get_arl_table,
  1043. },
  1044. {
  1045. .type = SWITCH_TYPE_NOVAL,
  1046. .name = "flush_arl_table",
  1047. .description = "Flush ARL table",
  1048. .set = ar8xxx_sw_set_flush_arl_table,
  1049. },
  1050. {
  1051. .type = SWITCH_TYPE_INT,
  1052. .name = "igmp_snooping",
  1053. .description = "Enable IGMP Snooping",
  1054. .set = ar8327_sw_set_igmp_snooping,
  1055. .get = ar8327_sw_get_igmp_snooping,
  1056. .max = 1
  1057. },
  1058. {
  1059. .type = SWITCH_TYPE_INT,
  1060. .name = "igmp_v3",
  1061. .description = "Enable IGMPv3 support",
  1062. .set = ar8327_sw_set_igmp_v3,
  1063. .get = ar8327_sw_get_igmp_v3,
  1064. .max = 1
  1065. },
  1066. };
  1067. static const struct switch_attr ar8327_sw_attr_port[] = {
  1068. {
  1069. .type = SWITCH_TYPE_NOVAL,
  1070. .name = "reset_mib",
  1071. .description = "Reset single port MIB counters",
  1072. .set = ar8xxx_sw_set_port_reset_mib,
  1073. },
  1074. {
  1075. .type = SWITCH_TYPE_STRING,
  1076. .name = "mib",
  1077. .description = "Get port's MIB counters",
  1078. .set = NULL,
  1079. .get = ar8xxx_sw_get_port_mib,
  1080. },
  1081. {
  1082. .type = SWITCH_TYPE_INT,
  1083. .name = "enable_eee",
  1084. .description = "Enable EEE PHY sleep mode",
  1085. .set = ar8327_sw_set_eee,
  1086. .get = ar8327_sw_get_eee,
  1087. .max = 1,
  1088. },
  1089. {
  1090. .type = SWITCH_TYPE_NOVAL,
  1091. .name = "flush_arl_table",
  1092. .description = "Flush port's ARL table entries",
  1093. .set = ar8xxx_sw_set_flush_port_arl_table,
  1094. },
  1095. {
  1096. .type = SWITCH_TYPE_INT,
  1097. .name = "igmp_snooping",
  1098. .description = "Enable port's IGMP Snooping",
  1099. .set = ar8327_sw_set_port_igmp_snooping,
  1100. .get = ar8327_sw_get_port_igmp_snooping,
  1101. .max = 1
  1102. },
  1103. };
  1104. static const struct switch_dev_ops ar8327_sw_ops = {
  1105. .attr_global = {
  1106. .attr = ar8327_sw_attr_globals,
  1107. .n_attr = ARRAY_SIZE(ar8327_sw_attr_globals),
  1108. },
  1109. .attr_port = {
  1110. .attr = ar8327_sw_attr_port,
  1111. .n_attr = ARRAY_SIZE(ar8327_sw_attr_port),
  1112. },
  1113. .attr_vlan = {
  1114. .attr = ar8xxx_sw_attr_vlan,
  1115. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
  1116. },
  1117. .get_port_pvid = ar8xxx_sw_get_pvid,
  1118. .set_port_pvid = ar8xxx_sw_set_pvid,
  1119. .get_vlan_ports = ar8327_sw_get_ports,
  1120. .set_vlan_ports = ar8327_sw_set_ports,
  1121. .apply_config = ar8327_sw_hw_apply,
  1122. .reset_switch = ar8xxx_sw_reset_switch,
  1123. .get_port_link = ar8xxx_sw_get_port_link,
  1124. };
  1125. const struct ar8xxx_chip ar8327_chip = {
  1126. .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  1127. .config_at_probe = true,
  1128. .mii_lo_first = true,
  1129. .name = "Atheros AR8327",
  1130. .ports = AR8327_NUM_PORTS,
  1131. .vlans = AR8X16_MAX_VLANS,
  1132. .swops = &ar8327_sw_ops,
  1133. .reg_port_stats_start = 0x1000,
  1134. .reg_port_stats_length = 0x100,
  1135. .hw_init = ar8327_hw_init,
  1136. .cleanup = ar8327_cleanup,
  1137. .init_globals = ar8327_init_globals,
  1138. .init_port = ar8327_init_port,
  1139. .setup_port = ar8327_setup_port,
  1140. .read_port_status = ar8327_read_port_status,
  1141. .read_port_eee_status = ar8327_read_port_eee_status,
  1142. .atu_flush = ar8327_atu_flush,
  1143. .atu_flush_port = ar8327_atu_flush_port,
  1144. .vtu_flush = ar8327_vtu_flush,
  1145. .vtu_load_vlan = ar8327_vtu_load_vlan,
  1146. .phy_fixup = ar8327_phy_fixup,
  1147. .set_mirror_regs = ar8327_set_mirror_regs,
  1148. .get_arl_entry = ar8327_get_arl_entry,
  1149. .sw_hw_apply = ar8327_sw_hw_apply,
  1150. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1151. .mib_decs = ar8236_mibs,
  1152. .mib_func = AR8327_REG_MIB_FUNC
  1153. };
  1154. const struct ar8xxx_chip ar8337_chip = {
  1155. .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  1156. .config_at_probe = true,
  1157. .mii_lo_first = true,
  1158. .name = "Atheros AR8337",
  1159. .ports = AR8327_NUM_PORTS,
  1160. .vlans = AR8X16_MAX_VLANS,
  1161. .swops = &ar8327_sw_ops,
  1162. .reg_port_stats_start = 0x1000,
  1163. .reg_port_stats_length = 0x100,
  1164. .hw_init = ar8327_hw_init,
  1165. .cleanup = ar8327_cleanup,
  1166. .init_globals = ar8327_init_globals,
  1167. .init_port = ar8327_init_port,
  1168. .setup_port = ar8327_setup_port,
  1169. .read_port_status = ar8327_read_port_status,
  1170. .read_port_eee_status = ar8327_read_port_eee_status,
  1171. .atu_flush = ar8327_atu_flush,
  1172. .atu_flush_port = ar8327_atu_flush_port,
  1173. .vtu_flush = ar8327_vtu_flush,
  1174. .vtu_load_vlan = ar8327_vtu_load_vlan,
  1175. .phy_fixup = ar8327_phy_fixup,
  1176. .set_mirror_regs = ar8327_set_mirror_regs,
  1177. .get_arl_entry = ar8327_get_arl_entry,
  1178. .sw_hw_apply = ar8327_sw_hw_apply,
  1179. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1180. .mib_decs = ar8236_mibs,
  1181. .mib_func = AR8327_REG_MIB_FUNC
  1182. };