ar8327.c 37 KB

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