ar8327.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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. ar8xxx_write(priv, AR8327_REG_PAD5_MODE, t);
  406. t = ar8327_get_pad_cfg(pdata->pad6_cfg);
  407. ar8xxx_write(priv, AR8327_REG_PAD6_MODE, t);
  408. pos = ar8xxx_read(priv, AR8327_REG_POWER_ON_STRIP);
  409. new_pos = pos;
  410. led_cfg = pdata->led_cfg;
  411. if (led_cfg) {
  412. if (led_cfg->open_drain)
  413. new_pos |= AR8327_POWER_ON_STRIP_LED_OPEN_EN;
  414. else
  415. new_pos &= ~AR8327_POWER_ON_STRIP_LED_OPEN_EN;
  416. ar8xxx_write(priv, AR8327_REG_LED_CTRL0, led_cfg->led_ctrl0);
  417. ar8xxx_write(priv, AR8327_REG_LED_CTRL1, led_cfg->led_ctrl1);
  418. ar8xxx_write(priv, AR8327_REG_LED_CTRL2, led_cfg->led_ctrl2);
  419. ar8xxx_write(priv, AR8327_REG_LED_CTRL3, led_cfg->led_ctrl3);
  420. if (new_pos != pos)
  421. new_pos |= AR8327_POWER_ON_STRIP_POWER_ON_SEL;
  422. }
  423. if (pdata->sgmii_cfg) {
  424. t = pdata->sgmii_cfg->sgmii_ctrl;
  425. if (priv->chip_rev == 1)
  426. t |= AR8327_SGMII_CTRL_EN_PLL |
  427. AR8327_SGMII_CTRL_EN_RX |
  428. AR8327_SGMII_CTRL_EN_TX;
  429. else
  430. t &= ~(AR8327_SGMII_CTRL_EN_PLL |
  431. AR8327_SGMII_CTRL_EN_RX |
  432. AR8327_SGMII_CTRL_EN_TX);
  433. ar8xxx_write(priv, AR8327_REG_SGMII_CTRL, t);
  434. if (pdata->sgmii_cfg->serdes_aen)
  435. new_pos &= ~AR8327_POWER_ON_STRIP_SERDES_AEN;
  436. else
  437. new_pos |= AR8327_POWER_ON_STRIP_SERDES_AEN;
  438. }
  439. ar8xxx_write(priv, AR8327_REG_POWER_ON_STRIP, new_pos);
  440. if (pdata->leds && pdata->num_leds) {
  441. int i;
  442. data->leds = kzalloc(pdata->num_leds * sizeof(void *),
  443. GFP_KERNEL);
  444. if (!data->leds)
  445. return -ENOMEM;
  446. for (i = 0; i < pdata->num_leds; i++)
  447. ar8327_led_create(priv, &pdata->leds[i]);
  448. }
  449. return 0;
  450. }
  451. #ifdef CONFIG_OF
  452. static int
  453. ar8327_hw_config_of(struct ar8xxx_priv *priv, struct device_node *np)
  454. {
  455. struct ar8327_data *data = priv->chip_data;
  456. const __be32 *paddr;
  457. int len;
  458. int i;
  459. paddr = of_get_property(np, "qca,ar8327-initvals", &len);
  460. if (!paddr || len < (2 * sizeof(*paddr)))
  461. return -EINVAL;
  462. len /= sizeof(*paddr);
  463. for (i = 0; i < len - 1; i += 2) {
  464. u32 reg;
  465. u32 val;
  466. reg = be32_to_cpup(paddr + i);
  467. val = be32_to_cpup(paddr + i + 1);
  468. switch (reg) {
  469. case AR8327_REG_PORT_STATUS(0):
  470. data->port0_status = val;
  471. break;
  472. case AR8327_REG_PORT_STATUS(6):
  473. data->port6_status = val;
  474. break;
  475. default:
  476. ar8xxx_write(priv, reg, val);
  477. break;
  478. }
  479. }
  480. return 0;
  481. }
  482. #else
  483. static inline int
  484. ar8327_hw_config_of(struct ar8xxx_priv *priv, struct device_node *np)
  485. {
  486. return -EINVAL;
  487. }
  488. #endif
  489. static int
  490. ar8327_hw_init(struct ar8xxx_priv *priv)
  491. {
  492. int ret;
  493. priv->chip_data = kzalloc(sizeof(struct ar8327_data), GFP_KERNEL);
  494. if (!priv->chip_data)
  495. return -ENOMEM;
  496. if (priv->phy->dev.of_node)
  497. ret = ar8327_hw_config_of(priv, priv->phy->dev.of_node);
  498. else
  499. ret = ar8327_hw_config_pdata(priv,
  500. priv->phy->dev.platform_data);
  501. if (ret)
  502. return ret;
  503. ar8327_leds_init(priv);
  504. ar8xxx_phy_init(priv);
  505. return 0;
  506. }
  507. static void
  508. ar8327_cleanup(struct ar8xxx_priv *priv)
  509. {
  510. ar8327_leds_cleanup(priv);
  511. }
  512. static void
  513. ar8327_init_globals(struct ar8xxx_priv *priv)
  514. {
  515. struct ar8327_data *data = priv->chip_data;
  516. u32 t;
  517. int i;
  518. /* enable CPU port and disable mirror port */
  519. t = AR8327_FWD_CTRL0_CPU_PORT_EN |
  520. AR8327_FWD_CTRL0_MIRROR_PORT;
  521. ar8xxx_write(priv, AR8327_REG_FWD_CTRL0, t);
  522. /* forward multicast and broadcast frames to CPU */
  523. t = (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_UC_FLOOD_S) |
  524. (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_MC_FLOOD_S) |
  525. (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_BC_FLOOD_S);
  526. ar8xxx_write(priv, AR8327_REG_FWD_CTRL1, t);
  527. /* enable jumbo frames */
  528. ar8xxx_rmw(priv, AR8327_REG_MAX_FRAME_SIZE,
  529. AR8327_MAX_FRAME_SIZE_MTU, 9018 + 8 + 2);
  530. /* Enable MIB counters */
  531. ar8xxx_reg_set(priv, AR8327_REG_MODULE_EN,
  532. AR8327_MODULE_EN_MIB);
  533. /* Disable EEE on all phy's due to stability issues */
  534. for (i = 0; i < AR8XXX_NUM_PHYS; i++)
  535. data->eee[i] = false;
  536. }
  537. static void
  538. ar8327_init_port(struct ar8xxx_priv *priv, int port)
  539. {
  540. struct ar8327_data *data = priv->chip_data;
  541. u32 t;
  542. if (port == AR8216_PORT_CPU)
  543. t = data->port0_status;
  544. else if (port == 6)
  545. t = data->port6_status;
  546. else
  547. t = AR8216_PORT_STATUS_LINK_AUTO;
  548. if (port != AR8216_PORT_CPU && port != 6) {
  549. /*hw limitation:if configure mac when there is traffic,
  550. port MAC may work abnormal. Need disable lan&wan mac at fisrt*/
  551. ar8xxx_write(priv, AR8327_REG_PORT_STATUS(port), 0);
  552. msleep(100);
  553. t |= AR8216_PORT_STATUS_FLOW_CONTROL;
  554. ar8xxx_write(priv, AR8327_REG_PORT_STATUS(port), t);
  555. } else {
  556. ar8xxx_write(priv, AR8327_REG_PORT_STATUS(port), t);
  557. }
  558. ar8xxx_write(priv, AR8327_REG_PORT_HEADER(port), 0);
  559. ar8xxx_write(priv, AR8327_REG_PORT_VLAN0(port), 0);
  560. t = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH << AR8327_PORT_VLAN1_OUT_MODE_S;
  561. ar8xxx_write(priv, AR8327_REG_PORT_VLAN1(port), t);
  562. t = AR8327_PORT_LOOKUP_LEARN;
  563. t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
  564. ar8xxx_write(priv, AR8327_REG_PORT_LOOKUP(port), t);
  565. }
  566. static u32
  567. ar8327_read_port_status(struct ar8xxx_priv *priv, int port)
  568. {
  569. u32 t;
  570. t = ar8xxx_read(priv, AR8327_REG_PORT_STATUS(port));
  571. /* map the flow control autoneg result bits to the flow control bits
  572. * used in forced mode to allow ar8216_read_port_link detect
  573. * flow control properly if autoneg is used
  574. */
  575. if (t & AR8216_PORT_STATUS_LINK_UP &&
  576. t & AR8216_PORT_STATUS_LINK_AUTO) {
  577. t &= ~(AR8216_PORT_STATUS_TXFLOW | AR8216_PORT_STATUS_RXFLOW);
  578. if (t & AR8327_PORT_STATUS_TXFLOW_AUTO)
  579. t |= AR8216_PORT_STATUS_TXFLOW;
  580. if (t & AR8327_PORT_STATUS_RXFLOW_AUTO)
  581. t |= AR8216_PORT_STATUS_RXFLOW;
  582. }
  583. return t;
  584. }
  585. static u32
  586. ar8327_read_port_eee_status(struct ar8xxx_priv *priv, int port)
  587. {
  588. int phy;
  589. u16 t;
  590. if (port >= priv->dev.ports)
  591. return 0;
  592. if (port == 0 || port == 6)
  593. return 0;
  594. phy = port - 1;
  595. /* EEE Ability Auto-negotiation Result */
  596. t = ar8xxx_phy_mmd_read(priv, phy, 0x7, 0x8000);
  597. return mmd_eee_adv_to_ethtool_adv_t(t);
  598. }
  599. static int
  600. ar8327_atu_flush(struct ar8xxx_priv *priv)
  601. {
  602. int ret;
  603. ret = ar8216_wait_bit(priv, AR8327_REG_ATU_FUNC,
  604. AR8327_ATU_FUNC_BUSY, 0);
  605. if (!ret)
  606. ar8xxx_write(priv, AR8327_REG_ATU_FUNC,
  607. AR8327_ATU_FUNC_OP_FLUSH |
  608. AR8327_ATU_FUNC_BUSY);
  609. return ret;
  610. }
  611. static int
  612. ar8327_atu_flush_port(struct ar8xxx_priv *priv, int port)
  613. {
  614. u32 t;
  615. int ret;
  616. ret = ar8216_wait_bit(priv, AR8327_REG_ATU_FUNC,
  617. AR8327_ATU_FUNC_BUSY, 0);
  618. if (!ret) {
  619. t = (port << AR8327_ATU_PORT_NUM_S);
  620. t |= AR8327_ATU_FUNC_OP_FLUSH_PORT;
  621. t |= AR8327_ATU_FUNC_BUSY;
  622. ar8xxx_write(priv, AR8327_REG_ATU_FUNC, t);
  623. }
  624. return ret;
  625. }
  626. static int
  627. ar8327_get_port_igmp(struct ar8xxx_priv *priv, int port)
  628. {
  629. u32 fwd_ctrl, frame_ack;
  630. fwd_ctrl = (BIT(port) << AR8327_FWD_CTRL1_IGMP_S);
  631. frame_ack = ((AR8327_FRAME_ACK_CTRL_IGMP_MLD |
  632. AR8327_FRAME_ACK_CTRL_IGMP_JOIN |
  633. AR8327_FRAME_ACK_CTRL_IGMP_LEAVE) <<
  634. AR8327_FRAME_ACK_CTRL_S(port));
  635. return (ar8xxx_read(priv, AR8327_REG_FWD_CTRL1) &
  636. fwd_ctrl) == fwd_ctrl &&
  637. (ar8xxx_read(priv, AR8327_REG_FRAME_ACK_CTRL(port)) &
  638. frame_ack) == frame_ack;
  639. }
  640. static void
  641. ar8327_set_port_igmp(struct ar8xxx_priv *priv, int port, int enable)
  642. {
  643. int reg_frame_ack = AR8327_REG_FRAME_ACK_CTRL(port);
  644. u32 val_frame_ack = (AR8327_FRAME_ACK_CTRL_IGMP_MLD |
  645. AR8327_FRAME_ACK_CTRL_IGMP_JOIN |
  646. AR8327_FRAME_ACK_CTRL_IGMP_LEAVE) <<
  647. AR8327_FRAME_ACK_CTRL_S(port);
  648. if (enable) {
  649. ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL1,
  650. BIT(port) << AR8327_FWD_CTRL1_MC_FLOOD_S,
  651. BIT(port) << AR8327_FWD_CTRL1_IGMP_S);
  652. ar8xxx_reg_set(priv, reg_frame_ack, val_frame_ack);
  653. } else {
  654. ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL1,
  655. BIT(port) << AR8327_FWD_CTRL1_IGMP_S,
  656. BIT(port) << AR8327_FWD_CTRL1_MC_FLOOD_S);
  657. ar8xxx_reg_clear(priv, reg_frame_ack, val_frame_ack);
  658. }
  659. }
  660. static void
  661. ar8327_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
  662. {
  663. if (ar8216_wait_bit(priv, AR8327_REG_VTU_FUNC1,
  664. AR8327_VTU_FUNC1_BUSY, 0))
  665. return;
  666. if ((op & AR8327_VTU_FUNC1_OP) == AR8327_VTU_FUNC1_OP_LOAD)
  667. ar8xxx_write(priv, AR8327_REG_VTU_FUNC0, val);
  668. op |= AR8327_VTU_FUNC1_BUSY;
  669. ar8xxx_write(priv, AR8327_REG_VTU_FUNC1, op);
  670. }
  671. static void
  672. ar8327_vtu_flush(struct ar8xxx_priv *priv)
  673. {
  674. ar8327_vtu_op(priv, AR8327_VTU_FUNC1_OP_FLUSH, 0);
  675. }
  676. static void
  677. ar8327_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask)
  678. {
  679. u32 op;
  680. u32 val;
  681. int i;
  682. op = AR8327_VTU_FUNC1_OP_LOAD | (vid << AR8327_VTU_FUNC1_VID_S);
  683. val = AR8327_VTU_FUNC0_VALID | AR8327_VTU_FUNC0_IVL;
  684. for (i = 0; i < AR8327_NUM_PORTS; i++) {
  685. u32 mode;
  686. if ((port_mask & BIT(i)) == 0)
  687. mode = AR8327_VTU_FUNC0_EG_MODE_NOT;
  688. else if (priv->vlan == 0)
  689. mode = AR8327_VTU_FUNC0_EG_MODE_KEEP;
  690. else if ((priv->vlan_tagged & BIT(i)) || (priv->vlan_id[priv->pvid[i]] != vid))
  691. mode = AR8327_VTU_FUNC0_EG_MODE_TAG;
  692. else
  693. mode = AR8327_VTU_FUNC0_EG_MODE_UNTAG;
  694. val |= mode << AR8327_VTU_FUNC0_EG_MODE_S(i);
  695. }
  696. ar8327_vtu_op(priv, op, val);
  697. }
  698. static void
  699. ar8327_setup_port(struct ar8xxx_priv *priv, int port, u32 members)
  700. {
  701. u32 t;
  702. u32 egress, ingress;
  703. u32 pvid = priv->vlan_id[priv->pvid[port]];
  704. if (priv->vlan) {
  705. egress = AR8327_PORT_VLAN1_OUT_MODE_UNMOD;
  706. ingress = AR8216_IN_SECURE;
  707. } else {
  708. egress = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH;
  709. ingress = AR8216_IN_PORT_ONLY;
  710. }
  711. t = pvid << AR8327_PORT_VLAN0_DEF_SVID_S;
  712. t |= pvid << AR8327_PORT_VLAN0_DEF_CVID_S;
  713. ar8xxx_write(priv, AR8327_REG_PORT_VLAN0(port), t);
  714. t = AR8327_PORT_VLAN1_PORT_VLAN_PROP;
  715. t |= egress << AR8327_PORT_VLAN1_OUT_MODE_S;
  716. ar8xxx_write(priv, AR8327_REG_PORT_VLAN1(port), t);
  717. t = members;
  718. t |= AR8327_PORT_LOOKUP_LEARN;
  719. t |= ingress << AR8327_PORT_LOOKUP_IN_MODE_S;
  720. t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
  721. ar8xxx_write(priv, AR8327_REG_PORT_LOOKUP(port), t);
  722. }
  723. static int
  724. ar8327_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
  725. {
  726. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  727. u8 ports = priv->vlan_table[val->port_vlan];
  728. int i;
  729. val->len = 0;
  730. for (i = 0; i < dev->ports; i++) {
  731. struct switch_port *p;
  732. if (!(ports & (1 << i)))
  733. continue;
  734. p = &val->value.ports[val->len++];
  735. p->id = i;
  736. if ((priv->vlan_tagged & (1 << i)) || (priv->pvid[i] != val->port_vlan))
  737. p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
  738. else
  739. p->flags = 0;
  740. }
  741. return 0;
  742. }
  743. static int
  744. ar8327_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
  745. {
  746. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  747. u8 *vt = &priv->vlan_table[val->port_vlan];
  748. int i;
  749. *vt = 0;
  750. for (i = 0; i < val->len; i++) {
  751. struct switch_port *p = &val->value.ports[i];
  752. if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
  753. if (val->port_vlan == priv->pvid[p->id]) {
  754. priv->vlan_tagged |= (1 << p->id);
  755. }
  756. } else {
  757. priv->vlan_tagged &= ~(1 << p->id);
  758. priv->pvid[p->id] = val->port_vlan;
  759. }
  760. *vt |= 1 << p->id;
  761. }
  762. return 0;
  763. }
  764. static void
  765. ar8327_set_mirror_regs(struct ar8xxx_priv *priv)
  766. {
  767. int port;
  768. /* reset all mirror registers */
  769. ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
  770. AR8327_FWD_CTRL0_MIRROR_PORT,
  771. (0xF << AR8327_FWD_CTRL0_MIRROR_PORT_S));
  772. for (port = 0; port < AR8327_NUM_PORTS; port++) {
  773. ar8xxx_reg_clear(priv, AR8327_REG_PORT_LOOKUP(port),
  774. AR8327_PORT_LOOKUP_ING_MIRROR_EN);
  775. ar8xxx_reg_clear(priv, AR8327_REG_PORT_HOL_CTRL1(port),
  776. AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN);
  777. }
  778. /* now enable mirroring if necessary */
  779. if (priv->source_port >= AR8327_NUM_PORTS ||
  780. priv->monitor_port >= AR8327_NUM_PORTS ||
  781. priv->source_port == priv->monitor_port) {
  782. return;
  783. }
  784. ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
  785. AR8327_FWD_CTRL0_MIRROR_PORT,
  786. (priv->monitor_port << AR8327_FWD_CTRL0_MIRROR_PORT_S));
  787. if (priv->mirror_rx)
  788. ar8xxx_reg_set(priv, AR8327_REG_PORT_LOOKUP(priv->source_port),
  789. AR8327_PORT_LOOKUP_ING_MIRROR_EN);
  790. if (priv->mirror_tx)
  791. ar8xxx_reg_set(priv, AR8327_REG_PORT_HOL_CTRL1(priv->source_port),
  792. AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN);
  793. }
  794. static int
  795. ar8327_sw_set_eee(struct switch_dev *dev,
  796. const struct switch_attr *attr,
  797. struct switch_val *val)
  798. {
  799. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  800. struct ar8327_data *data = priv->chip_data;
  801. int port = val->port_vlan;
  802. int phy;
  803. if (port >= dev->ports)
  804. return -EINVAL;
  805. if (port == 0 || port == 6)
  806. return -EOPNOTSUPP;
  807. phy = port - 1;
  808. data->eee[phy] = !!(val->value.i);
  809. return 0;
  810. }
  811. static int
  812. ar8327_sw_get_eee(struct switch_dev *dev,
  813. const struct switch_attr *attr,
  814. struct switch_val *val)
  815. {
  816. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  817. const struct ar8327_data *data = priv->chip_data;
  818. int port = val->port_vlan;
  819. int phy;
  820. if (port >= dev->ports)
  821. return -EINVAL;
  822. if (port == 0 || port == 6)
  823. return -EOPNOTSUPP;
  824. phy = port - 1;
  825. val->value.i = data->eee[phy];
  826. return 0;
  827. }
  828. static void
  829. ar8327_wait_atu_ready(struct ar8xxx_priv *priv, u16 r2, u16 r1)
  830. {
  831. int timeout = 20;
  832. while (ar8xxx_mii_read32(priv, r2, r1) & AR8327_ATU_FUNC_BUSY && --timeout)
  833. udelay(10);
  834. if (!timeout)
  835. pr_err("ar8327: timeout waiting for atu to become ready\n");
  836. }
  837. static void ar8327_get_arl_entry(struct ar8xxx_priv *priv,
  838. struct arl_entry *a, u32 *status, enum arl_op op)
  839. {
  840. struct mii_bus *bus = priv->mii_bus;
  841. u16 r2, page;
  842. u16 r1_data0, r1_data1, r1_data2, r1_func;
  843. u32 t, val0, val1, val2;
  844. int i;
  845. split_addr(AR8327_REG_ATU_DATA0, &r1_data0, &r2, &page);
  846. r2 |= 0x10;
  847. r1_data1 = (AR8327_REG_ATU_DATA1 >> 1) & 0x1e;
  848. r1_data2 = (AR8327_REG_ATU_DATA2 >> 1) & 0x1e;
  849. r1_func = (AR8327_REG_ATU_FUNC >> 1) & 0x1e;
  850. switch (op) {
  851. case AR8XXX_ARL_INITIALIZE:
  852. /* all ATU registers are on the same page
  853. * therefore set page only once
  854. */
  855. bus->write(bus, 0x18, 0, page);
  856. wait_for_page_switch();
  857. ar8327_wait_atu_ready(priv, r2, r1_func);
  858. ar8xxx_mii_write32(priv, r2, r1_data0, 0);
  859. ar8xxx_mii_write32(priv, r2, r1_data1, 0);
  860. ar8xxx_mii_write32(priv, r2, r1_data2, 0);
  861. break;
  862. case AR8XXX_ARL_GET_NEXT:
  863. ar8xxx_mii_write32(priv, r2, r1_func,
  864. AR8327_ATU_FUNC_OP_GET_NEXT |
  865. AR8327_ATU_FUNC_BUSY);
  866. ar8327_wait_atu_ready(priv, r2, r1_func);
  867. val0 = ar8xxx_mii_read32(priv, r2, r1_data0);
  868. val1 = ar8xxx_mii_read32(priv, r2, r1_data1);
  869. val2 = ar8xxx_mii_read32(priv, r2, r1_data2);
  870. *status = val2 & AR8327_ATU_STATUS;
  871. if (!*status)
  872. break;
  873. i = 0;
  874. t = AR8327_ATU_PORT0;
  875. while (!(val1 & t) && ++i < AR8327_NUM_PORTS)
  876. t <<= 1;
  877. a->port = i;
  878. a->mac[0] = (val0 & AR8327_ATU_ADDR0) >> AR8327_ATU_ADDR0_S;
  879. a->mac[1] = (val0 & AR8327_ATU_ADDR1) >> AR8327_ATU_ADDR1_S;
  880. a->mac[2] = (val0 & AR8327_ATU_ADDR2) >> AR8327_ATU_ADDR2_S;
  881. a->mac[3] = (val0 & AR8327_ATU_ADDR3) >> AR8327_ATU_ADDR3_S;
  882. a->mac[4] = (val1 & AR8327_ATU_ADDR4) >> AR8327_ATU_ADDR4_S;
  883. a->mac[5] = (val1 & AR8327_ATU_ADDR5) >> AR8327_ATU_ADDR5_S;
  884. break;
  885. }
  886. }
  887. static int
  888. ar8327_sw_hw_apply(struct switch_dev *dev)
  889. {
  890. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  891. const struct ar8327_data *data = priv->chip_data;
  892. int ret, i;
  893. ret = ar8xxx_sw_hw_apply(dev);
  894. if (ret)
  895. return ret;
  896. for (i=0; i < AR8XXX_NUM_PHYS; i++) {
  897. if (data->eee[i])
  898. ar8xxx_reg_clear(priv, AR8327_REG_EEE_CTRL,
  899. AR8327_EEE_CTRL_DISABLE_PHY(i));
  900. else
  901. ar8xxx_reg_set(priv, AR8327_REG_EEE_CTRL,
  902. AR8327_EEE_CTRL_DISABLE_PHY(i));
  903. }
  904. return 0;
  905. }
  906. int
  907. ar8327_sw_get_port_igmp_snooping(struct switch_dev *dev,
  908. const struct switch_attr *attr,
  909. struct switch_val *val)
  910. {
  911. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  912. int port = val->port_vlan;
  913. if (port >= dev->ports)
  914. return -EINVAL;
  915. mutex_lock(&priv->reg_mutex);
  916. val->value.i = ar8327_get_port_igmp(priv, port);
  917. mutex_unlock(&priv->reg_mutex);
  918. return 0;
  919. }
  920. int
  921. ar8327_sw_set_port_igmp_snooping(struct switch_dev *dev,
  922. const struct switch_attr *attr,
  923. struct switch_val *val)
  924. {
  925. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  926. int port = val->port_vlan;
  927. if (port >= dev->ports)
  928. return -EINVAL;
  929. mutex_lock(&priv->reg_mutex);
  930. ar8327_set_port_igmp(priv, port, val->value.i);
  931. mutex_unlock(&priv->reg_mutex);
  932. return 0;
  933. }
  934. int
  935. ar8327_sw_get_igmp_snooping(struct switch_dev *dev,
  936. const struct switch_attr *attr,
  937. struct switch_val *val)
  938. {
  939. int port;
  940. for (port = 0; port < dev->ports; port++) {
  941. val->port_vlan = port;
  942. if (ar8327_sw_get_port_igmp_snooping(dev, attr, val) ||
  943. !val->value.i)
  944. break;
  945. }
  946. return 0;
  947. }
  948. int
  949. ar8327_sw_set_igmp_snooping(struct switch_dev *dev,
  950. const struct switch_attr *attr,
  951. struct switch_val *val)
  952. {
  953. int port;
  954. for (port = 0; port < dev->ports; port++) {
  955. val->port_vlan = port;
  956. if (ar8327_sw_set_port_igmp_snooping(dev, attr, val))
  957. break;
  958. }
  959. return 0;
  960. }
  961. int
  962. ar8327_sw_get_igmp_v3(struct switch_dev *dev,
  963. const struct switch_attr *attr,
  964. struct switch_val *val)
  965. {
  966. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  967. u32 val_reg;
  968. mutex_lock(&priv->reg_mutex);
  969. val_reg = ar8xxx_read(priv, AR8327_REG_FRAME_ACK_CTRL1);
  970. val->value.i = ((val_reg & AR8327_FRAME_ACK_CTRL_IGMP_V3_EN) != 0);
  971. mutex_unlock(&priv->reg_mutex);
  972. return 0;
  973. }
  974. int
  975. ar8327_sw_set_igmp_v3(struct switch_dev *dev,
  976. const struct switch_attr *attr,
  977. struct switch_val *val)
  978. {
  979. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  980. mutex_lock(&priv->reg_mutex);
  981. if (val->value.i)
  982. ar8xxx_reg_set(priv, AR8327_REG_FRAME_ACK_CTRL1,
  983. AR8327_FRAME_ACK_CTRL_IGMP_V3_EN);
  984. else
  985. ar8xxx_reg_clear(priv, AR8327_REG_FRAME_ACK_CTRL1,
  986. AR8327_FRAME_ACK_CTRL_IGMP_V3_EN);
  987. mutex_unlock(&priv->reg_mutex);
  988. return 0;
  989. }
  990. static const struct switch_attr ar8327_sw_attr_globals[] = {
  991. {
  992. .type = SWITCH_TYPE_INT,
  993. .name = "enable_vlan",
  994. .description = "Enable VLAN mode",
  995. .set = ar8xxx_sw_set_vlan,
  996. .get = ar8xxx_sw_get_vlan,
  997. .max = 1
  998. },
  999. {
  1000. .type = SWITCH_TYPE_NOVAL,
  1001. .name = "reset_mibs",
  1002. .description = "Reset all MIB counters",
  1003. .set = ar8xxx_sw_set_reset_mibs,
  1004. },
  1005. {
  1006. .type = SWITCH_TYPE_INT,
  1007. .name = "enable_mirror_rx",
  1008. .description = "Enable mirroring of RX packets",
  1009. .set = ar8xxx_sw_set_mirror_rx_enable,
  1010. .get = ar8xxx_sw_get_mirror_rx_enable,
  1011. .max = 1
  1012. },
  1013. {
  1014. .type = SWITCH_TYPE_INT,
  1015. .name = "enable_mirror_tx",
  1016. .description = "Enable mirroring of TX packets",
  1017. .set = ar8xxx_sw_set_mirror_tx_enable,
  1018. .get = ar8xxx_sw_get_mirror_tx_enable,
  1019. .max = 1
  1020. },
  1021. {
  1022. .type = SWITCH_TYPE_INT,
  1023. .name = "mirror_monitor_port",
  1024. .description = "Mirror monitor port",
  1025. .set = ar8xxx_sw_set_mirror_monitor_port,
  1026. .get = ar8xxx_sw_get_mirror_monitor_port,
  1027. .max = AR8327_NUM_PORTS - 1
  1028. },
  1029. {
  1030. .type = SWITCH_TYPE_INT,
  1031. .name = "mirror_source_port",
  1032. .description = "Mirror source port",
  1033. .set = ar8xxx_sw_set_mirror_source_port,
  1034. .get = ar8xxx_sw_get_mirror_source_port,
  1035. .max = AR8327_NUM_PORTS - 1
  1036. },
  1037. {
  1038. .type = SWITCH_TYPE_INT,
  1039. .name = "arl_age_time",
  1040. .description = "ARL age time (secs)",
  1041. .set = ar8xxx_sw_set_arl_age_time,
  1042. .get = ar8xxx_sw_get_arl_age_time,
  1043. },
  1044. {
  1045. .type = SWITCH_TYPE_STRING,
  1046. .name = "arl_table",
  1047. .description = "Get ARL table",
  1048. .set = NULL,
  1049. .get = ar8xxx_sw_get_arl_table,
  1050. },
  1051. {
  1052. .type = SWITCH_TYPE_NOVAL,
  1053. .name = "flush_arl_table",
  1054. .description = "Flush ARL table",
  1055. .set = ar8xxx_sw_set_flush_arl_table,
  1056. },
  1057. {
  1058. .type = SWITCH_TYPE_INT,
  1059. .name = "igmp_snooping",
  1060. .description = "Enable IGMP Snooping",
  1061. .set = ar8327_sw_set_igmp_snooping,
  1062. .get = ar8327_sw_get_igmp_snooping,
  1063. .max = 1
  1064. },
  1065. {
  1066. .type = SWITCH_TYPE_INT,
  1067. .name = "igmp_v3",
  1068. .description = "Enable IGMPv3 support",
  1069. .set = ar8327_sw_set_igmp_v3,
  1070. .get = ar8327_sw_get_igmp_v3,
  1071. .max = 1
  1072. },
  1073. };
  1074. static const struct switch_attr ar8327_sw_attr_port[] = {
  1075. {
  1076. .type = SWITCH_TYPE_NOVAL,
  1077. .name = "reset_mib",
  1078. .description = "Reset single port MIB counters",
  1079. .set = ar8xxx_sw_set_port_reset_mib,
  1080. },
  1081. {
  1082. .type = SWITCH_TYPE_STRING,
  1083. .name = "mib",
  1084. .description = "Get port's MIB counters",
  1085. .set = NULL,
  1086. .get = ar8xxx_sw_get_port_mib,
  1087. },
  1088. {
  1089. .type = SWITCH_TYPE_INT,
  1090. .name = "enable_eee",
  1091. .description = "Enable EEE PHY sleep mode",
  1092. .set = ar8327_sw_set_eee,
  1093. .get = ar8327_sw_get_eee,
  1094. .max = 1,
  1095. },
  1096. {
  1097. .type = SWITCH_TYPE_NOVAL,
  1098. .name = "flush_arl_table",
  1099. .description = "Flush port's ARL table entries",
  1100. .set = ar8xxx_sw_set_flush_port_arl_table,
  1101. },
  1102. {
  1103. .type = SWITCH_TYPE_INT,
  1104. .name = "igmp_snooping",
  1105. .description = "Enable port's IGMP Snooping",
  1106. .set = ar8327_sw_set_port_igmp_snooping,
  1107. .get = ar8327_sw_get_port_igmp_snooping,
  1108. .max = 1
  1109. },
  1110. };
  1111. static const struct switch_dev_ops ar8327_sw_ops = {
  1112. .attr_global = {
  1113. .attr = ar8327_sw_attr_globals,
  1114. .n_attr = ARRAY_SIZE(ar8327_sw_attr_globals),
  1115. },
  1116. .attr_port = {
  1117. .attr = ar8327_sw_attr_port,
  1118. .n_attr = ARRAY_SIZE(ar8327_sw_attr_port),
  1119. },
  1120. .attr_vlan = {
  1121. .attr = ar8xxx_sw_attr_vlan,
  1122. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
  1123. },
  1124. .get_port_pvid = ar8xxx_sw_get_pvid,
  1125. .set_port_pvid = ar8xxx_sw_set_pvid,
  1126. .get_vlan_ports = ar8327_sw_get_ports,
  1127. .set_vlan_ports = ar8327_sw_set_ports,
  1128. .apply_config = ar8327_sw_hw_apply,
  1129. .reset_switch = ar8xxx_sw_reset_switch,
  1130. .get_port_link = ar8xxx_sw_get_port_link,
  1131. };
  1132. const struct ar8xxx_chip ar8327_chip = {
  1133. .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  1134. .config_at_probe = true,
  1135. .mii_lo_first = true,
  1136. .name = "Atheros AR8327",
  1137. .ports = AR8327_NUM_PORTS,
  1138. .vlans = AR8X16_MAX_VLANS,
  1139. .swops = &ar8327_sw_ops,
  1140. .reg_port_stats_start = 0x1000,
  1141. .reg_port_stats_length = 0x100,
  1142. .reg_arl_ctrl = AR8327_REG_ARL_CTRL,
  1143. .hw_init = ar8327_hw_init,
  1144. .cleanup = ar8327_cleanup,
  1145. .init_globals = ar8327_init_globals,
  1146. .init_port = ar8327_init_port,
  1147. .setup_port = ar8327_setup_port,
  1148. .read_port_status = ar8327_read_port_status,
  1149. .read_port_eee_status = ar8327_read_port_eee_status,
  1150. .atu_flush = ar8327_atu_flush,
  1151. .atu_flush_port = ar8327_atu_flush_port,
  1152. .vtu_flush = ar8327_vtu_flush,
  1153. .vtu_load_vlan = ar8327_vtu_load_vlan,
  1154. .phy_fixup = ar8327_phy_fixup,
  1155. .set_mirror_regs = ar8327_set_mirror_regs,
  1156. .get_arl_entry = ar8327_get_arl_entry,
  1157. .sw_hw_apply = ar8327_sw_hw_apply,
  1158. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1159. .mib_decs = ar8236_mibs,
  1160. .mib_func = AR8327_REG_MIB_FUNC
  1161. };
  1162. const struct ar8xxx_chip ar8337_chip = {
  1163. .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  1164. .config_at_probe = true,
  1165. .mii_lo_first = true,
  1166. .name = "Atheros AR8337",
  1167. .ports = AR8327_NUM_PORTS,
  1168. .vlans = AR8X16_MAX_VLANS,
  1169. .swops = &ar8327_sw_ops,
  1170. .reg_port_stats_start = 0x1000,
  1171. .reg_port_stats_length = 0x100,
  1172. .reg_arl_ctrl = AR8327_REG_ARL_CTRL,
  1173. .hw_init = ar8327_hw_init,
  1174. .cleanup = ar8327_cleanup,
  1175. .init_globals = ar8327_init_globals,
  1176. .init_port = ar8327_init_port,
  1177. .setup_port = ar8327_setup_port,
  1178. .read_port_status = ar8327_read_port_status,
  1179. .read_port_eee_status = ar8327_read_port_eee_status,
  1180. .atu_flush = ar8327_atu_flush,
  1181. .atu_flush_port = ar8327_atu_flush_port,
  1182. .vtu_flush = ar8327_vtu_flush,
  1183. .vtu_load_vlan = ar8327_vtu_load_vlan,
  1184. .phy_fixup = ar8327_phy_fixup,
  1185. .set_mirror_regs = ar8327_set_mirror_regs,
  1186. .get_arl_entry = ar8327_get_arl_entry,
  1187. .sw_hw_apply = ar8327_sw_hw_apply,
  1188. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1189. .mib_decs = ar8236_mibs,
  1190. .mib_func = AR8327_REG_MIB_FUNC
  1191. };