ar8327.c 35 KB

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