ar8327.c 34 KB

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