ar8327.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. /*
  2. * ar8327.c: AR8216 switch driver
  3. *
  4. * Copyright (C) 2009 Felix Fietkau <[email protected]>
  5. * Copyright (C) 2011-2012 Gabor Juhos <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/list.h>
  18. #include <linux/bitops.h>
  19. #include <linux/switch.h>
  20. #include <linux/delay.h>
  21. #include <linux/phy.h>
  22. #include <linux/lockdep.h>
  23. #include <linux/ar8216_platform.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/of_device.h>
  26. #include <linux/leds.h>
  27. #include <linux/mdio.h>
  28. #include "ar8216.h"
  29. #include "ar8327.h"
  30. extern const struct ar8xxx_mib_desc ar8236_mibs[39];
  31. extern const struct switch_attr ar8xxx_sw_attr_vlan[1];
  32. static u32
  33. ar8327_get_pad_cfg(struct ar8327_pad_cfg *cfg)
  34. {
  35. u32 t;
  36. if (!cfg)
  37. return 0;
  38. t = 0;
  39. switch (cfg->mode) {
  40. case AR8327_PAD_NC:
  41. break;
  42. case AR8327_PAD_MAC2MAC_MII:
  43. t = AR8327_PAD_MAC_MII_EN;
  44. if (cfg->rxclk_sel)
  45. t |= AR8327_PAD_MAC_MII_RXCLK_SEL;
  46. if (cfg->txclk_sel)
  47. t |= AR8327_PAD_MAC_MII_TXCLK_SEL;
  48. break;
  49. case AR8327_PAD_MAC2MAC_GMII:
  50. t = AR8327_PAD_MAC_GMII_EN;
  51. if (cfg->rxclk_sel)
  52. t |= AR8327_PAD_MAC_GMII_RXCLK_SEL;
  53. if (cfg->txclk_sel)
  54. t |= AR8327_PAD_MAC_GMII_TXCLK_SEL;
  55. break;
  56. case AR8327_PAD_MAC_SGMII:
  57. t = AR8327_PAD_SGMII_EN;
  58. /*
  59. * WAR for the QUalcomm Atheros AP136 board.
  60. * It seems that RGMII TX/RX delay settings needs to be
  61. * applied for SGMII mode as well, The ethernet is not
  62. * reliable without this.
  63. */
  64. t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
  65. t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
  66. if (cfg->rxclk_delay_en)
  67. t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
  68. if (cfg->txclk_delay_en)
  69. t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
  70. if (cfg->sgmii_delay_en)
  71. t |= AR8327_PAD_SGMII_DELAY_EN;
  72. break;
  73. case AR8327_PAD_MAC2PHY_MII:
  74. t = AR8327_PAD_PHY_MII_EN;
  75. if (cfg->rxclk_sel)
  76. t |= AR8327_PAD_PHY_MII_RXCLK_SEL;
  77. if (cfg->txclk_sel)
  78. t |= AR8327_PAD_PHY_MII_TXCLK_SEL;
  79. break;
  80. case AR8327_PAD_MAC2PHY_GMII:
  81. t = AR8327_PAD_PHY_GMII_EN;
  82. if (cfg->pipe_rxclk_sel)
  83. t |= AR8327_PAD_PHY_GMII_PIPE_RXCLK_SEL;
  84. if (cfg->rxclk_sel)
  85. t |= AR8327_PAD_PHY_GMII_RXCLK_SEL;
  86. if (cfg->txclk_sel)
  87. t |= AR8327_PAD_PHY_GMII_TXCLK_SEL;
  88. break;
  89. case AR8327_PAD_MAC_RGMII:
  90. t = AR8327_PAD_RGMII_EN;
  91. t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
  92. t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
  93. if (cfg->rxclk_delay_en)
  94. t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
  95. if (cfg->txclk_delay_en)
  96. t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
  97. break;
  98. case AR8327_PAD_PHY_GMII:
  99. t = AR8327_PAD_PHYX_GMII_EN;
  100. break;
  101. case AR8327_PAD_PHY_RGMII:
  102. t = AR8327_PAD_PHYX_RGMII_EN;
  103. break;
  104. case AR8327_PAD_PHY_MII:
  105. t = AR8327_PAD_PHYX_MII_EN;
  106. break;
  107. }
  108. return t;
  109. }
  110. static void
  111. ar8327_phy_fixup(struct ar8xxx_priv *priv, int phy)
  112. {
  113. switch (priv->chip_rev) {
  114. case 1:
  115. /* For 100M waveform */
  116. ar8xxx_phy_dbg_write(priv, phy, 0, 0x02ea);
  117. /* Turn on Gigabit clock */
  118. ar8xxx_phy_dbg_write(priv, phy, 0x3d, 0x68a0);
  119. break;
  120. case 2:
  121. ar8xxx_phy_mmd_write(priv, phy, 0x7, 0x3c, 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->mdio.dev.of_node)
  497. ret = ar8327_hw_config_of(priv, priv->phy->mdio.dev.of_node);
  498. else
  499. ret = ar8327_hw_config_pdata(priv,
  500. priv->phy->mdio.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. if (priv->vlan && priv->port_vlan_prio[port]) {
  714. u32 prio = priv->port_vlan_prio[port];
  715. t |= prio << AR8327_PORT_VLAN0_DEF_SPRI_S;
  716. t |= prio << AR8327_PORT_VLAN0_DEF_CPRI_S;
  717. }
  718. ar8xxx_write(priv, AR8327_REG_PORT_VLAN0(port), t);
  719. t = AR8327_PORT_VLAN1_PORT_VLAN_PROP;
  720. t |= egress << AR8327_PORT_VLAN1_OUT_MODE_S;
  721. if (priv->vlan && priv->port_vlan_prio[port])
  722. t |= AR8327_PORT_VLAN1_VLAN_PRI_PROP;
  723. ar8xxx_write(priv, AR8327_REG_PORT_VLAN1(port), t);
  724. t = members;
  725. t |= AR8327_PORT_LOOKUP_LEARN;
  726. t |= ingress << AR8327_PORT_LOOKUP_IN_MODE_S;
  727. t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
  728. ar8xxx_write(priv, AR8327_REG_PORT_LOOKUP(port), t);
  729. }
  730. static int
  731. ar8327_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
  732. {
  733. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  734. u8 ports = priv->vlan_table[val->port_vlan];
  735. int i;
  736. val->len = 0;
  737. for (i = 0; i < dev->ports; i++) {
  738. struct switch_port *p;
  739. if (!(ports & (1 << i)))
  740. continue;
  741. p = &val->value.ports[val->len++];
  742. p->id = i;
  743. if ((priv->vlan_tagged & (1 << i)) || (priv->pvid[i] != val->port_vlan))
  744. p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
  745. else
  746. p->flags = 0;
  747. }
  748. return 0;
  749. }
  750. static int
  751. ar8327_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
  752. {
  753. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  754. u8 *vt = &priv->vlan_table[val->port_vlan];
  755. int i;
  756. *vt = 0;
  757. for (i = 0; i < val->len; i++) {
  758. struct switch_port *p = &val->value.ports[i];
  759. if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
  760. if (val->port_vlan == priv->pvid[p->id]) {
  761. priv->vlan_tagged |= (1 << p->id);
  762. }
  763. } else {
  764. priv->vlan_tagged &= ~(1 << p->id);
  765. priv->pvid[p->id] = val->port_vlan;
  766. }
  767. *vt |= 1 << p->id;
  768. }
  769. return 0;
  770. }
  771. static void
  772. ar8327_set_mirror_regs(struct ar8xxx_priv *priv)
  773. {
  774. int port;
  775. /* reset all mirror registers */
  776. ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
  777. AR8327_FWD_CTRL0_MIRROR_PORT,
  778. (0xF << AR8327_FWD_CTRL0_MIRROR_PORT_S));
  779. for (port = 0; port < AR8327_NUM_PORTS; port++) {
  780. ar8xxx_reg_clear(priv, AR8327_REG_PORT_LOOKUP(port),
  781. AR8327_PORT_LOOKUP_ING_MIRROR_EN);
  782. ar8xxx_reg_clear(priv, AR8327_REG_PORT_HOL_CTRL1(port),
  783. AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN);
  784. }
  785. /* now enable mirroring if necessary */
  786. if (priv->source_port >= AR8327_NUM_PORTS ||
  787. priv->monitor_port >= AR8327_NUM_PORTS ||
  788. priv->source_port == priv->monitor_port) {
  789. return;
  790. }
  791. ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
  792. AR8327_FWD_CTRL0_MIRROR_PORT,
  793. (priv->monitor_port << AR8327_FWD_CTRL0_MIRROR_PORT_S));
  794. if (priv->mirror_rx)
  795. ar8xxx_reg_set(priv, AR8327_REG_PORT_LOOKUP(priv->source_port),
  796. AR8327_PORT_LOOKUP_ING_MIRROR_EN);
  797. if (priv->mirror_tx)
  798. ar8xxx_reg_set(priv, AR8327_REG_PORT_HOL_CTRL1(priv->source_port),
  799. AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN);
  800. }
  801. static int
  802. ar8327_sw_set_eee(struct switch_dev *dev,
  803. const struct switch_attr *attr,
  804. struct switch_val *val)
  805. {
  806. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  807. struct ar8327_data *data = priv->chip_data;
  808. int port = val->port_vlan;
  809. int phy;
  810. if (port >= dev->ports)
  811. return -EINVAL;
  812. if (port == 0 || port == 6)
  813. return -EOPNOTSUPP;
  814. phy = port - 1;
  815. data->eee[phy] = !!(val->value.i);
  816. return 0;
  817. }
  818. static int
  819. ar8327_sw_get_eee(struct switch_dev *dev,
  820. const struct switch_attr *attr,
  821. struct switch_val *val)
  822. {
  823. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  824. const struct ar8327_data *data = priv->chip_data;
  825. int port = val->port_vlan;
  826. int phy;
  827. if (port >= dev->ports)
  828. return -EINVAL;
  829. if (port == 0 || port == 6)
  830. return -EOPNOTSUPP;
  831. phy = port - 1;
  832. val->value.i = data->eee[phy];
  833. return 0;
  834. }
  835. static void
  836. ar8327_wait_atu_ready(struct ar8xxx_priv *priv, u16 r2, u16 r1)
  837. {
  838. int timeout = 20;
  839. while (ar8xxx_mii_read32(priv, r2, r1) & AR8327_ATU_FUNC_BUSY && --timeout) {
  840. udelay(10);
  841. cond_resched();
  842. }
  843. if (!timeout)
  844. pr_err("ar8327: timeout waiting for atu to become ready\n");
  845. }
  846. static void ar8327_get_arl_entry(struct ar8xxx_priv *priv,
  847. struct arl_entry *a, u32 *status, enum arl_op op)
  848. {
  849. struct mii_bus *bus = priv->mii_bus;
  850. u16 r2, page;
  851. u16 r1_data0, r1_data1, r1_data2, r1_func;
  852. u32 val0, val1, val2;
  853. split_addr(AR8327_REG_ATU_DATA0, &r1_data0, &r2, &page);
  854. r2 |= 0x10;
  855. r1_data1 = (AR8327_REG_ATU_DATA1 >> 1) & 0x1e;
  856. r1_data2 = (AR8327_REG_ATU_DATA2 >> 1) & 0x1e;
  857. r1_func = (AR8327_REG_ATU_FUNC >> 1) & 0x1e;
  858. switch (op) {
  859. case AR8XXX_ARL_INITIALIZE:
  860. /* all ATU registers are on the same page
  861. * therefore set page only once
  862. */
  863. bus->write(bus, 0x18, 0, page);
  864. wait_for_page_switch();
  865. ar8327_wait_atu_ready(priv, r2, r1_func);
  866. ar8xxx_mii_write32(priv, r2, r1_data0, 0);
  867. ar8xxx_mii_write32(priv, r2, r1_data1, 0);
  868. ar8xxx_mii_write32(priv, r2, r1_data2, 0);
  869. break;
  870. case AR8XXX_ARL_GET_NEXT:
  871. ar8xxx_mii_write32(priv, r2, r1_func,
  872. AR8327_ATU_FUNC_OP_GET_NEXT |
  873. AR8327_ATU_FUNC_BUSY);
  874. ar8327_wait_atu_ready(priv, r2, r1_func);
  875. val0 = ar8xxx_mii_read32(priv, r2, r1_data0);
  876. val1 = ar8xxx_mii_read32(priv, r2, r1_data1);
  877. val2 = ar8xxx_mii_read32(priv, r2, r1_data2);
  878. *status = val2 & AR8327_ATU_STATUS;
  879. if (!*status)
  880. break;
  881. a->portmap = (val1 & AR8327_ATU_PORTS) >> AR8327_ATU_PORTS_S;
  882. a->mac[0] = (val0 & AR8327_ATU_ADDR0) >> AR8327_ATU_ADDR0_S;
  883. a->mac[1] = (val0 & AR8327_ATU_ADDR1) >> AR8327_ATU_ADDR1_S;
  884. a->mac[2] = (val0 & AR8327_ATU_ADDR2) >> AR8327_ATU_ADDR2_S;
  885. a->mac[3] = (val0 & AR8327_ATU_ADDR3) >> AR8327_ATU_ADDR3_S;
  886. a->mac[4] = (val1 & AR8327_ATU_ADDR4) >> AR8327_ATU_ADDR4_S;
  887. a->mac[5] = (val1 & AR8327_ATU_ADDR5) >> AR8327_ATU_ADDR5_S;
  888. break;
  889. }
  890. }
  891. static int
  892. ar8327_sw_hw_apply(struct switch_dev *dev)
  893. {
  894. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  895. const struct ar8327_data *data = priv->chip_data;
  896. int ret, i;
  897. ret = ar8xxx_sw_hw_apply(dev);
  898. if (ret)
  899. return ret;
  900. for (i=0; i < AR8XXX_NUM_PHYS; i++) {
  901. if (data->eee[i])
  902. ar8xxx_reg_clear(priv, AR8327_REG_EEE_CTRL,
  903. AR8327_EEE_CTRL_DISABLE_PHY(i));
  904. else
  905. ar8xxx_reg_set(priv, AR8327_REG_EEE_CTRL,
  906. AR8327_EEE_CTRL_DISABLE_PHY(i));
  907. }
  908. return 0;
  909. }
  910. int
  911. ar8327_sw_get_port_igmp_snooping(struct switch_dev *dev,
  912. const struct switch_attr *attr,
  913. struct switch_val *val)
  914. {
  915. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  916. int port = val->port_vlan;
  917. if (port >= dev->ports)
  918. return -EINVAL;
  919. mutex_lock(&priv->reg_mutex);
  920. val->value.i = ar8327_get_port_igmp(priv, port);
  921. mutex_unlock(&priv->reg_mutex);
  922. return 0;
  923. }
  924. int
  925. ar8327_sw_set_port_igmp_snooping(struct switch_dev *dev,
  926. const struct switch_attr *attr,
  927. struct switch_val *val)
  928. {
  929. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  930. int port = val->port_vlan;
  931. if (port >= dev->ports)
  932. return -EINVAL;
  933. mutex_lock(&priv->reg_mutex);
  934. ar8327_set_port_igmp(priv, port, val->value.i);
  935. mutex_unlock(&priv->reg_mutex);
  936. return 0;
  937. }
  938. int
  939. ar8327_sw_get_igmp_snooping(struct switch_dev *dev,
  940. const struct switch_attr *attr,
  941. struct switch_val *val)
  942. {
  943. int port;
  944. for (port = 0; port < dev->ports; port++) {
  945. val->port_vlan = port;
  946. if (ar8327_sw_get_port_igmp_snooping(dev, attr, val) ||
  947. !val->value.i)
  948. break;
  949. }
  950. return 0;
  951. }
  952. int
  953. ar8327_sw_set_igmp_snooping(struct switch_dev *dev,
  954. const struct switch_attr *attr,
  955. struct switch_val *val)
  956. {
  957. int port;
  958. for (port = 0; port < dev->ports; port++) {
  959. val->port_vlan = port;
  960. if (ar8327_sw_set_port_igmp_snooping(dev, attr, val))
  961. break;
  962. }
  963. return 0;
  964. }
  965. int
  966. ar8327_sw_get_igmp_v3(struct switch_dev *dev,
  967. const struct switch_attr *attr,
  968. struct switch_val *val)
  969. {
  970. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  971. u32 val_reg;
  972. mutex_lock(&priv->reg_mutex);
  973. val_reg = ar8xxx_read(priv, AR8327_REG_FRAME_ACK_CTRL1);
  974. val->value.i = ((val_reg & AR8327_FRAME_ACK_CTRL_IGMP_V3_EN) != 0);
  975. mutex_unlock(&priv->reg_mutex);
  976. return 0;
  977. }
  978. int
  979. ar8327_sw_set_igmp_v3(struct switch_dev *dev,
  980. const struct switch_attr *attr,
  981. struct switch_val *val)
  982. {
  983. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  984. mutex_lock(&priv->reg_mutex);
  985. if (val->value.i)
  986. ar8xxx_reg_set(priv, AR8327_REG_FRAME_ACK_CTRL1,
  987. AR8327_FRAME_ACK_CTRL_IGMP_V3_EN);
  988. else
  989. ar8xxx_reg_clear(priv, AR8327_REG_FRAME_ACK_CTRL1,
  990. AR8327_FRAME_ACK_CTRL_IGMP_V3_EN);
  991. mutex_unlock(&priv->reg_mutex);
  992. return 0;
  993. }
  994. static int
  995. ar8327_sw_set_port_vlan_prio(struct switch_dev *dev, const struct switch_attr *attr,
  996. struct switch_val *val)
  997. {
  998. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  999. int port = val->port_vlan;
  1000. if (port >= dev->ports)
  1001. return -EINVAL;
  1002. if (port == 0 || port == 6)
  1003. return -EOPNOTSUPP;
  1004. if (val->value.i < 0 || val->value.i > 7)
  1005. return -EINVAL;
  1006. priv->port_vlan_prio[port] = val->value.i;
  1007. return 0;
  1008. }
  1009. static int
  1010. ar8327_sw_get_port_vlan_prio(struct switch_dev *dev, const struct switch_attr *attr,
  1011. struct switch_val *val)
  1012. {
  1013. struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1014. int port = val->port_vlan;
  1015. val->value.i = priv->port_vlan_prio[port];
  1016. return 0;
  1017. }
  1018. static const struct switch_attr ar8327_sw_attr_globals[] = {
  1019. {
  1020. .type = SWITCH_TYPE_INT,
  1021. .name = "enable_vlan",
  1022. .description = "Enable VLAN mode",
  1023. .set = ar8xxx_sw_set_vlan,
  1024. .get = ar8xxx_sw_get_vlan,
  1025. .max = 1
  1026. },
  1027. {
  1028. .type = SWITCH_TYPE_NOVAL,
  1029. .name = "reset_mibs",
  1030. .description = "Reset all MIB counters",
  1031. .set = ar8xxx_sw_set_reset_mibs,
  1032. },
  1033. {
  1034. .type = SWITCH_TYPE_INT,
  1035. .name = "enable_mirror_rx",
  1036. .description = "Enable mirroring of RX packets",
  1037. .set = ar8xxx_sw_set_mirror_rx_enable,
  1038. .get = ar8xxx_sw_get_mirror_rx_enable,
  1039. .max = 1
  1040. },
  1041. {
  1042. .type = SWITCH_TYPE_INT,
  1043. .name = "enable_mirror_tx",
  1044. .description = "Enable mirroring of TX packets",
  1045. .set = ar8xxx_sw_set_mirror_tx_enable,
  1046. .get = ar8xxx_sw_get_mirror_tx_enable,
  1047. .max = 1
  1048. },
  1049. {
  1050. .type = SWITCH_TYPE_INT,
  1051. .name = "mirror_monitor_port",
  1052. .description = "Mirror monitor port",
  1053. .set = ar8xxx_sw_set_mirror_monitor_port,
  1054. .get = ar8xxx_sw_get_mirror_monitor_port,
  1055. .max = AR8327_NUM_PORTS - 1
  1056. },
  1057. {
  1058. .type = SWITCH_TYPE_INT,
  1059. .name = "mirror_source_port",
  1060. .description = "Mirror source port",
  1061. .set = ar8xxx_sw_set_mirror_source_port,
  1062. .get = ar8xxx_sw_get_mirror_source_port,
  1063. .max = AR8327_NUM_PORTS - 1
  1064. },
  1065. {
  1066. .type = SWITCH_TYPE_INT,
  1067. .name = "arl_age_time",
  1068. .description = "ARL age time (secs)",
  1069. .set = ar8xxx_sw_set_arl_age_time,
  1070. .get = ar8xxx_sw_get_arl_age_time,
  1071. },
  1072. {
  1073. .type = SWITCH_TYPE_STRING,
  1074. .name = "arl_table",
  1075. .description = "Get ARL table",
  1076. .set = NULL,
  1077. .get = ar8xxx_sw_get_arl_table,
  1078. },
  1079. {
  1080. .type = SWITCH_TYPE_NOVAL,
  1081. .name = "flush_arl_table",
  1082. .description = "Flush ARL table",
  1083. .set = ar8xxx_sw_set_flush_arl_table,
  1084. },
  1085. {
  1086. .type = SWITCH_TYPE_INT,
  1087. .name = "igmp_snooping",
  1088. .description = "Enable IGMP Snooping",
  1089. .set = ar8327_sw_set_igmp_snooping,
  1090. .get = ar8327_sw_get_igmp_snooping,
  1091. .max = 1
  1092. },
  1093. {
  1094. .type = SWITCH_TYPE_INT,
  1095. .name = "igmp_v3",
  1096. .description = "Enable IGMPv3 support",
  1097. .set = ar8327_sw_set_igmp_v3,
  1098. .get = ar8327_sw_get_igmp_v3,
  1099. .max = 1
  1100. },
  1101. };
  1102. static const struct switch_attr ar8327_sw_attr_port[] = {
  1103. {
  1104. .type = SWITCH_TYPE_NOVAL,
  1105. .name = "reset_mib",
  1106. .description = "Reset single port MIB counters",
  1107. .set = ar8xxx_sw_set_port_reset_mib,
  1108. },
  1109. {
  1110. .type = SWITCH_TYPE_STRING,
  1111. .name = "mib",
  1112. .description = "Get port's MIB counters",
  1113. .set = NULL,
  1114. .get = ar8xxx_sw_get_port_mib,
  1115. },
  1116. {
  1117. .type = SWITCH_TYPE_INT,
  1118. .name = "enable_eee",
  1119. .description = "Enable EEE PHY sleep mode",
  1120. .set = ar8327_sw_set_eee,
  1121. .get = ar8327_sw_get_eee,
  1122. .max = 1,
  1123. },
  1124. {
  1125. .type = SWITCH_TYPE_NOVAL,
  1126. .name = "flush_arl_table",
  1127. .description = "Flush port's ARL table entries",
  1128. .set = ar8xxx_sw_set_flush_port_arl_table,
  1129. },
  1130. {
  1131. .type = SWITCH_TYPE_INT,
  1132. .name = "igmp_snooping",
  1133. .description = "Enable port's IGMP Snooping",
  1134. .set = ar8327_sw_set_port_igmp_snooping,
  1135. .get = ar8327_sw_get_port_igmp_snooping,
  1136. .max = 1
  1137. },
  1138. {
  1139. .type = SWITCH_TYPE_INT,
  1140. .name = "vlan_prio",
  1141. .description = "Port VLAN default priority (VLAN PCP) (0-7)",
  1142. .set = ar8327_sw_set_port_vlan_prio,
  1143. .get = ar8327_sw_get_port_vlan_prio,
  1144. .max = 7,
  1145. },
  1146. };
  1147. static const struct switch_dev_ops ar8327_sw_ops = {
  1148. .attr_global = {
  1149. .attr = ar8327_sw_attr_globals,
  1150. .n_attr = ARRAY_SIZE(ar8327_sw_attr_globals),
  1151. },
  1152. .attr_port = {
  1153. .attr = ar8327_sw_attr_port,
  1154. .n_attr = ARRAY_SIZE(ar8327_sw_attr_port),
  1155. },
  1156. .attr_vlan = {
  1157. .attr = ar8xxx_sw_attr_vlan,
  1158. .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
  1159. },
  1160. .get_port_pvid = ar8xxx_sw_get_pvid,
  1161. .set_port_pvid = ar8xxx_sw_set_pvid,
  1162. .get_vlan_ports = ar8327_sw_get_ports,
  1163. .set_vlan_ports = ar8327_sw_set_ports,
  1164. .apply_config = ar8327_sw_hw_apply,
  1165. .reset_switch = ar8xxx_sw_reset_switch,
  1166. .get_port_link = ar8xxx_sw_get_port_link,
  1167. /* The following op is disabled as it hogs the CPU and degrades performance.
  1168. An implementation has been attempted in 4d8a66d but reading MIB data is slow
  1169. on ar8xxx switches.
  1170. The high CPU load has been traced down to the ar8xxx_reg_wait() call in
  1171. ar8xxx_mib_op(), which has to usleep_range() till the MIB busy flag set by
  1172. the request to update the MIB counter is cleared. */
  1173. #if 0
  1174. .get_port_stats = ar8xxx_sw_get_port_stats,
  1175. #endif
  1176. };
  1177. const struct ar8xxx_chip ar8327_chip = {
  1178. .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  1179. .config_at_probe = true,
  1180. .mii_lo_first = true,
  1181. .name = "Atheros AR8327",
  1182. .ports = AR8327_NUM_PORTS,
  1183. .vlans = AR8X16_MAX_VLANS,
  1184. .swops = &ar8327_sw_ops,
  1185. .reg_port_stats_start = 0x1000,
  1186. .reg_port_stats_length = 0x100,
  1187. .reg_arl_ctrl = AR8327_REG_ARL_CTRL,
  1188. .hw_init = ar8327_hw_init,
  1189. .cleanup = ar8327_cleanup,
  1190. .init_globals = ar8327_init_globals,
  1191. .init_port = ar8327_init_port,
  1192. .setup_port = ar8327_setup_port,
  1193. .read_port_status = ar8327_read_port_status,
  1194. .read_port_eee_status = ar8327_read_port_eee_status,
  1195. .atu_flush = ar8327_atu_flush,
  1196. .atu_flush_port = ar8327_atu_flush_port,
  1197. .vtu_flush = ar8327_vtu_flush,
  1198. .vtu_load_vlan = ar8327_vtu_load_vlan,
  1199. .phy_fixup = ar8327_phy_fixup,
  1200. .set_mirror_regs = ar8327_set_mirror_regs,
  1201. .get_arl_entry = ar8327_get_arl_entry,
  1202. .sw_hw_apply = ar8327_sw_hw_apply,
  1203. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1204. .mib_decs = ar8236_mibs,
  1205. .mib_func = AR8327_REG_MIB_FUNC
  1206. };
  1207. const struct ar8xxx_chip ar8337_chip = {
  1208. .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  1209. .config_at_probe = true,
  1210. .mii_lo_first = true,
  1211. .name = "Atheros AR8337",
  1212. .ports = AR8327_NUM_PORTS,
  1213. .vlans = AR8X16_MAX_VLANS,
  1214. .swops = &ar8327_sw_ops,
  1215. .reg_port_stats_start = 0x1000,
  1216. .reg_port_stats_length = 0x100,
  1217. .reg_arl_ctrl = AR8327_REG_ARL_CTRL,
  1218. .hw_init = ar8327_hw_init,
  1219. .cleanup = ar8327_cleanup,
  1220. .init_globals = ar8327_init_globals,
  1221. .init_port = ar8327_init_port,
  1222. .setup_port = ar8327_setup_port,
  1223. .read_port_status = ar8327_read_port_status,
  1224. .read_port_eee_status = ar8327_read_port_eee_status,
  1225. .atu_flush = ar8327_atu_flush,
  1226. .atu_flush_port = ar8327_atu_flush_port,
  1227. .vtu_flush = ar8327_vtu_flush,
  1228. .vtu_load_vlan = ar8327_vtu_load_vlan,
  1229. .phy_fixup = ar8327_phy_fixup,
  1230. .set_mirror_regs = ar8327_set_mirror_regs,
  1231. .get_arl_entry = ar8327_get_arl_entry,
  1232. .sw_hw_apply = ar8327_sw_hw_apply,
  1233. .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1234. .mib_decs = ar8236_mibs,
  1235. .mib_func = AR8327_REG_MIB_FUNC
  1236. };