ar8327.c 38 KB

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