0015-phy-add-ar8216-PHY-support.patch 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671
  1. From 6137bedf972f576765c6e5d4373a488951371609 Mon Sep 17 00:00:00 2001
  2. From: Phil Sutter <phil@nwl.cc>
  3. Date: Tue, 13 May 2014 00:37:30 +0200
  4. Subject: [PATCH] phy: add ar8216 PHY support
  5. ---
  6. drivers/net/phy/Kconfig | 9 +
  7. drivers/net/phy/Makefile | 1 +
  8. drivers/net/phy/ar8216.c | 2978 +++++++++++++++++++++++++++++++++++++++
  9. drivers/net/phy/ar8216.h | 492 +++++++
  10. include/linux/ar8216_platform.h | 131 ++
  11. 5 files changed, 3611 insertions(+)
  12. create mode 100644 drivers/net/phy/ar8216.c
  13. create mode 100644 drivers/net/phy/ar8216.h
  14. create mode 100644 include/linux/ar8216_platform.h
  15. diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig
  16. index 36a13fc..0414889 100644
  17. --- a/drivers/net/phy/Kconfig
  18. +++ b/drivers/net/phy/Kconfig
  19. @@ -116,6 +116,15 @@ config MICREL_PHY
  20. ---help---
  21. Supports the KSZ9021, VSC8201, KS8001 PHYs.
  22. +config AR8216_PHY
  23. + tristate "Driver for Atheros AR8216 switches"
  24. + select ETHERNET_PACKET_MANGLE
  25. + select SWCONFIG
  26. +
  27. +config AR8216_PHY_LEDS
  28. + bool "Atheros AR8216 switch LED support"
  29. + depends on (AR8216_PHY && LEDS_CLASS)
  30. +
  31. config FIXED_PHY
  32. bool "Driver for MDIO Bus/PHY emulation with fixed speed/link PHYs"
  33. depends on PHYLIB=y
  34. diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile
  35. index b510bd6..3c76ff8 100644
  36. --- a/drivers/net/phy/Makefile
  37. +++ b/drivers/net/phy/Makefile
  38. @@ -16,6 +16,7 @@ obj-$(CONFIG_BCM63XX_PHY) += bcm63xx.o
  39. obj-$(CONFIG_BCM87XX_PHY) += bcm87xx.o
  40. obj-$(CONFIG_ICPLUS_PHY) += icplus.o
  41. obj-$(CONFIG_REALTEK_PHY) += realtek.o
  42. +obj-$(CONFIG_AR8216_PHY) += ar8216.o
  43. obj-$(CONFIG_LSI_ET1011C_PHY) += et1011c.o
  44. obj-$(CONFIG_FIXED_PHY) += fixed.o
  45. obj-$(CONFIG_MDIO_BITBANG) += mdio-bitbang.o
  46. diff --git a/drivers/net/phy/ar8216.c b/drivers/net/phy/ar8216.c
  47. new file mode 100644
  48. index 0000000..3f60878
  49. --- /dev/null
  50. +++ b/drivers/net/phy/ar8216.c
  51. @@ -0,0 +1,2978 @@
  52. +/*
  53. + * ar8216.c: AR8216 switch driver
  54. + *
  55. + * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
  56. + * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
  57. + *
  58. + * This program is free software; you can redistribute it and/or
  59. + * modify it under the terms of the GNU General Public License
  60. + * as published by the Free Software Foundation; either version 2
  61. + * of the License, or (at your option) any later version.
  62. + *
  63. + * This program is distributed in the hope that it will be useful,
  64. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  65. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  66. + * GNU General Public License for more details.
  67. + */
  68. +
  69. +#include <linux/if.h>
  70. +#include <linux/module.h>
  71. +#include <linux/init.h>
  72. +#include <linux/list.h>
  73. +#include <linux/if_ether.h>
  74. +#include <linux/skbuff.h>
  75. +#include <linux/netdevice.h>
  76. +#include <linux/netlink.h>
  77. +#include <linux/bitops.h>
  78. +#include <net/genetlink.h>
  79. +#include <linux/switch.h>
  80. +#include <linux/delay.h>
  81. +#include <linux/phy.h>
  82. +#include <linux/netdevice.h>
  83. +#include <linux/etherdevice.h>
  84. +#include <linux/lockdep.h>
  85. +#include <linux/ar8216_platform.h>
  86. +#include <linux/workqueue.h>
  87. +#include <linux/of_device.h>
  88. +#include <linux/leds.h>
  89. +#include <linux/gpio.h>
  90. +
  91. +#include "ar8216.h"
  92. +
  93. +/* size of the vlan table */
  94. +#define AR8X16_MAX_VLANS 128
  95. +#define AR8X16_PROBE_RETRIES 10
  96. +#define AR8X16_MAX_PORTS 8
  97. +
  98. +#define AR8XXX_MIB_WORK_DELAY 2000 /* msecs */
  99. +
  100. +struct ar8xxx_priv;
  101. +
  102. +#define AR8XXX_CAP_GIGE BIT(0)
  103. +#define AR8XXX_CAP_MIB_COUNTERS BIT(1)
  104. +
  105. +enum {
  106. + AR8XXX_VER_AR8216 = 0x01,
  107. + AR8XXX_VER_AR8236 = 0x03,
  108. + AR8XXX_VER_AR8316 = 0x10,
  109. + AR8XXX_VER_AR8327 = 0x12,
  110. + AR8XXX_VER_AR8337 = 0x13,
  111. +};
  112. +
  113. +struct ar8xxx_mib_desc {
  114. + unsigned int size;
  115. + unsigned int offset;
  116. + const char *name;
  117. +};
  118. +
  119. +struct ar8xxx_chip {
  120. + unsigned long caps;
  121. +
  122. + int (*hw_init)(struct ar8xxx_priv *priv);
  123. + void (*cleanup)(struct ar8xxx_priv *priv);
  124. +
  125. + void (*init_globals)(struct ar8xxx_priv *priv);
  126. + void (*init_port)(struct ar8xxx_priv *priv, int port);
  127. + void (*setup_port)(struct ar8xxx_priv *priv, int port, u32 egress,
  128. + u32 ingress, u32 members, u32 pvid);
  129. + u32 (*read_port_status)(struct ar8xxx_priv *priv, int port);
  130. + int (*atu_flush)(struct ar8xxx_priv *priv);
  131. + void (*vtu_flush)(struct ar8xxx_priv *priv);
  132. + void (*vtu_load_vlan)(struct ar8xxx_priv *priv, u32 vid, u32 port_mask);
  133. +
  134. + const struct ar8xxx_mib_desc *mib_decs;
  135. + unsigned num_mibs;
  136. +};
  137. +
  138. +enum ar8327_led_pattern {
  139. + AR8327_LED_PATTERN_OFF = 0,
  140. + AR8327_LED_PATTERN_BLINK,
  141. + AR8327_LED_PATTERN_ON,
  142. + AR8327_LED_PATTERN_RULE,
  143. +};
  144. +
  145. +struct ar8327_led_entry {
  146. + unsigned reg;
  147. + unsigned shift;
  148. +};
  149. +
  150. +struct ar8327_led {
  151. + struct led_classdev cdev;
  152. + struct ar8xxx_priv *sw_priv;
  153. +
  154. + char *name;
  155. + bool active_low;
  156. + u8 led_num;
  157. + enum ar8327_led_mode mode;
  158. +
  159. + struct mutex mutex;
  160. + spinlock_t lock;
  161. + struct work_struct led_work;
  162. + bool enable_hw_mode;
  163. + enum ar8327_led_pattern pattern;
  164. +};
  165. +
  166. +struct ar8327_data {
  167. + u32 port0_status;
  168. + u32 port6_status;
  169. +
  170. + struct ar8327_led **leds;
  171. + unsigned int num_leds;
  172. +};
  173. +
  174. +struct ar8xxx_priv {
  175. + struct switch_dev dev;
  176. + struct mii_bus *mii_bus;
  177. + struct phy_device *phy;
  178. +
  179. + u32 (*read)(struct ar8xxx_priv *priv, int reg);
  180. + void (*write)(struct ar8xxx_priv *priv, int reg, u32 val);
  181. + u32 (*rmw)(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val);
  182. +
  183. + int (*get_port_link)(unsigned port);
  184. +
  185. + const struct net_device_ops *ndo_old;
  186. + struct net_device_ops ndo;
  187. + struct mutex reg_mutex;
  188. + u8 chip_ver;
  189. + u8 chip_rev;
  190. + const struct ar8xxx_chip *chip;
  191. + union {
  192. + struct ar8327_data ar8327;
  193. + } chip_data;
  194. + bool initialized;
  195. + bool port4_phy;
  196. + char buf[2048];
  197. +
  198. + bool init;
  199. + bool mii_lo_first;
  200. +
  201. + struct mutex mib_lock;
  202. + struct delayed_work mib_work;
  203. + int mib_next_port;
  204. + u64 *mib_stats;
  205. +
  206. + struct list_head list;
  207. + unsigned int use_count;
  208. +
  209. + /* all fields below are cleared on reset */
  210. + bool vlan;
  211. + u16 vlan_id[AR8X16_MAX_VLANS];
  212. + u8 vlan_table[AR8X16_MAX_VLANS];
  213. + u8 vlan_tagged;
  214. + u16 pvid[AR8X16_MAX_PORTS];
  215. +
  216. + /* mirroring */
  217. + bool mirror_rx;
  218. + bool mirror_tx;
  219. + int source_port;
  220. + int monitor_port;
  221. +};
  222. +
  223. +#define MIB_DESC(_s , _o, _n) \
  224. + { \
  225. + .size = (_s), \
  226. + .offset = (_o), \
  227. + .name = (_n), \
  228. + }
  229. +
  230. +static const struct ar8xxx_mib_desc ar8216_mibs[] = {
  231. + MIB_DESC(1, AR8216_STATS_RXBROAD, "RxBroad"),
  232. + MIB_DESC(1, AR8216_STATS_RXPAUSE, "RxPause"),
  233. + MIB_DESC(1, AR8216_STATS_RXMULTI, "RxMulti"),
  234. + MIB_DESC(1, AR8216_STATS_RXFCSERR, "RxFcsErr"),
  235. + MIB_DESC(1, AR8216_STATS_RXALIGNERR, "RxAlignErr"),
  236. + MIB_DESC(1, AR8216_STATS_RXRUNT, "RxRunt"),
  237. + MIB_DESC(1, AR8216_STATS_RXFRAGMENT, "RxFragment"),
  238. + MIB_DESC(1, AR8216_STATS_RX64BYTE, "Rx64Byte"),
  239. + MIB_DESC(1, AR8216_STATS_RX128BYTE, "Rx128Byte"),
  240. + MIB_DESC(1, AR8216_STATS_RX256BYTE, "Rx256Byte"),
  241. + MIB_DESC(1, AR8216_STATS_RX512BYTE, "Rx512Byte"),
  242. + MIB_DESC(1, AR8216_STATS_RX1024BYTE, "Rx1024Byte"),
  243. + MIB_DESC(1, AR8216_STATS_RXMAXBYTE, "RxMaxByte"),
  244. + MIB_DESC(1, AR8216_STATS_RXTOOLONG, "RxTooLong"),
  245. + MIB_DESC(2, AR8216_STATS_RXGOODBYTE, "RxGoodByte"),
  246. + MIB_DESC(2, AR8216_STATS_RXBADBYTE, "RxBadByte"),
  247. + MIB_DESC(1, AR8216_STATS_RXOVERFLOW, "RxOverFlow"),
  248. + MIB_DESC(1, AR8216_STATS_FILTERED, "Filtered"),
  249. + MIB_DESC(1, AR8216_STATS_TXBROAD, "TxBroad"),
  250. + MIB_DESC(1, AR8216_STATS_TXPAUSE, "TxPause"),
  251. + MIB_DESC(1, AR8216_STATS_TXMULTI, "TxMulti"),
  252. + MIB_DESC(1, AR8216_STATS_TXUNDERRUN, "TxUnderRun"),
  253. + MIB_DESC(1, AR8216_STATS_TX64BYTE, "Tx64Byte"),
  254. + MIB_DESC(1, AR8216_STATS_TX128BYTE, "Tx128Byte"),
  255. + MIB_DESC(1, AR8216_STATS_TX256BYTE, "Tx256Byte"),
  256. + MIB_DESC(1, AR8216_STATS_TX512BYTE, "Tx512Byte"),
  257. + MIB_DESC(1, AR8216_STATS_TX1024BYTE, "Tx1024Byte"),
  258. + MIB_DESC(1, AR8216_STATS_TXMAXBYTE, "TxMaxByte"),
  259. + MIB_DESC(1, AR8216_STATS_TXOVERSIZE, "TxOverSize"),
  260. + MIB_DESC(2, AR8216_STATS_TXBYTE, "TxByte"),
  261. + MIB_DESC(1, AR8216_STATS_TXCOLLISION, "TxCollision"),
  262. + MIB_DESC(1, AR8216_STATS_TXABORTCOL, "TxAbortCol"),
  263. + MIB_DESC(1, AR8216_STATS_TXMULTICOL, "TxMultiCol"),
  264. + MIB_DESC(1, AR8216_STATS_TXSINGLECOL, "TxSingleCol"),
  265. + MIB_DESC(1, AR8216_STATS_TXEXCDEFER, "TxExcDefer"),
  266. + MIB_DESC(1, AR8216_STATS_TXDEFER, "TxDefer"),
  267. + MIB_DESC(1, AR8216_STATS_TXLATECOL, "TxLateCol"),
  268. +};
  269. +
  270. +static const struct ar8xxx_mib_desc ar8236_mibs[] = {
  271. + MIB_DESC(1, AR8236_STATS_RXBROAD, "RxBroad"),
  272. + MIB_DESC(1, AR8236_STATS_RXPAUSE, "RxPause"),
  273. + MIB_DESC(1, AR8236_STATS_RXMULTI, "RxMulti"),
  274. + MIB_DESC(1, AR8236_STATS_RXFCSERR, "RxFcsErr"),
  275. + MIB_DESC(1, AR8236_STATS_RXALIGNERR, "RxAlignErr"),
  276. + MIB_DESC(1, AR8236_STATS_RXRUNT, "RxRunt"),
  277. + MIB_DESC(1, AR8236_STATS_RXFRAGMENT, "RxFragment"),
  278. + MIB_DESC(1, AR8236_STATS_RX64BYTE, "Rx64Byte"),
  279. + MIB_DESC(1, AR8236_STATS_RX128BYTE, "Rx128Byte"),
  280. + MIB_DESC(1, AR8236_STATS_RX256BYTE, "Rx256Byte"),
  281. + MIB_DESC(1, AR8236_STATS_RX512BYTE, "Rx512Byte"),
  282. + MIB_DESC(1, AR8236_STATS_RX1024BYTE, "Rx1024Byte"),
  283. + MIB_DESC(1, AR8236_STATS_RX1518BYTE, "Rx1518Byte"),
  284. + MIB_DESC(1, AR8236_STATS_RXMAXBYTE, "RxMaxByte"),
  285. + MIB_DESC(1, AR8236_STATS_RXTOOLONG, "RxTooLong"),
  286. + MIB_DESC(2, AR8236_STATS_RXGOODBYTE, "RxGoodByte"),
  287. + MIB_DESC(2, AR8236_STATS_RXBADBYTE, "RxBadByte"),
  288. + MIB_DESC(1, AR8236_STATS_RXOVERFLOW, "RxOverFlow"),
  289. + MIB_DESC(1, AR8236_STATS_FILTERED, "Filtered"),
  290. + MIB_DESC(1, AR8236_STATS_TXBROAD, "TxBroad"),
  291. + MIB_DESC(1, AR8236_STATS_TXPAUSE, "TxPause"),
  292. + MIB_DESC(1, AR8236_STATS_TXMULTI, "TxMulti"),
  293. + MIB_DESC(1, AR8236_STATS_TXUNDERRUN, "TxUnderRun"),
  294. + MIB_DESC(1, AR8236_STATS_TX64BYTE, "Tx64Byte"),
  295. + MIB_DESC(1, AR8236_STATS_TX128BYTE, "Tx128Byte"),
  296. + MIB_DESC(1, AR8236_STATS_TX256BYTE, "Tx256Byte"),
  297. + MIB_DESC(1, AR8236_STATS_TX512BYTE, "Tx512Byte"),
  298. + MIB_DESC(1, AR8236_STATS_TX1024BYTE, "Tx1024Byte"),
  299. + MIB_DESC(1, AR8236_STATS_TX1518BYTE, "Tx1518Byte"),
  300. + MIB_DESC(1, AR8236_STATS_TXMAXBYTE, "TxMaxByte"),
  301. + MIB_DESC(1, AR8236_STATS_TXOVERSIZE, "TxOverSize"),
  302. + MIB_DESC(2, AR8236_STATS_TXBYTE, "TxByte"),
  303. + MIB_DESC(1, AR8236_STATS_TXCOLLISION, "TxCollision"),
  304. + MIB_DESC(1, AR8236_STATS_TXABORTCOL, "TxAbortCol"),
  305. + MIB_DESC(1, AR8236_STATS_TXMULTICOL, "TxMultiCol"),
  306. + MIB_DESC(1, AR8236_STATS_TXSINGLECOL, "TxSingleCol"),
  307. + MIB_DESC(1, AR8236_STATS_TXEXCDEFER, "TxExcDefer"),
  308. + MIB_DESC(1, AR8236_STATS_TXDEFER, "TxDefer"),
  309. + MIB_DESC(1, AR8236_STATS_TXLATECOL, "TxLateCol"),
  310. +};
  311. +
  312. +static DEFINE_MUTEX(ar8xxx_dev_list_lock);
  313. +static LIST_HEAD(ar8xxx_dev_list);
  314. +
  315. +static inline struct ar8xxx_priv *
  316. +swdev_to_ar8xxx(struct switch_dev *swdev)
  317. +{
  318. + return container_of(swdev, struct ar8xxx_priv, dev);
  319. +}
  320. +
  321. +static inline bool ar8xxx_has_gige(struct ar8xxx_priv *priv)
  322. +{
  323. + return priv->chip->caps & AR8XXX_CAP_GIGE;
  324. +}
  325. +
  326. +static inline bool ar8xxx_has_mib_counters(struct ar8xxx_priv *priv)
  327. +{
  328. + return priv->chip->caps & AR8XXX_CAP_MIB_COUNTERS;
  329. +}
  330. +
  331. +static inline bool chip_is_ar8216(struct ar8xxx_priv *priv)
  332. +{
  333. + return priv->chip_ver == AR8XXX_VER_AR8216;
  334. +}
  335. +
  336. +static inline bool chip_is_ar8236(struct ar8xxx_priv *priv)
  337. +{
  338. + return priv->chip_ver == AR8XXX_VER_AR8236;
  339. +}
  340. +
  341. +static inline bool chip_is_ar8316(struct ar8xxx_priv *priv)
  342. +{
  343. + return priv->chip_ver == AR8XXX_VER_AR8316;
  344. +}
  345. +
  346. +static inline bool chip_is_ar8327(struct ar8xxx_priv *priv)
  347. +{
  348. + return priv->chip_ver == AR8XXX_VER_AR8327;
  349. +}
  350. +
  351. +static inline bool chip_is_ar8337(struct ar8xxx_priv *priv)
  352. +{
  353. + return priv->chip_ver == AR8XXX_VER_AR8337;
  354. +}
  355. +
  356. +static inline void
  357. +split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
  358. +{
  359. + regaddr >>= 1;
  360. + *r1 = regaddr & 0x1e;
  361. +
  362. + regaddr >>= 5;
  363. + *r2 = regaddr & 0x7;
  364. +
  365. + regaddr >>= 3;
  366. + *page = regaddr & 0x1ff;
  367. +}
  368. +
  369. +static u32
  370. +ar8xxx_mii_read(struct ar8xxx_priv *priv, int reg)
  371. +{
  372. + struct mii_bus *bus = priv->mii_bus;
  373. + u16 r1, r2, page;
  374. + u16 lo, hi;
  375. +
  376. + split_addr((u32) reg, &r1, &r2, &page);
  377. +
  378. + mutex_lock(&bus->mdio_lock);
  379. +
  380. + bus->write(bus, 0x18, 0, page);
  381. + usleep_range(1000, 2000); /* wait for the page switch to propagate */
  382. + lo = bus->read(bus, 0x10 | r2, r1);
  383. + hi = bus->read(bus, 0x10 | r2, r1 + 1);
  384. +
  385. + mutex_unlock(&bus->mdio_lock);
  386. +
  387. + return (hi << 16) | lo;
  388. +}
  389. +
  390. +static void
  391. +ar8xxx_mii_write(struct ar8xxx_priv *priv, int reg, u32 val)
  392. +{
  393. + struct mii_bus *bus = priv->mii_bus;
  394. + u16 r1, r2, r3;
  395. + u16 lo, hi;
  396. +
  397. + split_addr((u32) reg, &r1, &r2, &r3);
  398. + lo = val & 0xffff;
  399. + hi = (u16) (val >> 16);
  400. +
  401. + mutex_lock(&bus->mdio_lock);
  402. +
  403. + bus->write(bus, 0x18, 0, r3);
  404. + usleep_range(1000, 2000); /* wait for the page switch to propagate */
  405. + if (priv->mii_lo_first) {
  406. + bus->write(bus, 0x10 | r2, r1, lo);
  407. + bus->write(bus, 0x10 | r2, r1 + 1, hi);
  408. + } else {
  409. + bus->write(bus, 0x10 | r2, r1 + 1, hi);
  410. + bus->write(bus, 0x10 | r2, r1, lo);
  411. + }
  412. +
  413. + mutex_unlock(&bus->mdio_lock);
  414. +}
  415. +
  416. +static u32
  417. +ar8xxx_mii_rmw(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
  418. +{
  419. + struct mii_bus *bus = priv->mii_bus;
  420. + u16 r1, r2, page;
  421. + u16 lo, hi;
  422. + u32 ret;
  423. +
  424. + split_addr((u32) reg, &r1, &r2, &page);
  425. +
  426. + mutex_lock(&bus->mdio_lock);
  427. +
  428. + bus->write(bus, 0x18, 0, page);
  429. + usleep_range(1000, 2000); /* wait for the page switch to propagate */
  430. +
  431. + lo = bus->read(bus, 0x10 | r2, r1);
  432. + hi = bus->read(bus, 0x10 | r2, r1 + 1);
  433. +
  434. + ret = hi << 16 | lo;
  435. + ret &= ~mask;
  436. + ret |= val;
  437. +
  438. + lo = ret & 0xffff;
  439. + hi = (u16) (ret >> 16);
  440. +
  441. + if (priv->mii_lo_first) {
  442. + bus->write(bus, 0x10 | r2, r1, lo);
  443. + bus->write(bus, 0x10 | r2, r1 + 1, hi);
  444. + } else {
  445. + bus->write(bus, 0x10 | r2, r1 + 1, hi);
  446. + bus->write(bus, 0x10 | r2, r1, lo);
  447. + }
  448. +
  449. + mutex_unlock(&bus->mdio_lock);
  450. +
  451. + return ret;
  452. +}
  453. +
  454. +
  455. +static void
  456. +ar8xxx_phy_dbg_write(struct ar8xxx_priv *priv, int phy_addr,
  457. + u16 dbg_addr, u16 dbg_data)
  458. +{
  459. + struct mii_bus *bus = priv->mii_bus;
  460. +
  461. + mutex_lock(&bus->mdio_lock);
  462. + bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
  463. + bus->write(bus, phy_addr, MII_ATH_DBG_DATA, dbg_data);
  464. + mutex_unlock(&bus->mdio_lock);
  465. +}
  466. +
  467. +static void
  468. +ar8xxx_phy_mmd_write(struct ar8xxx_priv *priv, int phy_addr, u16 addr, u16 data)
  469. +{
  470. + struct mii_bus *bus = priv->mii_bus;
  471. +
  472. + mutex_lock(&bus->mdio_lock);
  473. + bus->write(bus, phy_addr, MII_ATH_MMD_ADDR, addr);
  474. + bus->write(bus, phy_addr, MII_ATH_MMD_DATA, data);
  475. + mutex_unlock(&bus->mdio_lock);
  476. +}
  477. +
  478. +static inline u32
  479. +ar8xxx_rmw(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
  480. +{
  481. + return priv->rmw(priv, reg, mask, val);
  482. +}
  483. +
  484. +static inline void
  485. +ar8xxx_reg_set(struct ar8xxx_priv *priv, int reg, u32 val)
  486. +{
  487. + priv->rmw(priv, reg, 0, val);
  488. +}
  489. +
  490. +static int
  491. +ar8xxx_reg_wait(struct ar8xxx_priv *priv, u32 reg, u32 mask, u32 val,
  492. + unsigned timeout)
  493. +{
  494. + int i;
  495. +
  496. + for (i = 0; i < timeout; i++) {
  497. + u32 t;
  498. +
  499. + t = priv->read(priv, reg);
  500. + if ((t & mask) == val)
  501. + return 0;
  502. +
  503. + usleep_range(1000, 2000);
  504. + }
  505. +
  506. + return -ETIMEDOUT;
  507. +}
  508. +
  509. +static int
  510. +ar8xxx_mib_op(struct ar8xxx_priv *priv, u32 op)
  511. +{
  512. + unsigned mib_func;
  513. + int ret;
  514. +
  515. + lockdep_assert_held(&priv->mib_lock);
  516. +
  517. + if (chip_is_ar8327(priv) || chip_is_ar8337(priv))
  518. + mib_func = AR8327_REG_MIB_FUNC;
  519. + else
  520. + mib_func = AR8216_REG_MIB_FUNC;
  521. +
  522. + /* Capture the hardware statistics for all ports */
  523. + ar8xxx_rmw(priv, mib_func, AR8216_MIB_FUNC, (op << AR8216_MIB_FUNC_S));
  524. +
  525. + /* Wait for the capturing to complete. */
  526. + ret = ar8xxx_reg_wait(priv, mib_func, AR8216_MIB_BUSY, 0, 10);
  527. + if (ret)
  528. + goto out;
  529. +
  530. + ret = 0;
  531. +
  532. +out:
  533. + return ret;
  534. +}
  535. +
  536. +static int
  537. +ar8xxx_mib_capture(struct ar8xxx_priv *priv)
  538. +{
  539. + return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_CAPTURE);
  540. +}
  541. +
  542. +static int
  543. +ar8xxx_mib_flush(struct ar8xxx_priv *priv)
  544. +{
  545. + return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_FLUSH);
  546. +}
  547. +
  548. +static void
  549. +ar8xxx_mib_fetch_port_stat(struct ar8xxx_priv *priv, int port, bool flush)
  550. +{
  551. + unsigned int base;
  552. + u64 *mib_stats;
  553. + int i;
  554. +
  555. + WARN_ON(port >= priv->dev.ports);
  556. +
  557. + lockdep_assert_held(&priv->mib_lock);
  558. +
  559. + if (chip_is_ar8327(priv) || chip_is_ar8337(priv))
  560. + base = AR8327_REG_PORT_STATS_BASE(port);
  561. + else if (chip_is_ar8236(priv) ||
  562. + chip_is_ar8316(priv))
  563. + base = AR8236_REG_PORT_STATS_BASE(port);
  564. + else
  565. + base = AR8216_REG_PORT_STATS_BASE(port);
  566. +
  567. + mib_stats = &priv->mib_stats[port * priv->chip->num_mibs];
  568. + for (i = 0; i < priv->chip->num_mibs; i++) {
  569. + const struct ar8xxx_mib_desc *mib;
  570. + u64 t;
  571. +
  572. + mib = &priv->chip->mib_decs[i];
  573. + t = priv->read(priv, base + mib->offset);
  574. + if (mib->size == 2) {
  575. + u64 hi;
  576. +
  577. + hi = priv->read(priv, base + mib->offset + 4);
  578. + t |= hi << 32;
  579. + }
  580. +
  581. + if (flush)
  582. + mib_stats[i] = 0;
  583. + else
  584. + mib_stats[i] += t;
  585. + }
  586. +}
  587. +
  588. +static void
  589. +ar8216_read_port_link(struct ar8xxx_priv *priv, int port,
  590. + struct switch_port_link *link)
  591. +{
  592. + u32 status;
  593. + u32 speed;
  594. +
  595. + memset(link, '\0', sizeof(*link));
  596. +
  597. + status = priv->chip->read_port_status(priv, port);
  598. +
  599. + link->aneg = !!(status & AR8216_PORT_STATUS_LINK_AUTO);
  600. + if (link->aneg) {
  601. + link->link = !!(status & AR8216_PORT_STATUS_LINK_UP);
  602. + } else {
  603. + link->link = true;
  604. +
  605. + if (priv->get_port_link) {
  606. + int err;
  607. +
  608. + err = priv->get_port_link(port);
  609. + if (err >= 0)
  610. + link->link = !!err;
  611. + }
  612. + }
  613. +
  614. + if (!link->link)
  615. + return;
  616. +
  617. + link->duplex = !!(status & AR8216_PORT_STATUS_DUPLEX);
  618. + link->tx_flow = !!(status & AR8216_PORT_STATUS_TXFLOW);
  619. + link->rx_flow = !!(status & AR8216_PORT_STATUS_RXFLOW);
  620. +
  621. + speed = (status & AR8216_PORT_STATUS_SPEED) >>
  622. + AR8216_PORT_STATUS_SPEED_S;
  623. +
  624. + switch (speed) {
  625. + case AR8216_PORT_SPEED_10M:
  626. + link->speed = SWITCH_PORT_SPEED_10;
  627. + break;
  628. + case AR8216_PORT_SPEED_100M:
  629. + link->speed = SWITCH_PORT_SPEED_100;
  630. + break;
  631. + case AR8216_PORT_SPEED_1000M:
  632. + link->speed = SWITCH_PORT_SPEED_1000;
  633. + break;
  634. + default:
  635. + link->speed = SWITCH_PORT_SPEED_UNKNOWN;
  636. + break;
  637. + }
  638. +}
  639. +
  640. +static struct sk_buff *
  641. +ar8216_mangle_tx(struct net_device *dev, struct sk_buff *skb)
  642. +{
  643. + struct ar8xxx_priv *priv = dev->phy_ptr;
  644. + unsigned char *buf;
  645. +
  646. + if (unlikely(!priv))
  647. + goto error;
  648. +
  649. + if (!priv->vlan)
  650. + goto send;
  651. +
  652. + if (unlikely(skb_headroom(skb) < 2)) {
  653. + if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
  654. + goto error;
  655. + }
  656. +
  657. + buf = skb_push(skb, 2);
  658. + buf[0] = 0x10;
  659. + buf[1] = 0x80;
  660. +
  661. +send:
  662. + return skb;
  663. +
  664. +error:
  665. + dev_kfree_skb_any(skb);
  666. + return NULL;
  667. +}
  668. +
  669. +static void
  670. +ar8216_mangle_rx(struct net_device *dev, struct sk_buff *skb)
  671. +{
  672. + struct ar8xxx_priv *priv;
  673. + unsigned char *buf;
  674. + int port, vlan;
  675. +
  676. + priv = dev->phy_ptr;
  677. + if (!priv)
  678. + return;
  679. +
  680. + /* don't strip the header if vlan mode is disabled */
  681. + if (!priv->vlan)
  682. + return;
  683. +
  684. + /* strip header, get vlan id */
  685. + buf = skb->data;
  686. + skb_pull(skb, 2);
  687. +
  688. + /* check for vlan header presence */
  689. + if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
  690. + return;
  691. +
  692. + port = buf[0] & 0xf;
  693. +
  694. + /* no need to fix up packets coming from a tagged source */
  695. + if (priv->vlan_tagged & (1 << port))
  696. + return;
  697. +
  698. + /* lookup port vid from local table, the switch passes an invalid vlan id */
  699. + vlan = priv->vlan_id[priv->pvid[port]];
  700. +
  701. + buf[14 + 2] &= 0xf0;
  702. + buf[14 + 2] |= vlan >> 8;
  703. + buf[15 + 2] = vlan & 0xff;
  704. +}
  705. +
  706. +static int
  707. +ar8216_wait_bit(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
  708. +{
  709. + int timeout = 20;
  710. + u32 t = 0;
  711. +
  712. + while (1) {
  713. + t = priv->read(priv, reg);
  714. + if ((t & mask) == val)
  715. + return 0;
  716. +
  717. + if (timeout-- <= 0)
  718. + break;
  719. +
  720. + udelay(10);
  721. + }
  722. +
  723. + pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
  724. + (unsigned int) reg, t, mask, val);
  725. + return -ETIMEDOUT;
  726. +}
  727. +
  728. +static void
  729. +ar8216_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
  730. +{
  731. + if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
  732. + return;
  733. + if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
  734. + val &= AR8216_VTUDATA_MEMBER;
  735. + val |= AR8216_VTUDATA_VALID;
  736. + priv->write(priv, AR8216_REG_VTU_DATA, val);
  737. + }
  738. + op |= AR8216_VTU_ACTIVE;
  739. + priv->write(priv, AR8216_REG_VTU, op);
  740. +}
  741. +
  742. +static void
  743. +ar8216_vtu_flush(struct ar8xxx_priv *priv)
  744. +{
  745. + ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
  746. +}
  747. +
  748. +static void
  749. +ar8216_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask)
  750. +{
  751. + u32 op;
  752. +
  753. + op = AR8216_VTU_OP_LOAD | (vid << AR8216_VTU_VID_S);
  754. + ar8216_vtu_op(priv, op, port_mask);
  755. +}
  756. +
  757. +static int
  758. +ar8216_atu_flush(struct ar8xxx_priv *priv)
  759. +{
  760. + int ret;
  761. +
  762. + ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
  763. + if (!ret)
  764. + priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
  765. +
  766. + return ret;
  767. +}
  768. +
  769. +static u32
  770. +ar8216_read_port_status(struct ar8xxx_priv *priv, int port)
  771. +{
  772. + return priv->read(priv, AR8216_REG_PORT_STATUS(port));
  773. +}
  774. +
  775. +static void
  776. +ar8216_setup_port(struct ar8xxx_priv *priv, int port, u32 egress, u32 ingress,
  777. + u32 members, u32 pvid)
  778. +{
  779. + u32 header;
  780. +
  781. + if (chip_is_ar8216(priv) && priv->vlan && port == AR8216_PORT_CPU)
  782. + header = AR8216_PORT_CTRL_HEADER;
  783. + else
  784. + header = 0;
  785. +
  786. + ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  787. + AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
  788. + AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
  789. + AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
  790. + AR8216_PORT_CTRL_LEARN | header |
  791. + (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
  792. + (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
  793. +
  794. + ar8xxx_rmw(priv, AR8216_REG_PORT_VLAN(port),
  795. + AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
  796. + AR8216_PORT_VLAN_DEFAULT_ID,
  797. + (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
  798. + (ingress << AR8216_PORT_VLAN_MODE_S) |
  799. + (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
  800. +}
  801. +
  802. +static int
  803. +ar8216_hw_init(struct ar8xxx_priv *priv)
  804. +{
  805. + return 0;
  806. +}
  807. +
  808. +static void
  809. +ar8216_init_globals(struct ar8xxx_priv *priv)
  810. +{
  811. + /* standard atheros magic */
  812. + priv->write(priv, 0x38, 0xc000050e);
  813. +
  814. + ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  815. + AR8216_GCTRL_MTU, 1518 + 8 + 2);
  816. +}
  817. +
  818. +static void
  819. +ar8216_init_port(struct ar8xxx_priv *priv, int port)
  820. +{
  821. + /* Enable port learning and tx */
  822. + priv->write(priv, AR8216_REG_PORT_CTRL(port),
  823. + AR8216_PORT_CTRL_LEARN |
  824. + (4 << AR8216_PORT_CTRL_STATE_S));
  825. +
  826. + priv->write(priv, AR8216_REG_PORT_VLAN(port), 0);
  827. +
  828. + if (port == AR8216_PORT_CPU) {
  829. + priv->write(priv, AR8216_REG_PORT_STATUS(port),
  830. + AR8216_PORT_STATUS_LINK_UP |
  831. + (ar8xxx_has_gige(priv) ?
  832. + AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
  833. + AR8216_PORT_STATUS_TXMAC |
  834. + AR8216_PORT_STATUS_RXMAC |
  835. + (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_RXFLOW : 0) |
  836. + (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_TXFLOW : 0) |
  837. + AR8216_PORT_STATUS_DUPLEX);
  838. + } else {
  839. + priv->write(priv, AR8216_REG_PORT_STATUS(port),
  840. + AR8216_PORT_STATUS_LINK_AUTO);
  841. + }
  842. +}
  843. +
  844. +static const struct ar8xxx_chip ar8216_chip = {
  845. + .caps = AR8XXX_CAP_MIB_COUNTERS,
  846. +
  847. + .hw_init = ar8216_hw_init,
  848. + .init_globals = ar8216_init_globals,
  849. + .init_port = ar8216_init_port,
  850. + .setup_port = ar8216_setup_port,
  851. + .read_port_status = ar8216_read_port_status,
  852. + .atu_flush = ar8216_atu_flush,
  853. + .vtu_flush = ar8216_vtu_flush,
  854. + .vtu_load_vlan = ar8216_vtu_load_vlan,
  855. +
  856. + .num_mibs = ARRAY_SIZE(ar8216_mibs),
  857. + .mib_decs = ar8216_mibs,
  858. +};
  859. +
  860. +static void
  861. +ar8236_setup_port(struct ar8xxx_priv *priv, int port, u32 egress, u32 ingress,
  862. + u32 members, u32 pvid)
  863. +{
  864. + ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  865. + AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
  866. + AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
  867. + AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
  868. + AR8216_PORT_CTRL_LEARN |
  869. + (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
  870. + (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
  871. +
  872. + ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN(port),
  873. + AR8236_PORT_VLAN_DEFAULT_ID,
  874. + (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
  875. +
  876. + ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN2(port),
  877. + AR8236_PORT_VLAN2_VLAN_MODE |
  878. + AR8236_PORT_VLAN2_MEMBER,
  879. + (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
  880. + (members << AR8236_PORT_VLAN2_MEMBER_S));
  881. +}
  882. +
  883. +static int
  884. +ar8236_hw_init(struct ar8xxx_priv *priv)
  885. +{
  886. + int i;
  887. + struct mii_bus *bus;
  888. +
  889. + if (priv->initialized)
  890. + return 0;
  891. +
  892. + /* Initialize the PHYs */
  893. + bus = priv->mii_bus;
  894. + for (i = 0; i < 5; i++) {
  895. + mdiobus_write(bus, i, MII_ADVERTISE,
  896. + ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
  897. + ADVERTISE_PAUSE_ASYM);
  898. + mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  899. + }
  900. + msleep(1000);
  901. +
  902. + priv->initialized = true;
  903. + return 0;
  904. +}
  905. +
  906. +static void
  907. +ar8236_init_globals(struct ar8xxx_priv *priv)
  908. +{
  909. + /* enable jumbo frames */
  910. + ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  911. + AR8316_GCTRL_MTU, 9018 + 8 + 2);
  912. +
  913. + /* Enable MIB counters */
  914. + ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
  915. + (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
  916. + AR8236_MIB_EN);
  917. +}
  918. +
  919. +static const struct ar8xxx_chip ar8236_chip = {
  920. + .caps = AR8XXX_CAP_MIB_COUNTERS,
  921. + .hw_init = ar8236_hw_init,
  922. + .init_globals = ar8236_init_globals,
  923. + .init_port = ar8216_init_port,
  924. + .setup_port = ar8236_setup_port,
  925. + .read_port_status = ar8216_read_port_status,
  926. + .atu_flush = ar8216_atu_flush,
  927. + .vtu_flush = ar8216_vtu_flush,
  928. + .vtu_load_vlan = ar8216_vtu_load_vlan,
  929. +
  930. + .num_mibs = ARRAY_SIZE(ar8236_mibs),
  931. + .mib_decs = ar8236_mibs,
  932. +};
  933. +
  934. +static int
  935. +ar8316_hw_init(struct ar8xxx_priv *priv)
  936. +{
  937. + int i;
  938. + u32 val, newval;
  939. + struct mii_bus *bus;
  940. +
  941. + val = priv->read(priv, AR8316_REG_POSTRIP);
  942. +
  943. + if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  944. + if (priv->port4_phy) {
  945. + /* value taken from Ubiquiti RouterStation Pro */
  946. + newval = 0x81461bea;
  947. + pr_info("ar8316: Using port 4 as PHY\n");
  948. + } else {
  949. + newval = 0x01261be2;
  950. + pr_info("ar8316: Using port 4 as switch port\n");
  951. + }
  952. + } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
  953. + /* value taken from AVM Fritz!Box 7390 sources */
  954. + newval = 0x010e5b71;
  955. + } else {
  956. + /* no known value for phy interface */
  957. + pr_err("ar8316: unsupported mii mode: %d.\n",
  958. + priv->phy->interface);
  959. + return -EINVAL;
  960. + }
  961. +
  962. + if (val == newval)
  963. + goto out;
  964. +
  965. + priv->write(priv, AR8316_REG_POSTRIP, newval);
  966. +
  967. + if (priv->port4_phy &&
  968. + priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
  969. + /* work around for phy4 rgmii mode */
  970. + ar8xxx_phy_dbg_write(priv, 4, 0x12, 0x480c);
  971. + /* rx delay */
  972. + ar8xxx_phy_dbg_write(priv, 4, 0x0, 0x824e);
  973. + /* tx delay */
  974. + ar8xxx_phy_dbg_write(priv, 4, 0x5, 0x3d47);
  975. + msleep(1000);
  976. + }
  977. +
  978. + /* Initialize the ports */
  979. + bus = priv->mii_bus;
  980. + for (i = 0; i < 5; i++) {
  981. + /* initialize the port itself */
  982. + mdiobus_write(bus, i, MII_ADVERTISE,
  983. + ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
  984. + mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
  985. + mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  986. + }
  987. +
  988. + msleep(1000);
  989. +
  990. +out:
  991. + priv->initialized = true;
  992. + return 0;
  993. +}
  994. +
  995. +static void
  996. +ar8316_init_globals(struct ar8xxx_priv *priv)
  997. +{
  998. + /* standard atheros magic */
  999. + priv->write(priv, 0x38, 0xc000050e);
  1000. +
  1001. + /* enable cpu port to receive multicast and broadcast frames */
  1002. + priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
  1003. +
  1004. + /* enable jumbo frames */
  1005. + ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
  1006. + AR8316_GCTRL_MTU, 9018 + 8 + 2);
  1007. +
  1008. + /* Enable MIB counters */
  1009. + ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
  1010. + (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
  1011. + AR8236_MIB_EN);
  1012. +}
  1013. +
  1014. +static const struct ar8xxx_chip ar8316_chip = {
  1015. + .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  1016. + .hw_init = ar8316_hw_init,
  1017. + .init_globals = ar8316_init_globals,
  1018. + .init_port = ar8216_init_port,
  1019. + .setup_port = ar8216_setup_port,
  1020. + .read_port_status = ar8216_read_port_status,
  1021. + .atu_flush = ar8216_atu_flush,
  1022. + .vtu_flush = ar8216_vtu_flush,
  1023. + .vtu_load_vlan = ar8216_vtu_load_vlan,
  1024. +
  1025. + .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1026. + .mib_decs = ar8236_mibs,
  1027. +};
  1028. +
  1029. +static u32
  1030. +ar8327_get_pad_cfg(struct ar8327_pad_cfg *cfg)
  1031. +{
  1032. + u32 t;
  1033. +
  1034. + if (!cfg)
  1035. + return 0;
  1036. +
  1037. + t = 0;
  1038. + switch (cfg->mode) {
  1039. + case AR8327_PAD_NC:
  1040. + break;
  1041. +
  1042. + case AR8327_PAD_MAC2MAC_MII:
  1043. + t = AR8327_PAD_MAC_MII_EN;
  1044. + if (cfg->rxclk_sel)
  1045. + t |= AR8327_PAD_MAC_MII_RXCLK_SEL;
  1046. + if (cfg->txclk_sel)
  1047. + t |= AR8327_PAD_MAC_MII_TXCLK_SEL;
  1048. + break;
  1049. +
  1050. + case AR8327_PAD_MAC2MAC_GMII:
  1051. + t = AR8327_PAD_MAC_GMII_EN;
  1052. + if (cfg->rxclk_sel)
  1053. + t |= AR8327_PAD_MAC_GMII_RXCLK_SEL;
  1054. + if (cfg->txclk_sel)
  1055. + t |= AR8327_PAD_MAC_GMII_TXCLK_SEL;
  1056. + break;
  1057. +
  1058. + case AR8327_PAD_MAC_SGMII:
  1059. + t = AR8327_PAD_SGMII_EN;
  1060. +
  1061. + /*
  1062. + * WAR for the QUalcomm Atheros AP136 board.
  1063. + * It seems that RGMII TX/RX delay settings needs to be
  1064. + * applied for SGMII mode as well, The ethernet is not
  1065. + * reliable without this.
  1066. + */
  1067. + t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
  1068. + t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
  1069. + if (cfg->rxclk_delay_en)
  1070. + t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
  1071. + if (cfg->txclk_delay_en)
  1072. + t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
  1073. +
  1074. + if (cfg->sgmii_delay_en)
  1075. + t |= AR8327_PAD_SGMII_DELAY_EN;
  1076. +
  1077. + break;
  1078. +
  1079. + case AR8327_PAD_MAC2PHY_MII:
  1080. + t = AR8327_PAD_PHY_MII_EN;
  1081. + if (cfg->rxclk_sel)
  1082. + t |= AR8327_PAD_PHY_MII_RXCLK_SEL;
  1083. + if (cfg->txclk_sel)
  1084. + t |= AR8327_PAD_PHY_MII_TXCLK_SEL;
  1085. + break;
  1086. +
  1087. + case AR8327_PAD_MAC2PHY_GMII:
  1088. + t = AR8327_PAD_PHY_GMII_EN;
  1089. + if (cfg->pipe_rxclk_sel)
  1090. + t |= AR8327_PAD_PHY_GMII_PIPE_RXCLK_SEL;
  1091. + if (cfg->rxclk_sel)
  1092. + t |= AR8327_PAD_PHY_GMII_RXCLK_SEL;
  1093. + if (cfg->txclk_sel)
  1094. + t |= AR8327_PAD_PHY_GMII_TXCLK_SEL;
  1095. + break;
  1096. +
  1097. + case AR8327_PAD_MAC_RGMII:
  1098. + t = AR8327_PAD_RGMII_EN;
  1099. + t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
  1100. + t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
  1101. + if (cfg->rxclk_delay_en)
  1102. + t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
  1103. + if (cfg->txclk_delay_en)
  1104. + t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
  1105. + break;
  1106. +
  1107. + case AR8327_PAD_PHY_GMII:
  1108. + t = AR8327_PAD_PHYX_GMII_EN;
  1109. + break;
  1110. +
  1111. + case AR8327_PAD_PHY_RGMII:
  1112. + t = AR8327_PAD_PHYX_RGMII_EN;
  1113. + break;
  1114. +
  1115. + case AR8327_PAD_PHY_MII:
  1116. + t = AR8327_PAD_PHYX_MII_EN;
  1117. + break;
  1118. + }
  1119. +
  1120. + return t;
  1121. +}
  1122. +
  1123. +static void
  1124. +ar8327_phy_fixup(struct ar8xxx_priv *priv, int phy)
  1125. +{
  1126. + switch (priv->chip_rev) {
  1127. + case 1:
  1128. + /* For 100M waveform */
  1129. + ar8xxx_phy_dbg_write(priv, phy, 0, 0x02ea);
  1130. + /* Turn on Gigabit clock */
  1131. + ar8xxx_phy_dbg_write(priv, phy, 0x3d, 0x68a0);
  1132. + break;
  1133. +
  1134. + case 2:
  1135. + ar8xxx_phy_mmd_write(priv, phy, 0x7, 0x3c);
  1136. + ar8xxx_phy_mmd_write(priv, phy, 0x4007, 0x0);
  1137. + /* fallthrough */
  1138. + case 4:
  1139. + ar8xxx_phy_mmd_write(priv, phy, 0x3, 0x800d);
  1140. + ar8xxx_phy_mmd_write(priv, phy, 0x4003, 0x803f);
  1141. +
  1142. + ar8xxx_phy_dbg_write(priv, phy, 0x3d, 0x6860);
  1143. + ar8xxx_phy_dbg_write(priv, phy, 0x5, 0x2c46);
  1144. + ar8xxx_phy_dbg_write(priv, phy, 0x3c, 0x6000);
  1145. + break;
  1146. + }
  1147. +}
  1148. +
  1149. +static u32
  1150. +ar8327_get_port_init_status(struct ar8327_port_cfg *cfg)
  1151. +{
  1152. + u32 t;
  1153. +
  1154. + if (!cfg->force_link)
  1155. + return AR8216_PORT_STATUS_LINK_AUTO;
  1156. +
  1157. + t = AR8216_PORT_STATUS_TXMAC | AR8216_PORT_STATUS_RXMAC;
  1158. + t |= cfg->duplex ? AR8216_PORT_STATUS_DUPLEX : 0;
  1159. + t |= cfg->rxpause ? AR8216_PORT_STATUS_RXFLOW : 0;
  1160. + t |= cfg->txpause ? AR8216_PORT_STATUS_TXFLOW : 0;
  1161. +
  1162. + switch (cfg->speed) {
  1163. + case AR8327_PORT_SPEED_10:
  1164. + t |= AR8216_PORT_SPEED_10M;
  1165. + break;
  1166. + case AR8327_PORT_SPEED_100:
  1167. + t |= AR8216_PORT_SPEED_100M;
  1168. + break;
  1169. + case AR8327_PORT_SPEED_1000:
  1170. + t |= AR8216_PORT_SPEED_1000M;
  1171. + break;
  1172. + }
  1173. +
  1174. + return t;
  1175. +}
  1176. +
  1177. +#define AR8327_LED_ENTRY(_num, _reg, _shift) \
  1178. + [_num] = { .reg = (_reg), .shift = (_shift) }
  1179. +
  1180. +static const struct ar8327_led_entry
  1181. +ar8327_led_map[AR8327_NUM_LEDS] = {
  1182. + AR8327_LED_ENTRY(AR8327_LED_PHY0_0, 0, 14),
  1183. + AR8327_LED_ENTRY(AR8327_LED_PHY0_1, 1, 14),
  1184. + AR8327_LED_ENTRY(AR8327_LED_PHY0_2, 2, 14),
  1185. +
  1186. + AR8327_LED_ENTRY(AR8327_LED_PHY1_0, 3, 8),
  1187. + AR8327_LED_ENTRY(AR8327_LED_PHY1_1, 3, 10),
  1188. + AR8327_LED_ENTRY(AR8327_LED_PHY1_2, 3, 12),
  1189. +
  1190. + AR8327_LED_ENTRY(AR8327_LED_PHY2_0, 3, 14),
  1191. + AR8327_LED_ENTRY(AR8327_LED_PHY2_1, 3, 16),
  1192. + AR8327_LED_ENTRY(AR8327_LED_PHY2_2, 3, 18),
  1193. +
  1194. + AR8327_LED_ENTRY(AR8327_LED_PHY3_0, 3, 20),
  1195. + AR8327_LED_ENTRY(AR8327_LED_PHY3_1, 3, 22),
  1196. + AR8327_LED_ENTRY(AR8327_LED_PHY3_2, 3, 24),
  1197. +
  1198. + AR8327_LED_ENTRY(AR8327_LED_PHY4_0, 0, 30),
  1199. + AR8327_LED_ENTRY(AR8327_LED_PHY4_1, 1, 30),
  1200. + AR8327_LED_ENTRY(AR8327_LED_PHY4_2, 2, 30),
  1201. +};
  1202. +
  1203. +static void
  1204. +ar8327_set_led_pattern(struct ar8xxx_priv *priv, unsigned int led_num,
  1205. + enum ar8327_led_pattern pattern)
  1206. +{
  1207. + const struct ar8327_led_entry *entry;
  1208. +
  1209. + entry = &ar8327_led_map[led_num];
  1210. + ar8xxx_rmw(priv, AR8327_REG_LED_CTRL(entry->reg),
  1211. + (3 << entry->shift), pattern << entry->shift);
  1212. +}
  1213. +
  1214. +static void
  1215. +ar8327_led_work_func(struct work_struct *work)
  1216. +{
  1217. + struct ar8327_led *aled;
  1218. + u8 pattern;
  1219. +
  1220. + aled = container_of(work, struct ar8327_led, led_work);
  1221. +
  1222. + spin_lock(&aled->lock);
  1223. + pattern = aled->pattern;
  1224. + spin_unlock(&aled->lock);
  1225. +
  1226. + ar8327_set_led_pattern(aled->sw_priv, aled->led_num,
  1227. + pattern);
  1228. +}
  1229. +
  1230. +static void
  1231. +ar8327_led_schedule_change(struct ar8327_led *aled, u8 pattern)
  1232. +{
  1233. + if (aled->pattern == pattern)
  1234. + return;
  1235. +
  1236. + aled->pattern = pattern;
  1237. + schedule_work(&aled->led_work);
  1238. +}
  1239. +
  1240. +static inline struct ar8327_led *
  1241. +led_cdev_to_ar8327_led(struct led_classdev *led_cdev)
  1242. +{
  1243. + return container_of(led_cdev, struct ar8327_led, cdev);
  1244. +}
  1245. +
  1246. +static int
  1247. +ar8327_led_blink_set(struct led_classdev *led_cdev,
  1248. + unsigned long *delay_on,
  1249. + unsigned long *delay_off)
  1250. +{
  1251. + struct ar8327_led *aled = led_cdev_to_ar8327_led(led_cdev);
  1252. +
  1253. + if (*delay_on == 0 && *delay_off == 0) {
  1254. + *delay_on = 125;
  1255. + *delay_off = 125;
  1256. + }
  1257. +
  1258. + if (*delay_on != 125 || *delay_off != 125) {
  1259. + /*
  1260. + * The hardware only supports blinking at 4Hz. Fall back
  1261. + * to software implementation in other cases.
  1262. + */
  1263. + return -EINVAL;
  1264. + }
  1265. +
  1266. + spin_lock(&aled->lock);
  1267. +
  1268. + aled->enable_hw_mode = false;
  1269. + ar8327_led_schedule_change(aled, AR8327_LED_PATTERN_BLINK);
  1270. +
  1271. + spin_unlock(&aled->lock);
  1272. +
  1273. + return 0;
  1274. +}
  1275. +
  1276. +static void
  1277. +ar8327_led_set_brightness(struct led_classdev *led_cdev,
  1278. + enum led_brightness brightness)
  1279. +{
  1280. + struct ar8327_led *aled = led_cdev_to_ar8327_led(led_cdev);
  1281. + u8 pattern;
  1282. + bool active;
  1283. +
  1284. + active = (brightness != LED_OFF);
  1285. + active ^= aled->active_low;
  1286. +
  1287. + pattern = (active) ? AR8327_LED_PATTERN_ON :
  1288. + AR8327_LED_PATTERN_OFF;
  1289. +
  1290. + spin_lock(&aled->lock);
  1291. +
  1292. + aled->enable_hw_mode = false;
  1293. + ar8327_led_schedule_change(aled, pattern);
  1294. +
  1295. + spin_unlock(&aled->lock);
  1296. +}
  1297. +
  1298. +static ssize_t
  1299. +ar8327_led_enable_hw_mode_show(struct device *dev,
  1300. + struct device_attribute *attr,
  1301. + char *buf)
  1302. +{
  1303. + struct led_classdev *led_cdev = dev_get_drvdata(dev);
  1304. + struct ar8327_led *aled = led_cdev_to_ar8327_led(led_cdev);
  1305. + ssize_t ret = 0;
  1306. +
  1307. + spin_lock(&aled->lock);
  1308. + ret += sprintf(buf, "%d\n", aled->enable_hw_mode);
  1309. + spin_unlock(&aled->lock);
  1310. +
  1311. + return ret;
  1312. +}
  1313. +
  1314. +static ssize_t
  1315. +ar8327_led_enable_hw_mode_store(struct device *dev,
  1316. + struct device_attribute *attr,
  1317. + const char *buf,
  1318. + size_t size)
  1319. +{
  1320. + struct led_classdev *led_cdev = dev_get_drvdata(dev);
  1321. + struct ar8327_led *aled = led_cdev_to_ar8327_led(led_cdev);
  1322. + u8 pattern;
  1323. + u8 value;
  1324. + int ret;
  1325. +
  1326. + ret = kstrtou8(buf, 10, &value);
  1327. + if (ret < 0)
  1328. + return -EINVAL;
  1329. +
  1330. + spin_lock(&aled->lock);
  1331. +
  1332. + aled->enable_hw_mode = !!value;
  1333. + if (aled->enable_hw_mode)
  1334. + pattern = AR8327_LED_PATTERN_RULE;
  1335. + else
  1336. + pattern = AR8327_LED_PATTERN_OFF;
  1337. +
  1338. + ar8327_led_schedule_change(aled, pattern);
  1339. +
  1340. + spin_unlock(&aled->lock);
  1341. +
  1342. + return size;
  1343. +}
  1344. +
  1345. +static DEVICE_ATTR(enable_hw_mode, S_IRUGO | S_IWUSR,
  1346. + ar8327_led_enable_hw_mode_show,
  1347. + ar8327_led_enable_hw_mode_store);
  1348. +
  1349. +static int
  1350. +ar8327_led_register(struct ar8xxx_priv *priv, struct ar8327_led *aled)
  1351. +{
  1352. + int ret;
  1353. +
  1354. + ret = led_classdev_register(NULL, &aled->cdev);
  1355. + if (ret < 0)
  1356. + return ret;
  1357. +
  1358. + if (aled->mode == AR8327_LED_MODE_HW) {
  1359. + ret = device_create_file(aled->cdev.dev,
  1360. + &dev_attr_enable_hw_mode);
  1361. + if (ret)
  1362. + goto err_unregister;
  1363. + }
  1364. +
  1365. + return 0;
  1366. +
  1367. +err_unregister:
  1368. + led_classdev_unregister(&aled->cdev);
  1369. + return ret;
  1370. +}
  1371. +
  1372. +static void
  1373. +ar8327_led_unregister(struct ar8327_led *aled)
  1374. +{
  1375. + if (aled->mode == AR8327_LED_MODE_HW)
  1376. + device_remove_file(aled->cdev.dev, &dev_attr_enable_hw_mode);
  1377. +
  1378. + led_classdev_unregister(&aled->cdev);
  1379. + cancel_work_sync(&aled->led_work);
  1380. +}
  1381. +
  1382. +static int
  1383. +ar8327_led_create(struct ar8xxx_priv *priv,
  1384. + const struct ar8327_led_info *led_info)
  1385. +{
  1386. + struct ar8327_data *data = &priv->chip_data.ar8327;
  1387. + struct ar8327_led *aled;
  1388. + int ret;
  1389. +
  1390. + if (!IS_ENABLED(CONFIG_AR8216_PHY_LEDS))
  1391. + return 0;
  1392. +
  1393. + if (!led_info->name)
  1394. + return -EINVAL;
  1395. +
  1396. + if (led_info->led_num >= AR8327_NUM_LEDS)
  1397. + return -EINVAL;
  1398. +
  1399. + aled = kzalloc(sizeof(*aled) + strlen(led_info->name) + 1,
  1400. + GFP_KERNEL);
  1401. + if (!aled)
  1402. + return -ENOMEM;
  1403. +
  1404. + aled->sw_priv = priv;
  1405. + aled->led_num = led_info->led_num;
  1406. + aled->active_low = led_info->active_low;
  1407. + aled->mode = led_info->mode;
  1408. +
  1409. + if (aled->mode == AR8327_LED_MODE_HW)
  1410. + aled->enable_hw_mode = true;
  1411. +
  1412. + aled->name = (char *)(aled + 1);
  1413. + strcpy(aled->name, led_info->name);
  1414. +
  1415. + aled->cdev.name = aled->name;
  1416. + aled->cdev.brightness_set = ar8327_led_set_brightness;
  1417. + aled->cdev.blink_set = ar8327_led_blink_set;
  1418. + aled->cdev.default_trigger = led_info->default_trigger;
  1419. +
  1420. + spin_lock_init(&aled->lock);
  1421. + mutex_init(&aled->mutex);
  1422. + INIT_WORK(&aled->led_work, ar8327_led_work_func);
  1423. +
  1424. + ret = ar8327_led_register(priv, aled);
  1425. + if (ret)
  1426. + goto err_free;
  1427. +
  1428. + data->leds[data->num_leds++] = aled;
  1429. +
  1430. + return 0;
  1431. +
  1432. +err_free:
  1433. + kfree(aled);
  1434. + return ret;
  1435. +}
  1436. +
  1437. +static void
  1438. +ar8327_led_destroy(struct ar8327_led *aled)
  1439. +{
  1440. + ar8327_led_unregister(aled);
  1441. + kfree(aled);
  1442. +}
  1443. +
  1444. +static void
  1445. +ar8327_leds_init(struct ar8xxx_priv *priv)
  1446. +{
  1447. + struct ar8327_data *data;
  1448. + unsigned i;
  1449. +
  1450. + if (!IS_ENABLED(CONFIG_AR8216_PHY_LEDS))
  1451. + return;
  1452. +
  1453. + data = &priv->chip_data.ar8327;
  1454. +
  1455. + for (i = 0; i < data->num_leds; i++) {
  1456. + struct ar8327_led *aled;
  1457. +
  1458. + aled = data->leds[i];
  1459. +
  1460. + if (aled->enable_hw_mode)
  1461. + aled->pattern = AR8327_LED_PATTERN_RULE;
  1462. + else
  1463. + aled->pattern = AR8327_LED_PATTERN_OFF;
  1464. +
  1465. + ar8327_set_led_pattern(priv, aled->led_num, aled->pattern);
  1466. + }
  1467. +}
  1468. +
  1469. +static void
  1470. +ar8327_leds_cleanup(struct ar8xxx_priv *priv)
  1471. +{
  1472. + struct ar8327_data *data = &priv->chip_data.ar8327;
  1473. + unsigned i;
  1474. +
  1475. + if (!IS_ENABLED(CONFIG_AR8216_PHY_LEDS))
  1476. + return;
  1477. +
  1478. + for (i = 0; i < data->num_leds; i++) {
  1479. + struct ar8327_led *aled;
  1480. +
  1481. + aled = data->leds[i];
  1482. + ar8327_led_destroy(aled);
  1483. + }
  1484. +
  1485. + kfree(data->leds);
  1486. +}
  1487. +
  1488. +static int
  1489. +ar8327_hw_config_pdata(struct ar8xxx_priv *priv,
  1490. + struct ar8327_platform_data *pdata)
  1491. +{
  1492. + struct ar8327_led_cfg *led_cfg;
  1493. + struct ar8327_data *data;
  1494. + u32 pos, new_pos;
  1495. + u32 t;
  1496. +
  1497. + if (!pdata)
  1498. + return -EINVAL;
  1499. +
  1500. + priv->get_port_link = pdata->get_port_link;
  1501. +
  1502. + data = &priv->chip_data.ar8327;
  1503. +
  1504. + data->port0_status = ar8327_get_port_init_status(&pdata->port0_cfg);
  1505. + data->port6_status = ar8327_get_port_init_status(&pdata->port6_cfg);
  1506. +
  1507. + t = ar8327_get_pad_cfg(pdata->pad0_cfg);
  1508. + if (chip_is_ar8337(priv))
  1509. + t |= AR8337_PAD_MAC06_EXCHANGE_EN;
  1510. +
  1511. + priv->write(priv, AR8327_REG_PAD0_MODE, t);
  1512. + t = ar8327_get_pad_cfg(pdata->pad5_cfg);
  1513. + priv->write(priv, AR8327_REG_PAD5_MODE, t);
  1514. + t = ar8327_get_pad_cfg(pdata->pad6_cfg);
  1515. + priv->write(priv, AR8327_REG_PAD6_MODE, t);
  1516. +
  1517. + pos = priv->read(priv, AR8327_REG_POWER_ON_STRIP);
  1518. + new_pos = pos;
  1519. +
  1520. + led_cfg = pdata->led_cfg;
  1521. + if (led_cfg) {
  1522. + if (led_cfg->open_drain)
  1523. + new_pos |= AR8327_POWER_ON_STRIP_LED_OPEN_EN;
  1524. + else
  1525. + new_pos &= ~AR8327_POWER_ON_STRIP_LED_OPEN_EN;
  1526. +
  1527. + priv->write(priv, AR8327_REG_LED_CTRL0, led_cfg->led_ctrl0);
  1528. + priv->write(priv, AR8327_REG_LED_CTRL1, led_cfg->led_ctrl1);
  1529. + priv->write(priv, AR8327_REG_LED_CTRL2, led_cfg->led_ctrl2);
  1530. + priv->write(priv, AR8327_REG_LED_CTRL3, led_cfg->led_ctrl3);
  1531. +
  1532. + if (new_pos != pos)
  1533. + new_pos |= AR8327_POWER_ON_STRIP_POWER_ON_SEL;
  1534. + }
  1535. +
  1536. + if (pdata->sgmii_cfg) {
  1537. + t = pdata->sgmii_cfg->sgmii_ctrl;
  1538. + if (priv->chip_rev == 1)
  1539. + t |= AR8327_SGMII_CTRL_EN_PLL |
  1540. + AR8327_SGMII_CTRL_EN_RX |
  1541. + AR8327_SGMII_CTRL_EN_TX;
  1542. + else
  1543. + t &= ~(AR8327_SGMII_CTRL_EN_PLL |
  1544. + AR8327_SGMII_CTRL_EN_RX |
  1545. + AR8327_SGMII_CTRL_EN_TX);
  1546. +
  1547. + priv->write(priv, AR8327_REG_SGMII_CTRL, t);
  1548. +
  1549. + if (pdata->sgmii_cfg->serdes_aen)
  1550. + new_pos &= ~AR8327_POWER_ON_STRIP_SERDES_AEN;
  1551. + else
  1552. + new_pos |= AR8327_POWER_ON_STRIP_SERDES_AEN;
  1553. + }
  1554. +
  1555. + priv->write(priv, AR8327_REG_POWER_ON_STRIP, new_pos);
  1556. +
  1557. + if (pdata->leds && pdata->num_leds) {
  1558. + int i;
  1559. +
  1560. + data->leds = kzalloc(pdata->num_leds * sizeof(void *),
  1561. + GFP_KERNEL);
  1562. + if (!data->leds)
  1563. + return -ENOMEM;
  1564. +
  1565. + for (i = 0; i < pdata->num_leds; i++)
  1566. + ar8327_led_create(priv, &pdata->leds[i]);
  1567. + }
  1568. +
  1569. + return 0;
  1570. +}
  1571. +
  1572. +#ifdef CONFIG_OF
  1573. +static int
  1574. +ar8327_hw_config_of(struct ar8xxx_priv *priv, struct device_node *np)
  1575. +{
  1576. + const __be32 *paddr;
  1577. + int len;
  1578. + int i;
  1579. +
  1580. + paddr = of_get_property(np, "qca,ar8327-initvals", &len);
  1581. + if (!paddr || len < (2 * sizeof(*paddr)))
  1582. + return -EINVAL;
  1583. +
  1584. + len /= sizeof(*paddr);
  1585. +
  1586. + for (i = 0; i < len - 1; i += 2) {
  1587. + u32 reg;
  1588. + u32 val;
  1589. +
  1590. + reg = be32_to_cpup(paddr + i);
  1591. + val = be32_to_cpup(paddr + i + 1);
  1592. +
  1593. + switch (reg) {
  1594. + case AR8327_REG_PORT_STATUS(0):
  1595. + priv->chip_data.ar8327.port0_status = val;
  1596. + break;
  1597. + case AR8327_REG_PORT_STATUS(6):
  1598. + priv->chip_data.ar8327.port6_status = val;
  1599. + break;
  1600. + default:
  1601. + priv->write(priv, reg, val);
  1602. + break;
  1603. + }
  1604. + }
  1605. +
  1606. + return 0;
  1607. +}
  1608. +#else
  1609. +static inline int
  1610. +ar8327_hw_config_of(struct ar8xxx_priv *priv, struct device_node *np)
  1611. +{
  1612. + return -EINVAL;
  1613. +}
  1614. +#endif
  1615. +
  1616. +static int
  1617. +ar8327_hw_init(struct ar8xxx_priv *priv)
  1618. +{
  1619. + struct mii_bus *bus;
  1620. + int ret;
  1621. + int i;
  1622. +
  1623. + if (priv->phy->dev.of_node)
  1624. + ret = ar8327_hw_config_of(priv, priv->phy->dev.of_node);
  1625. + else
  1626. + ret = ar8327_hw_config_pdata(priv,
  1627. + priv->phy->dev.platform_data);
  1628. +
  1629. + if (ret)
  1630. + return ret;
  1631. +
  1632. + ar8327_leds_init(priv);
  1633. +
  1634. + bus = priv->mii_bus;
  1635. + for (i = 0; i < AR8327_NUM_PHYS; i++) {
  1636. + ar8327_phy_fixup(priv, i);
  1637. +
  1638. + /* start aneg on the PHY */
  1639. + mdiobus_write(bus, i, MII_ADVERTISE, ADVERTISE_ALL |
  1640. + ADVERTISE_PAUSE_CAP |
  1641. + ADVERTISE_PAUSE_ASYM);
  1642. + mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
  1643. + mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
  1644. + }
  1645. +
  1646. + msleep(1000);
  1647. +
  1648. + return 0;
  1649. +}
  1650. +
  1651. +static void
  1652. +ar8327_cleanup(struct ar8xxx_priv *priv)
  1653. +{
  1654. + ar8327_leds_cleanup(priv);
  1655. +}
  1656. +
  1657. +static void
  1658. +ar8327_init_globals(struct ar8xxx_priv *priv)
  1659. +{
  1660. + u32 t;
  1661. +
  1662. + /* enable CPU port and disable mirror port */
  1663. + t = AR8327_FWD_CTRL0_CPU_PORT_EN |
  1664. + AR8327_FWD_CTRL0_MIRROR_PORT;
  1665. + priv->write(priv, AR8327_REG_FWD_CTRL0, t);
  1666. +
  1667. + /* forward multicast and broadcast frames to CPU */
  1668. + t = (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_UC_FLOOD_S) |
  1669. + (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_MC_FLOOD_S) |
  1670. + (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_BC_FLOOD_S);
  1671. + priv->write(priv, AR8327_REG_FWD_CTRL1, t);
  1672. +
  1673. + /* enable jumbo frames */
  1674. + ar8xxx_rmw(priv, AR8327_REG_MAX_FRAME_SIZE,
  1675. + AR8327_MAX_FRAME_SIZE_MTU, 9018 + 8 + 2);
  1676. +
  1677. + /* Enable MIB counters */
  1678. + ar8xxx_reg_set(priv, AR8327_REG_MODULE_EN,
  1679. + AR8327_MODULE_EN_MIB);
  1680. +}
  1681. +
  1682. +static void
  1683. +ar8327_init_port(struct ar8xxx_priv *priv, int port)
  1684. +{
  1685. + u32 t;
  1686. +
  1687. + if (port == AR8216_PORT_CPU)
  1688. + t = priv->chip_data.ar8327.port0_status;
  1689. + else if (port == 6)
  1690. + t = priv->chip_data.ar8327.port6_status;
  1691. + else
  1692. + t = AR8216_PORT_STATUS_LINK_AUTO;
  1693. +
  1694. + priv->write(priv, AR8327_REG_PORT_STATUS(port), t);
  1695. + priv->write(priv, AR8327_REG_PORT_HEADER(port), 0);
  1696. +
  1697. + t = 1 << AR8327_PORT_VLAN0_DEF_SVID_S;
  1698. + t |= 1 << AR8327_PORT_VLAN0_DEF_CVID_S;
  1699. + priv->write(priv, AR8327_REG_PORT_VLAN0(port), t);
  1700. +
  1701. + t = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH << AR8327_PORT_VLAN1_OUT_MODE_S;
  1702. + priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
  1703. +
  1704. + t = AR8327_PORT_LOOKUP_LEARN;
  1705. + t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
  1706. + priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
  1707. +}
  1708. +
  1709. +static u32
  1710. +ar8327_read_port_status(struct ar8xxx_priv *priv, int port)
  1711. +{
  1712. + return priv->read(priv, AR8327_REG_PORT_STATUS(port));
  1713. +}
  1714. +
  1715. +static int
  1716. +ar8327_atu_flush(struct ar8xxx_priv *priv)
  1717. +{
  1718. + int ret;
  1719. +
  1720. + ret = ar8216_wait_bit(priv, AR8327_REG_ATU_FUNC,
  1721. + AR8327_ATU_FUNC_BUSY, 0);
  1722. + if (!ret)
  1723. + priv->write(priv, AR8327_REG_ATU_FUNC,
  1724. + AR8327_ATU_FUNC_OP_FLUSH);
  1725. +
  1726. + return ret;
  1727. +}
  1728. +
  1729. +static void
  1730. +ar8327_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
  1731. +{
  1732. + if (ar8216_wait_bit(priv, AR8327_REG_VTU_FUNC1,
  1733. + AR8327_VTU_FUNC1_BUSY, 0))
  1734. + return;
  1735. +
  1736. + if ((op & AR8327_VTU_FUNC1_OP) == AR8327_VTU_FUNC1_OP_LOAD)
  1737. + priv->write(priv, AR8327_REG_VTU_FUNC0, val);
  1738. +
  1739. + op |= AR8327_VTU_FUNC1_BUSY;
  1740. + priv->write(priv, AR8327_REG_VTU_FUNC1, op);
  1741. +}
  1742. +
  1743. +static void
  1744. +ar8327_vtu_flush(struct ar8xxx_priv *priv)
  1745. +{
  1746. + ar8327_vtu_op(priv, AR8327_VTU_FUNC1_OP_FLUSH, 0);
  1747. +}
  1748. +
  1749. +static void
  1750. +ar8327_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask)
  1751. +{
  1752. + u32 op;
  1753. + u32 val;
  1754. + int i;
  1755. +
  1756. + op = AR8327_VTU_FUNC1_OP_LOAD | (vid << AR8327_VTU_FUNC1_VID_S);
  1757. + val = AR8327_VTU_FUNC0_VALID | AR8327_VTU_FUNC0_IVL;
  1758. + for (i = 0; i < AR8327_NUM_PORTS; i++) {
  1759. + u32 mode;
  1760. +
  1761. + if ((port_mask & BIT(i)) == 0)
  1762. + mode = AR8327_VTU_FUNC0_EG_MODE_NOT;
  1763. + else if (priv->vlan == 0)
  1764. + mode = AR8327_VTU_FUNC0_EG_MODE_KEEP;
  1765. + else if (priv->vlan_tagged & BIT(i))
  1766. + mode = AR8327_VTU_FUNC0_EG_MODE_TAG;
  1767. + else
  1768. + mode = AR8327_VTU_FUNC0_EG_MODE_UNTAG;
  1769. +
  1770. + val |= mode << AR8327_VTU_FUNC0_EG_MODE_S(i);
  1771. + }
  1772. + ar8327_vtu_op(priv, op, val);
  1773. +}
  1774. +
  1775. +static void
  1776. +ar8327_setup_port(struct ar8xxx_priv *priv, int port, u32 egress, u32 ingress,
  1777. + u32 members, u32 pvid)
  1778. +{
  1779. + u32 t;
  1780. + u32 mode;
  1781. +
  1782. + t = pvid << AR8327_PORT_VLAN0_DEF_SVID_S;
  1783. + t |= pvid << AR8327_PORT_VLAN0_DEF_CVID_S;
  1784. + priv->write(priv, AR8327_REG_PORT_VLAN0(port), t);
  1785. +
  1786. + mode = AR8327_PORT_VLAN1_OUT_MODE_UNMOD;
  1787. + switch (egress) {
  1788. + case AR8216_OUT_KEEP:
  1789. + mode = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH;
  1790. + break;
  1791. + case AR8216_OUT_STRIP_VLAN:
  1792. + mode = AR8327_PORT_VLAN1_OUT_MODE_UNTAG;
  1793. + break;
  1794. + case AR8216_OUT_ADD_VLAN:
  1795. + mode = AR8327_PORT_VLAN1_OUT_MODE_TAG;
  1796. + break;
  1797. + }
  1798. +
  1799. + t = AR8327_PORT_VLAN1_PORT_VLAN_PROP;
  1800. + t |= mode << AR8327_PORT_VLAN1_OUT_MODE_S;
  1801. + priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
  1802. +
  1803. + t = members;
  1804. + t |= AR8327_PORT_LOOKUP_LEARN;
  1805. + t |= ingress << AR8327_PORT_LOOKUP_IN_MODE_S;
  1806. + t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
  1807. + priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
  1808. +}
  1809. +
  1810. +static const struct ar8xxx_chip ar8327_chip = {
  1811. + .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
  1812. + .hw_init = ar8327_hw_init,
  1813. + .cleanup = ar8327_cleanup,
  1814. + .init_globals = ar8327_init_globals,
  1815. + .init_port = ar8327_init_port,
  1816. + .setup_port = ar8327_setup_port,
  1817. + .read_port_status = ar8327_read_port_status,
  1818. + .atu_flush = ar8327_atu_flush,
  1819. + .vtu_flush = ar8327_vtu_flush,
  1820. + .vtu_load_vlan = ar8327_vtu_load_vlan,
  1821. +
  1822. + .num_mibs = ARRAY_SIZE(ar8236_mibs),
  1823. + .mib_decs = ar8236_mibs,
  1824. +};
  1825. +
  1826. +static int
  1827. +ar8xxx_sw_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  1828. + struct switch_val *val)
  1829. +{
  1830. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1831. + priv->vlan = !!val->value.i;
  1832. + return 0;
  1833. +}
  1834. +
  1835. +static int
  1836. +ar8xxx_sw_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  1837. + struct switch_val *val)
  1838. +{
  1839. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1840. + val->value.i = priv->vlan;
  1841. + return 0;
  1842. +}
  1843. +
  1844. +
  1845. +static int
  1846. +ar8xxx_sw_set_pvid(struct switch_dev *dev, int port, int vlan)
  1847. +{
  1848. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1849. +
  1850. + /* make sure no invalid PVIDs get set */
  1851. +
  1852. + if (vlan >= dev->vlans)
  1853. + return -EINVAL;
  1854. +
  1855. + priv->pvid[port] = vlan;
  1856. + return 0;
  1857. +}
  1858. +
  1859. +static int
  1860. +ar8xxx_sw_get_pvid(struct switch_dev *dev, int port, int *vlan)
  1861. +{
  1862. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1863. + *vlan = priv->pvid[port];
  1864. + return 0;
  1865. +}
  1866. +
  1867. +static int
  1868. +ar8xxx_sw_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
  1869. + struct switch_val *val)
  1870. +{
  1871. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1872. + priv->vlan_id[val->port_vlan] = val->value.i;
  1873. + return 0;
  1874. +}
  1875. +
  1876. +static int
  1877. +ar8xxx_sw_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
  1878. + struct switch_val *val)
  1879. +{
  1880. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1881. + val->value.i = priv->vlan_id[val->port_vlan];
  1882. + return 0;
  1883. +}
  1884. +
  1885. +static int
  1886. +ar8xxx_sw_get_port_link(struct switch_dev *dev, int port,
  1887. + struct switch_port_link *link)
  1888. +{
  1889. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1890. +
  1891. + ar8216_read_port_link(priv, port, link);
  1892. + return 0;
  1893. +}
  1894. +
  1895. +static int
  1896. +ar8xxx_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
  1897. +{
  1898. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1899. + u8 ports = priv->vlan_table[val->port_vlan];
  1900. + int i;
  1901. +
  1902. + val->len = 0;
  1903. + for (i = 0; i < dev->ports; i++) {
  1904. + struct switch_port *p;
  1905. +
  1906. + if (!(ports & (1 << i)))
  1907. + continue;
  1908. +
  1909. + p = &val->value.ports[val->len++];
  1910. + p->id = i;
  1911. + if (priv->vlan_tagged & (1 << i))
  1912. + p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
  1913. + else
  1914. + p->flags = 0;
  1915. + }
  1916. + return 0;
  1917. +}
  1918. +
  1919. +static int
  1920. +ar8xxx_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
  1921. +{
  1922. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  1923. + u8 *vt = &priv->vlan_table[val->port_vlan];
  1924. + int i, j;
  1925. +
  1926. + *vt = 0;
  1927. + for (i = 0; i < val->len; i++) {
  1928. + struct switch_port *p = &val->value.ports[i];
  1929. +
  1930. + if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
  1931. + priv->vlan_tagged |= (1 << p->id);
  1932. + } else {
  1933. + priv->vlan_tagged &= ~(1 << p->id);
  1934. + priv->pvid[p->id] = val->port_vlan;
  1935. +
  1936. + /* make sure that an untagged port does not
  1937. + * appear in other vlans */
  1938. + for (j = 0; j < AR8X16_MAX_VLANS; j++) {
  1939. + if (j == val->port_vlan)
  1940. + continue;
  1941. + priv->vlan_table[j] &= ~(1 << p->id);
  1942. + }
  1943. + }
  1944. +
  1945. + *vt |= 1 << p->id;
  1946. + }
  1947. + return 0;
  1948. +}
  1949. +
  1950. +static void
  1951. +ar8327_set_mirror_regs(struct ar8xxx_priv *priv)
  1952. +{
  1953. + int port;
  1954. +
  1955. + /* reset all mirror registers */
  1956. + ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
  1957. + AR8327_FWD_CTRL0_MIRROR_PORT,
  1958. + (0xF << AR8327_FWD_CTRL0_MIRROR_PORT_S));
  1959. + for (port = 0; port < AR8327_NUM_PORTS; port++) {
  1960. + ar8xxx_rmw(priv, AR8327_REG_PORT_LOOKUP(port),
  1961. + AR8327_PORT_LOOKUP_ING_MIRROR_EN,
  1962. + 0);
  1963. +
  1964. + ar8xxx_rmw(priv, AR8327_REG_PORT_HOL_CTRL1(port),
  1965. + AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN,
  1966. + 0);
  1967. + }
  1968. +
  1969. + /* now enable mirroring if necessary */
  1970. + if (priv->source_port >= AR8327_NUM_PORTS ||
  1971. + priv->monitor_port >= AR8327_NUM_PORTS ||
  1972. + priv->source_port == priv->monitor_port) {
  1973. + return;
  1974. + }
  1975. +
  1976. + ar8xxx_rmw(priv, AR8327_REG_FWD_CTRL0,
  1977. + AR8327_FWD_CTRL0_MIRROR_PORT,
  1978. + (priv->monitor_port << AR8327_FWD_CTRL0_MIRROR_PORT_S));
  1979. +
  1980. + if (priv->mirror_rx)
  1981. + ar8xxx_rmw(priv, AR8327_REG_PORT_LOOKUP(priv->source_port),
  1982. + AR8327_PORT_LOOKUP_ING_MIRROR_EN,
  1983. + AR8327_PORT_LOOKUP_ING_MIRROR_EN);
  1984. +
  1985. + if (priv->mirror_tx)
  1986. + ar8xxx_rmw(priv, AR8327_REG_PORT_HOL_CTRL1(priv->source_port),
  1987. + AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN,
  1988. + AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN);
  1989. +}
  1990. +
  1991. +static void
  1992. +ar8216_set_mirror_regs(struct ar8xxx_priv *priv)
  1993. +{
  1994. + int port;
  1995. +
  1996. + /* reset all mirror registers */
  1997. + ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CPUPORT,
  1998. + AR8216_GLOBAL_CPUPORT_MIRROR_PORT,
  1999. + (0xF << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
  2000. + for (port = 0; port < AR8216_NUM_PORTS; port++) {
  2001. + ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  2002. + AR8216_PORT_CTRL_MIRROR_RX,
  2003. + 0);
  2004. +
  2005. + ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
  2006. + AR8216_PORT_CTRL_MIRROR_TX,
  2007. + 0);
  2008. + }
  2009. +
  2010. + /* now enable mirroring if necessary */
  2011. + if (priv->source_port >= AR8216_NUM_PORTS ||
  2012. + priv->monitor_port >= AR8216_NUM_PORTS ||
  2013. + priv->source_port == priv->monitor_port) {
  2014. + return;
  2015. + }
  2016. +
  2017. + ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CPUPORT,
  2018. + AR8216_GLOBAL_CPUPORT_MIRROR_PORT,
  2019. + (priv->monitor_port << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
  2020. +
  2021. + if (priv->mirror_rx)
  2022. + ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(priv->source_port),
  2023. + AR8216_PORT_CTRL_MIRROR_RX,
  2024. + AR8216_PORT_CTRL_MIRROR_RX);
  2025. +
  2026. + if (priv->mirror_tx)
  2027. + ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(priv->source_port),
  2028. + AR8216_PORT_CTRL_MIRROR_TX,
  2029. + AR8216_PORT_CTRL_MIRROR_TX);
  2030. +}
  2031. +
  2032. +static void
  2033. +ar8xxx_set_mirror_regs(struct ar8xxx_priv *priv)
  2034. +{
  2035. + if (chip_is_ar8327(priv) || chip_is_ar8337(priv)) {
  2036. + ar8327_set_mirror_regs(priv);
  2037. + } else {
  2038. + ar8216_set_mirror_regs(priv);
  2039. + }
  2040. +}
  2041. +
  2042. +static int
  2043. +ar8xxx_sw_hw_apply(struct switch_dev *dev)
  2044. +{
  2045. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  2046. + u8 portmask[AR8X16_MAX_PORTS];
  2047. + int i, j;
  2048. +
  2049. + mutex_lock(&priv->reg_mutex);
  2050. + /* flush all vlan translation unit entries */
  2051. + priv->chip->vtu_flush(priv);
  2052. +
  2053. + memset(portmask, 0, sizeof(portmask));
  2054. + if (!priv->init) {
  2055. + /* calculate the port destination masks and load vlans
  2056. + * into the vlan translation unit */
  2057. + for (j = 0; j < AR8X16_MAX_VLANS; j++) {
  2058. + u8 vp = priv->vlan_table[j];
  2059. +
  2060. + if (!vp)
  2061. + continue;
  2062. +
  2063. + for (i = 0; i < dev->ports; i++) {
  2064. + u8 mask = (1 << i);
  2065. + if (vp & mask)
  2066. + portmask[i] |= vp & ~mask;
  2067. + }
  2068. +
  2069. + priv->chip->vtu_load_vlan(priv, priv->vlan_id[j],
  2070. + priv->vlan_table[j]);
  2071. + }
  2072. + } else {
  2073. + /* vlan disabled:
  2074. + * isolate all ports, but connect them to the cpu port */
  2075. + for (i = 0; i < dev->ports; i++) {
  2076. + if (i == AR8216_PORT_CPU)
  2077. + continue;
  2078. +
  2079. + portmask[i] = 1 << AR8216_PORT_CPU;
  2080. + portmask[AR8216_PORT_CPU] |= (1 << i);
  2081. + }
  2082. + }
  2083. +
  2084. + /* update the port destination mask registers and tag settings */
  2085. + for (i = 0; i < dev->ports; i++) {
  2086. + int egress, ingress;
  2087. + int pvid;
  2088. +
  2089. + if (priv->vlan) {
  2090. + pvid = priv->vlan_id[priv->pvid[i]];
  2091. + if (priv->vlan_tagged & (1 << i))
  2092. + egress = AR8216_OUT_ADD_VLAN;
  2093. + else
  2094. + egress = AR8216_OUT_STRIP_VLAN;
  2095. + ingress = AR8216_IN_SECURE;
  2096. + } else {
  2097. + pvid = i;
  2098. + egress = AR8216_OUT_KEEP;
  2099. + ingress = AR8216_IN_PORT_ONLY;
  2100. + }
  2101. +
  2102. + priv->chip->setup_port(priv, i, egress, ingress, portmask[i],
  2103. + pvid);
  2104. + }
  2105. +
  2106. + ar8xxx_set_mirror_regs(priv);
  2107. +
  2108. + mutex_unlock(&priv->reg_mutex);
  2109. + return 0;
  2110. +}
  2111. +
  2112. +static int
  2113. +ar8xxx_sw_reset_switch(struct switch_dev *dev)
  2114. +{
  2115. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  2116. + int i;
  2117. +
  2118. + mutex_lock(&priv->reg_mutex);
  2119. + memset(&priv->vlan, 0, sizeof(struct ar8xxx_priv) -
  2120. + offsetof(struct ar8xxx_priv, vlan));
  2121. +
  2122. + for (i = 0; i < AR8X16_MAX_VLANS; i++)
  2123. + priv->vlan_id[i] = i;
  2124. +
  2125. + /* Configure all ports */
  2126. + for (i = 0; i < dev->ports; i++)
  2127. + priv->chip->init_port(priv, i);
  2128. +
  2129. + priv->mirror_rx = false;
  2130. + priv->mirror_tx = false;
  2131. + priv->source_port = 0;
  2132. + priv->monitor_port = 0;
  2133. +
  2134. + priv->chip->init_globals(priv);
  2135. +
  2136. + mutex_unlock(&priv->reg_mutex);
  2137. +
  2138. + return ar8xxx_sw_hw_apply(dev);
  2139. +}
  2140. +
  2141. +static int
  2142. +ar8xxx_sw_set_reset_mibs(struct switch_dev *dev,
  2143. + const struct switch_attr *attr,
  2144. + struct switch_val *val)
  2145. +{
  2146. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  2147. + unsigned int len;
  2148. + int ret;
  2149. +
  2150. + if (!ar8xxx_has_mib_counters(priv))
  2151. + return -EOPNOTSUPP;
  2152. +
  2153. + mutex_lock(&priv->mib_lock);
  2154. +
  2155. + len = priv->dev.ports * priv->chip->num_mibs *
  2156. + sizeof(*priv->mib_stats);
  2157. + memset(priv->mib_stats, '\0', len);
  2158. + ret = ar8xxx_mib_flush(priv);
  2159. + if (ret)
  2160. + goto unlock;
  2161. +
  2162. + ret = 0;
  2163. +
  2164. +unlock:
  2165. + mutex_unlock(&priv->mib_lock);
  2166. + return ret;
  2167. +}
  2168. +
  2169. +static int
  2170. +ar8xxx_sw_set_mirror_rx_enable(struct switch_dev *dev,
  2171. + const struct switch_attr *attr,
  2172. + struct switch_val *val)
  2173. +{
  2174. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  2175. +
  2176. + mutex_lock(&priv->reg_mutex);
  2177. + priv->mirror_rx = !!val->value.i;
  2178. + ar8xxx_set_mirror_regs(priv);
  2179. + mutex_unlock(&priv->reg_mutex);
  2180. +
  2181. + return 0;
  2182. +}
  2183. +
  2184. +static int
  2185. +ar8xxx_sw_get_mirror_rx_enable(struct switch_dev *dev,
  2186. + const struct switch_attr *attr,
  2187. + struct switch_val *val)
  2188. +{
  2189. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  2190. + val->value.i = priv->mirror_rx;
  2191. + return 0;
  2192. +}
  2193. +
  2194. +static int
  2195. +ar8xxx_sw_set_mirror_tx_enable(struct switch_dev *dev,
  2196. + const struct switch_attr *attr,
  2197. + struct switch_val *val)
  2198. +{
  2199. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  2200. +
  2201. + mutex_lock(&priv->reg_mutex);
  2202. + priv->mirror_tx = !!val->value.i;
  2203. + ar8xxx_set_mirror_regs(priv);
  2204. + mutex_unlock(&priv->reg_mutex);
  2205. +
  2206. + return 0;
  2207. +}
  2208. +
  2209. +static int
  2210. +ar8xxx_sw_get_mirror_tx_enable(struct switch_dev *dev,
  2211. + const struct switch_attr *attr,
  2212. + struct switch_val *val)
  2213. +{
  2214. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  2215. + val->value.i = priv->mirror_tx;
  2216. + return 0;
  2217. +}
  2218. +
  2219. +static int
  2220. +ar8xxx_sw_set_mirror_monitor_port(struct switch_dev *dev,
  2221. + const struct switch_attr *attr,
  2222. + struct switch_val *val)
  2223. +{
  2224. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  2225. +
  2226. + mutex_lock(&priv->reg_mutex);
  2227. + priv->monitor_port = val->value.i;
  2228. + ar8xxx_set_mirror_regs(priv);
  2229. + mutex_unlock(&priv->reg_mutex);
  2230. +
  2231. + return 0;
  2232. +}
  2233. +
  2234. +static int
  2235. +ar8xxx_sw_get_mirror_monitor_port(struct switch_dev *dev,
  2236. + const struct switch_attr *attr,
  2237. + struct switch_val *val)
  2238. +{
  2239. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  2240. + val->value.i = priv->monitor_port;
  2241. + return 0;
  2242. +}
  2243. +
  2244. +static int
  2245. +ar8xxx_sw_set_mirror_source_port(struct switch_dev *dev,
  2246. + const struct switch_attr *attr,
  2247. + struct switch_val *val)
  2248. +{
  2249. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  2250. +
  2251. + mutex_lock(&priv->reg_mutex);
  2252. + priv->source_port = val->value.i;
  2253. + ar8xxx_set_mirror_regs(priv);
  2254. + mutex_unlock(&priv->reg_mutex);
  2255. +
  2256. + return 0;
  2257. +}
  2258. +
  2259. +static int
  2260. +ar8xxx_sw_get_mirror_source_port(struct switch_dev *dev,
  2261. + const struct switch_attr *attr,
  2262. + struct switch_val *val)
  2263. +{
  2264. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  2265. + val->value.i = priv->source_port;
  2266. + return 0;
  2267. +}
  2268. +
  2269. +static int
  2270. +ar8xxx_sw_set_port_reset_mib(struct switch_dev *dev,
  2271. + const struct switch_attr *attr,
  2272. + struct switch_val *val)
  2273. +{
  2274. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  2275. + int port;
  2276. + int ret;
  2277. +
  2278. + if (!ar8xxx_has_mib_counters(priv))
  2279. + return -EOPNOTSUPP;
  2280. +
  2281. + port = val->port_vlan;
  2282. + if (port >= dev->ports)
  2283. + return -EINVAL;
  2284. +
  2285. + mutex_lock(&priv->mib_lock);
  2286. + ret = ar8xxx_mib_capture(priv);
  2287. + if (ret)
  2288. + goto unlock;
  2289. +
  2290. + ar8xxx_mib_fetch_port_stat(priv, port, true);
  2291. +
  2292. + ret = 0;
  2293. +
  2294. +unlock:
  2295. + mutex_unlock(&priv->mib_lock);
  2296. + return ret;
  2297. +}
  2298. +
  2299. +static int
  2300. +ar8xxx_sw_get_port_mib(struct switch_dev *dev,
  2301. + const struct switch_attr *attr,
  2302. + struct switch_val *val)
  2303. +{
  2304. + struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
  2305. + const struct ar8xxx_chip *chip = priv->chip;
  2306. + u64 *mib_stats;
  2307. + int port;
  2308. + int ret;
  2309. + char *buf = priv->buf;
  2310. + int i, len = 0;
  2311. +
  2312. + if (!ar8xxx_has_mib_counters(priv))
  2313. + return -EOPNOTSUPP;
  2314. +
  2315. + port = val->port_vlan;
  2316. + if (port >= dev->ports)
  2317. + return -EINVAL;
  2318. +
  2319. + mutex_lock(&priv->mib_lock);
  2320. + ret = ar8xxx_mib_capture(priv);
  2321. + if (ret)
  2322. + goto unlock;
  2323. +
  2324. + ar8xxx_mib_fetch_port_stat(priv, port, false);
  2325. +
  2326. + len += snprintf(buf + len, sizeof(priv->buf) - len,
  2327. + "Port %d MIB counters\n",
  2328. + port);
  2329. +
  2330. + mib_stats = &priv->mib_stats[port * chip->num_mibs];
  2331. + for (i = 0; i < chip->num_mibs; i++)
  2332. + len += snprintf(buf + len, sizeof(priv->buf) - len,
  2333. + "%-12s: %llu\n",
  2334. + chip->mib_decs[i].name,
  2335. + mib_stats[i]);
  2336. +
  2337. + val->value.s = buf;
  2338. + val->len = len;
  2339. +
  2340. + ret = 0;
  2341. +
  2342. +unlock:
  2343. + mutex_unlock(&priv->mib_lock);
  2344. + return ret;
  2345. +}
  2346. +
  2347. +static struct switch_attr ar8xxx_sw_attr_globals[] = {
  2348. + {
  2349. + .type = SWITCH_TYPE_INT,
  2350. + .name = "enable_vlan",
  2351. + .description = "Enable VLAN mode",
  2352. + .set = ar8xxx_sw_set_vlan,
  2353. + .get = ar8xxx_sw_get_vlan,
  2354. + .max = 1
  2355. + },
  2356. + {
  2357. + .type = SWITCH_TYPE_NOVAL,
  2358. + .name = "reset_mibs",
  2359. + .description = "Reset all MIB counters",
  2360. + .set = ar8xxx_sw_set_reset_mibs,
  2361. + },
  2362. + {
  2363. + .type = SWITCH_TYPE_INT,
  2364. + .name = "enable_mirror_rx",
  2365. + .description = "Enable mirroring of RX packets",
  2366. + .set = ar8xxx_sw_set_mirror_rx_enable,
  2367. + .get = ar8xxx_sw_get_mirror_rx_enable,
  2368. + .max = 1
  2369. + },
  2370. + {
  2371. + .type = SWITCH_TYPE_INT,
  2372. + .name = "enable_mirror_tx",
  2373. + .description = "Enable mirroring of TX packets",
  2374. + .set = ar8xxx_sw_set_mirror_tx_enable,
  2375. + .get = ar8xxx_sw_get_mirror_tx_enable,
  2376. + .max = 1
  2377. + },
  2378. + {
  2379. + .type = SWITCH_TYPE_INT,
  2380. + .name = "mirror_monitor_port",
  2381. + .description = "Mirror monitor port",
  2382. + .set = ar8xxx_sw_set_mirror_monitor_port,
  2383. + .get = ar8xxx_sw_get_mirror_monitor_port,
  2384. + .max = AR8216_NUM_PORTS - 1
  2385. + },
  2386. + {
  2387. + .type = SWITCH_TYPE_INT,
  2388. + .name = "mirror_source_port",
  2389. + .description = "Mirror source port",
  2390. + .set = ar8xxx_sw_set_mirror_source_port,
  2391. + .get = ar8xxx_sw_get_mirror_source_port,
  2392. + .max = AR8216_NUM_PORTS - 1
  2393. + },
  2394. +};
  2395. +
  2396. +static struct switch_attr ar8327_sw_attr_globals[] = {
  2397. + {
  2398. + .type = SWITCH_TYPE_INT,
  2399. + .name = "enable_vlan",
  2400. + .description = "Enable VLAN mode",
  2401. + .set = ar8xxx_sw_set_vlan,
  2402. + .get = ar8xxx_sw_get_vlan,
  2403. + .max = 1
  2404. + },
  2405. + {
  2406. + .type = SWITCH_TYPE_NOVAL,
  2407. + .name = "reset_mibs",
  2408. + .description = "Reset all MIB counters",
  2409. + .set = ar8xxx_sw_set_reset_mibs,
  2410. + },
  2411. + {
  2412. + .type = SWITCH_TYPE_INT,
  2413. + .name = "enable_mirror_rx",
  2414. + .description = "Enable mirroring of RX packets",
  2415. + .set = ar8xxx_sw_set_mirror_rx_enable,
  2416. + .get = ar8xxx_sw_get_mirror_rx_enable,
  2417. + .max = 1
  2418. + },
  2419. + {
  2420. + .type = SWITCH_TYPE_INT,
  2421. + .name = "enable_mirror_tx",
  2422. + .description = "Enable mirroring of TX packets",
  2423. + .set = ar8xxx_sw_set_mirror_tx_enable,
  2424. + .get = ar8xxx_sw_get_mirror_tx_enable,
  2425. + .max = 1
  2426. + },
  2427. + {
  2428. + .type = SWITCH_TYPE_INT,
  2429. + .name = "mirror_monitor_port",
  2430. + .description = "Mirror monitor port",
  2431. + .set = ar8xxx_sw_set_mirror_monitor_port,
  2432. + .get = ar8xxx_sw_get_mirror_monitor_port,
  2433. + .max = AR8327_NUM_PORTS - 1
  2434. + },
  2435. + {
  2436. + .type = SWITCH_TYPE_INT,
  2437. + .name = "mirror_source_port",
  2438. + .description = "Mirror source port",
  2439. + .set = ar8xxx_sw_set_mirror_source_port,
  2440. + .get = ar8xxx_sw_get_mirror_source_port,
  2441. + .max = AR8327_NUM_PORTS - 1
  2442. + },
  2443. +};
  2444. +
  2445. +static struct switch_attr ar8xxx_sw_attr_port[] = {
  2446. + {
  2447. + .type = SWITCH_TYPE_NOVAL,
  2448. + .name = "reset_mib",
  2449. + .description = "Reset single port MIB counters",
  2450. + .set = ar8xxx_sw_set_port_reset_mib,
  2451. + },
  2452. + {
  2453. + .type = SWITCH_TYPE_STRING,
  2454. + .name = "mib",
  2455. + .description = "Get port's MIB counters",
  2456. + .set = NULL,
  2457. + .get = ar8xxx_sw_get_port_mib,
  2458. + },
  2459. +};
  2460. +
  2461. +static struct switch_attr ar8xxx_sw_attr_vlan[] = {
  2462. + {
  2463. + .type = SWITCH_TYPE_INT,
  2464. + .name = "vid",
  2465. + .description = "VLAN ID (0-4094)",
  2466. + .set = ar8xxx_sw_set_vid,
  2467. + .get = ar8xxx_sw_get_vid,
  2468. + .max = 4094,
  2469. + },
  2470. +};
  2471. +
  2472. +static const struct switch_dev_ops ar8xxx_sw_ops = {
  2473. + .attr_global = {
  2474. + .attr = ar8xxx_sw_attr_globals,
  2475. + .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_globals),
  2476. + },
  2477. + .attr_port = {
  2478. + .attr = ar8xxx_sw_attr_port,
  2479. + .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_port),
  2480. + },
  2481. + .attr_vlan = {
  2482. + .attr = ar8xxx_sw_attr_vlan,
  2483. + .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
  2484. + },
  2485. + .get_port_pvid = ar8xxx_sw_get_pvid,
  2486. + .set_port_pvid = ar8xxx_sw_set_pvid,
  2487. + .get_vlan_ports = ar8xxx_sw_get_ports,
  2488. + .set_vlan_ports = ar8xxx_sw_set_ports,
  2489. + .apply_config = ar8xxx_sw_hw_apply,
  2490. + .reset_switch = ar8xxx_sw_reset_switch,
  2491. + .get_port_link = ar8xxx_sw_get_port_link,
  2492. +};
  2493. +
  2494. +static const struct switch_dev_ops ar8327_sw_ops = {
  2495. + .attr_global = {
  2496. + .attr = ar8327_sw_attr_globals,
  2497. + .n_attr = ARRAY_SIZE(ar8327_sw_attr_globals),
  2498. + },
  2499. + .attr_port = {
  2500. + .attr = ar8xxx_sw_attr_port,
  2501. + .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_port),
  2502. + },
  2503. + .attr_vlan = {
  2504. + .attr = ar8xxx_sw_attr_vlan,
  2505. + .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
  2506. + },
  2507. + .get_port_pvid = ar8xxx_sw_get_pvid,
  2508. + .set_port_pvid = ar8xxx_sw_set_pvid,
  2509. + .get_vlan_ports = ar8xxx_sw_get_ports,
  2510. + .set_vlan_ports = ar8xxx_sw_set_ports,
  2511. + .apply_config = ar8xxx_sw_hw_apply,
  2512. + .reset_switch = ar8xxx_sw_reset_switch,
  2513. + .get_port_link = ar8xxx_sw_get_port_link,
  2514. +};
  2515. +
  2516. +static int
  2517. +ar8xxx_id_chip(struct ar8xxx_priv *priv)
  2518. +{
  2519. + u32 val;
  2520. + u16 id;
  2521. + int i;
  2522. +
  2523. + val = priv->read(priv, AR8216_REG_CTRL);
  2524. + if (val == ~0)
  2525. + return -ENODEV;
  2526. +
  2527. + id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  2528. + for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
  2529. + u16 t;
  2530. +
  2531. + val = priv->read(priv, AR8216_REG_CTRL);
  2532. + if (val == ~0)
  2533. + return -ENODEV;
  2534. +
  2535. + t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  2536. + if (t != id)
  2537. + return -ENODEV;
  2538. + }
  2539. +
  2540. + priv->chip_ver = (id & AR8216_CTRL_VERSION) >> AR8216_CTRL_VERSION_S;
  2541. + priv->chip_rev = (id & AR8216_CTRL_REVISION);
  2542. +
  2543. + switch (priv->chip_ver) {
  2544. + case AR8XXX_VER_AR8216:
  2545. + priv->chip = &ar8216_chip;
  2546. + break;
  2547. + case AR8XXX_VER_AR8236:
  2548. + priv->chip = &ar8236_chip;
  2549. + break;
  2550. + case AR8XXX_VER_AR8316:
  2551. + priv->chip = &ar8316_chip;
  2552. + break;
  2553. + case AR8XXX_VER_AR8327:
  2554. + priv->mii_lo_first = true;
  2555. + priv->chip = &ar8327_chip;
  2556. + break;
  2557. + case AR8XXX_VER_AR8337:
  2558. + priv->mii_lo_first = true;
  2559. + priv->chip = &ar8327_chip;
  2560. + break;
  2561. + default:
  2562. + pr_err("ar8216: Unknown Atheros device [ver=%d, rev=%d]\n",
  2563. + priv->chip_ver, priv->chip_rev);
  2564. +
  2565. + return -ENODEV;
  2566. + }
  2567. +
  2568. + return 0;
  2569. +}
  2570. +
  2571. +static void
  2572. +ar8xxx_mib_work_func(struct work_struct *work)
  2573. +{
  2574. + struct ar8xxx_priv *priv;
  2575. + int err;
  2576. +
  2577. + priv = container_of(work, struct ar8xxx_priv, mib_work.work);
  2578. +
  2579. + mutex_lock(&priv->mib_lock);
  2580. +
  2581. + err = ar8xxx_mib_capture(priv);
  2582. + if (err)
  2583. + goto next_port;
  2584. +
  2585. + ar8xxx_mib_fetch_port_stat(priv, priv->mib_next_port, false);
  2586. +
  2587. +next_port:
  2588. + priv->mib_next_port++;
  2589. + if (priv->mib_next_port >= priv->dev.ports)
  2590. + priv->mib_next_port = 0;
  2591. +
  2592. + mutex_unlock(&priv->mib_lock);
  2593. + schedule_delayed_work(&priv->mib_work,
  2594. + msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
  2595. +}
  2596. +
  2597. +static int
  2598. +ar8xxx_mib_init(struct ar8xxx_priv *priv)
  2599. +{
  2600. + unsigned int len;
  2601. +
  2602. + if (!ar8xxx_has_mib_counters(priv))
  2603. + return 0;
  2604. +
  2605. + BUG_ON(!priv->chip->mib_decs || !priv->chip->num_mibs);
  2606. +
  2607. + len = priv->dev.ports * priv->chip->num_mibs *
  2608. + sizeof(*priv->mib_stats);
  2609. + priv->mib_stats = kzalloc(len, GFP_KERNEL);
  2610. +
  2611. + if (!priv->mib_stats)
  2612. + return -ENOMEM;
  2613. +
  2614. + return 0;
  2615. +}
  2616. +
  2617. +static void
  2618. +ar8xxx_mib_start(struct ar8xxx_priv *priv)
  2619. +{
  2620. + if (!ar8xxx_has_mib_counters(priv))
  2621. + return;
  2622. +
  2623. + schedule_delayed_work(&priv->mib_work,
  2624. + msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
  2625. +}
  2626. +
  2627. +static void
  2628. +ar8xxx_mib_stop(struct ar8xxx_priv *priv)
  2629. +{
  2630. + if (!ar8xxx_has_mib_counters(priv))
  2631. + return;
  2632. +
  2633. + cancel_delayed_work(&priv->mib_work);
  2634. +}
  2635. +
  2636. +static struct ar8xxx_priv *
  2637. +ar8xxx_create(void)
  2638. +{
  2639. + struct ar8xxx_priv *priv;
  2640. +
  2641. + priv = kzalloc(sizeof(struct ar8xxx_priv), GFP_KERNEL);
  2642. + if (priv == NULL)
  2643. + return NULL;
  2644. +
  2645. + mutex_init(&priv->reg_mutex);
  2646. + mutex_init(&priv->mib_lock);
  2647. + INIT_DELAYED_WORK(&priv->mib_work, ar8xxx_mib_work_func);
  2648. +
  2649. + return priv;
  2650. +}
  2651. +
  2652. +static void
  2653. +ar8xxx_free(struct ar8xxx_priv *priv)
  2654. +{
  2655. + if (priv->chip && priv->chip->cleanup)
  2656. + priv->chip->cleanup(priv);
  2657. +
  2658. + kfree(priv->mib_stats);
  2659. + kfree(priv);
  2660. +}
  2661. +
  2662. +static struct ar8xxx_priv *
  2663. +ar8xxx_create_mii(struct mii_bus *bus)
  2664. +{
  2665. + struct ar8xxx_priv *priv;
  2666. +
  2667. + priv = ar8xxx_create();
  2668. + if (priv) {
  2669. + priv->mii_bus = bus;
  2670. + priv->read = ar8xxx_mii_read;
  2671. + priv->write = ar8xxx_mii_write;
  2672. + priv->rmw = ar8xxx_mii_rmw;
  2673. + }
  2674. +
  2675. + return priv;
  2676. +}
  2677. +
  2678. +static int
  2679. +ar8xxx_probe_switch(struct ar8xxx_priv *priv)
  2680. +{
  2681. + struct switch_dev *swdev;
  2682. + int ret;
  2683. +
  2684. + ret = ar8xxx_id_chip(priv);
  2685. + if (ret)
  2686. + return ret;
  2687. +
  2688. + swdev = &priv->dev;
  2689. + swdev->cpu_port = AR8216_PORT_CPU;
  2690. + swdev->ops = &ar8xxx_sw_ops;
  2691. +
  2692. + if (chip_is_ar8316(priv)) {
  2693. + swdev->name = "Atheros AR8316";
  2694. + swdev->vlans = AR8X16_MAX_VLANS;
  2695. + swdev->ports = AR8216_NUM_PORTS;
  2696. + } else if (chip_is_ar8236(priv)) {
  2697. + swdev->name = "Atheros AR8236";
  2698. + swdev->vlans = AR8216_NUM_VLANS;
  2699. + swdev->ports = AR8216_NUM_PORTS;
  2700. + } else if (chip_is_ar8327(priv)) {
  2701. + swdev->name = "Atheros AR8327";
  2702. + swdev->vlans = AR8X16_MAX_VLANS;
  2703. + swdev->ports = AR8327_NUM_PORTS;
  2704. + swdev->ops = &ar8327_sw_ops;
  2705. + } else if (chip_is_ar8337(priv)) {
  2706. + swdev->name = "Atheros AR8337";
  2707. + swdev->vlans = AR8X16_MAX_VLANS;
  2708. + swdev->ports = AR8327_NUM_PORTS;
  2709. + swdev->ops = &ar8327_sw_ops;
  2710. + } else {
  2711. + swdev->name = "Atheros AR8216";
  2712. + swdev->vlans = AR8216_NUM_VLANS;
  2713. + swdev->ports = AR8216_NUM_PORTS;
  2714. + }
  2715. +
  2716. + ret = ar8xxx_mib_init(priv);
  2717. + if (ret)
  2718. + return ret;
  2719. +
  2720. + return 0;
  2721. +}
  2722. +
  2723. +static int
  2724. +ar8xxx_start(struct ar8xxx_priv *priv)
  2725. +{
  2726. + int ret;
  2727. +
  2728. + priv->init = true;
  2729. +
  2730. + ret = priv->chip->hw_init(priv);
  2731. + if (ret)
  2732. + return ret;
  2733. +
  2734. + ret = ar8xxx_sw_reset_switch(&priv->dev);
  2735. + if (ret)
  2736. + return ret;
  2737. +
  2738. + priv->init = false;
  2739. +
  2740. + ar8xxx_mib_start(priv);
  2741. +
  2742. + return 0;
  2743. +}
  2744. +
  2745. +static int
  2746. +ar8xxx_phy_config_init(struct phy_device *phydev)
  2747. +{
  2748. + struct ar8xxx_priv *priv = phydev->priv;
  2749. + struct net_device *dev = phydev->attached_dev;
  2750. + int ret;
  2751. +
  2752. + if (WARN_ON(!priv))
  2753. + return -ENODEV;
  2754. +
  2755. + if (chip_is_ar8327(priv) || chip_is_ar8337(priv))
  2756. + return 0;
  2757. +
  2758. + priv->phy = phydev;
  2759. +
  2760. + if (phydev->addr != 0) {
  2761. + if (chip_is_ar8316(priv)) {
  2762. + /* switch device has been initialized, reinit */
  2763. + priv->dev.ports = (AR8216_NUM_PORTS - 1);
  2764. + priv->initialized = false;
  2765. + priv->port4_phy = true;
  2766. + ar8316_hw_init(priv);
  2767. + return 0;
  2768. + }
  2769. +
  2770. + return 0;
  2771. + }
  2772. +
  2773. + ret = ar8xxx_start(priv);
  2774. + if (ret)
  2775. + return ret;
  2776. +
  2777. + /* VID fixup only needed on ar8216 */
  2778. + if (chip_is_ar8216(priv)) {
  2779. + dev->phy_ptr = priv;
  2780. + dev->priv_flags |= IFF_NO_IP_ALIGN;
  2781. + dev->eth_mangle_rx = ar8216_mangle_rx;
  2782. + dev->eth_mangle_tx = ar8216_mangle_tx;
  2783. + }
  2784. +
  2785. + return 0;
  2786. +}
  2787. +
  2788. +static int
  2789. +ar8xxx_phy_read_status(struct phy_device *phydev)
  2790. +{
  2791. + struct ar8xxx_priv *priv = phydev->priv;
  2792. + struct switch_port_link link;
  2793. + int ret;
  2794. +
  2795. + if (phydev->addr != 0)
  2796. + return genphy_read_status(phydev);
  2797. +
  2798. + ar8216_read_port_link(priv, phydev->addr, &link);
  2799. + phydev->link = !!link.link;
  2800. + if (!phydev->link)
  2801. + return 0;
  2802. +
  2803. + switch (link.speed) {
  2804. + case SWITCH_PORT_SPEED_10:
  2805. + phydev->speed = SPEED_10;
  2806. + break;
  2807. + case SWITCH_PORT_SPEED_100:
  2808. + phydev->speed = SPEED_100;
  2809. + break;
  2810. + case SWITCH_PORT_SPEED_1000:
  2811. + phydev->speed = SPEED_1000;
  2812. + break;
  2813. + default:
  2814. + phydev->speed = 0;
  2815. + }
  2816. + phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
  2817. +
  2818. + /* flush the address translation unit */
  2819. + mutex_lock(&priv->reg_mutex);
  2820. + ret = priv->chip->atu_flush(priv);
  2821. + mutex_unlock(&priv->reg_mutex);
  2822. +
  2823. + phydev->state = PHY_RUNNING;
  2824. + netif_carrier_on(phydev->attached_dev);
  2825. + phydev->adjust_link(phydev->attached_dev);
  2826. +
  2827. + return ret;
  2828. +}
  2829. +
  2830. +static int
  2831. +ar8xxx_phy_config_aneg(struct phy_device *phydev)
  2832. +{
  2833. + if (phydev->addr == 0)
  2834. + return 0;
  2835. +
  2836. + return genphy_config_aneg(phydev);
  2837. +}
  2838. +
  2839. +static const u32 ar8xxx_phy_ids[] = {
  2840. + 0x004dd033,
  2841. + 0x004dd034, /* AR8327 */
  2842. + 0x004dd036, /* AR8337 */
  2843. + 0x004dd041,
  2844. + 0x004dd042,
  2845. +};
  2846. +
  2847. +static bool
  2848. +ar8xxx_phy_match(u32 phy_id)
  2849. +{
  2850. + int i;
  2851. +
  2852. + for (i = 0; i < ARRAY_SIZE(ar8xxx_phy_ids); i++)
  2853. + if (phy_id == ar8xxx_phy_ids[i])
  2854. + return true;
  2855. +
  2856. + return false;
  2857. +}
  2858. +
  2859. +static bool
  2860. +ar8xxx_is_possible(struct mii_bus *bus)
  2861. +{
  2862. + unsigned i;
  2863. +
  2864. + for (i = 0; i < 4; i++) {
  2865. + u32 phy_id;
  2866. +
  2867. + phy_id = mdiobus_read(bus, i, MII_PHYSID1) << 16;
  2868. + phy_id |= mdiobus_read(bus, i, MII_PHYSID2);
  2869. + if (!ar8xxx_phy_match(phy_id)) {
  2870. + pr_debug("ar8xxx: unknown PHY at %s:%02x id:%08x\n",
  2871. + dev_name(&bus->dev), i, phy_id);
  2872. + return false;
  2873. + }
  2874. + }
  2875. +
  2876. + return true;
  2877. +}
  2878. +
  2879. +static int
  2880. +ar8xxx_phy_probe(struct phy_device *phydev)
  2881. +{
  2882. + struct ar8xxx_priv *priv;
  2883. + struct switch_dev *swdev;
  2884. + int ret;
  2885. +
  2886. + /* skip PHYs at unused adresses */
  2887. + if (phydev->addr != 0 && phydev->addr != 4)
  2888. + return -ENODEV;
  2889. +
  2890. + if (!ar8xxx_is_possible(phydev->bus))
  2891. + return -ENODEV;
  2892. +
  2893. + mutex_lock(&ar8xxx_dev_list_lock);
  2894. + list_for_each_entry(priv, &ar8xxx_dev_list, list)
  2895. + if (priv->mii_bus == phydev->bus)
  2896. + goto found;
  2897. +
  2898. + priv = ar8xxx_create_mii(phydev->bus);
  2899. + if (priv == NULL) {
  2900. + ret = -ENOMEM;
  2901. + goto unlock;
  2902. + }
  2903. +
  2904. + ret = ar8xxx_probe_switch(priv);
  2905. + if (ret)
  2906. + goto free_priv;
  2907. +
  2908. + swdev = &priv->dev;
  2909. + swdev->alias = dev_name(&priv->mii_bus->dev);
  2910. + ret = register_switch(swdev, NULL);
  2911. + if (ret)
  2912. + goto free_priv;
  2913. +
  2914. + pr_info("%s: %s rev. %u switch registered on %s\n",
  2915. + swdev->devname, swdev->name, priv->chip_rev,
  2916. + dev_name(&priv->mii_bus->dev));
  2917. +
  2918. +found:
  2919. + priv->use_count++;
  2920. +
  2921. + if (phydev->addr == 0) {
  2922. + if (ar8xxx_has_gige(priv)) {
  2923. + phydev->supported = SUPPORTED_1000baseT_Full;
  2924. + phydev->advertising = ADVERTISED_1000baseT_Full;
  2925. + } else {
  2926. + phydev->supported = SUPPORTED_100baseT_Full;
  2927. + phydev->advertising = ADVERTISED_100baseT_Full;
  2928. + }
  2929. +
  2930. + if (chip_is_ar8327(priv) || chip_is_ar8337(priv)) {
  2931. + priv->phy = phydev;
  2932. +
  2933. + ret = ar8xxx_start(priv);
  2934. + if (ret)
  2935. + goto err_unregister_switch;
  2936. + }
  2937. + } else {
  2938. + if (ar8xxx_has_gige(priv)) {
  2939. + phydev->supported |= SUPPORTED_1000baseT_Full;
  2940. + phydev->advertising |= ADVERTISED_1000baseT_Full;
  2941. + }
  2942. + }
  2943. +
  2944. + phydev->priv = priv;
  2945. +
  2946. + list_add(&priv->list, &ar8xxx_dev_list);
  2947. +
  2948. + mutex_unlock(&ar8xxx_dev_list_lock);
  2949. +
  2950. + return 0;
  2951. +
  2952. +err_unregister_switch:
  2953. + if (--priv->use_count)
  2954. + goto unlock;
  2955. +
  2956. + unregister_switch(&priv->dev);
  2957. +
  2958. +free_priv:
  2959. + ar8xxx_free(priv);
  2960. +unlock:
  2961. + mutex_unlock(&ar8xxx_dev_list_lock);
  2962. + return ret;
  2963. +}
  2964. +
  2965. +static void
  2966. +ar8xxx_phy_detach(struct phy_device *phydev)
  2967. +{
  2968. + struct net_device *dev = phydev->attached_dev;
  2969. +
  2970. + if (!dev)
  2971. + return;
  2972. +
  2973. + dev->phy_ptr = NULL;
  2974. + dev->priv_flags &= ~IFF_NO_IP_ALIGN;
  2975. + dev->eth_mangle_rx = NULL;
  2976. + dev->eth_mangle_tx = NULL;
  2977. +}
  2978. +
  2979. +static void
  2980. +ar8xxx_phy_remove(struct phy_device *phydev)
  2981. +{
  2982. + struct ar8xxx_priv *priv = phydev->priv;
  2983. +
  2984. + if (WARN_ON(!priv))
  2985. + return;
  2986. +
  2987. + phydev->priv = NULL;
  2988. + if (--priv->use_count > 0)
  2989. + return;
  2990. +
  2991. + mutex_lock(&ar8xxx_dev_list_lock);
  2992. + list_del(&priv->list);
  2993. + mutex_unlock(&ar8xxx_dev_list_lock);
  2994. +
  2995. + unregister_switch(&priv->dev);
  2996. + ar8xxx_mib_stop(priv);
  2997. + ar8xxx_free(priv);
  2998. +}
  2999. +
  3000. +static struct phy_driver ar8xxx_phy_driver = {
  3001. + .phy_id = 0x004d0000,
  3002. + .name = "Atheros AR8216/AR8236/AR8316",
  3003. + .phy_id_mask = 0xffff0000,
  3004. + .features = PHY_BASIC_FEATURES,
  3005. + .probe = ar8xxx_phy_probe,
  3006. + .remove = ar8xxx_phy_remove,
  3007. + .detach = ar8xxx_phy_detach,
  3008. + .config_init = ar8xxx_phy_config_init,
  3009. + .config_aneg = ar8xxx_phy_config_aneg,
  3010. + .read_status = ar8xxx_phy_read_status,
  3011. + .driver = { .owner = THIS_MODULE },
  3012. +};
  3013. +
  3014. +int __init
  3015. +ar8xxx_init(void)
  3016. +{
  3017. + return phy_driver_register(&ar8xxx_phy_driver);
  3018. +}
  3019. +
  3020. +void __exit
  3021. +ar8xxx_exit(void)
  3022. +{
  3023. + phy_driver_unregister(&ar8xxx_phy_driver);
  3024. +}
  3025. +
  3026. +module_init(ar8xxx_init);
  3027. +module_exit(ar8xxx_exit);
  3028. +MODULE_LICENSE("GPL");
  3029. +
  3030. diff --git a/drivers/net/phy/ar8216.h b/drivers/net/phy/ar8216.h
  3031. new file mode 100644
  3032. index 0000000..00d6d7f
  3033. --- /dev/null
  3034. +++ b/drivers/net/phy/ar8216.h
  3035. @@ -0,0 +1,492 @@
  3036. +/*
  3037. + * ar8216.h: AR8216 switch driver
  3038. + *
  3039. + * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
  3040. + *
  3041. + * This program is free software; you can redistribute it and/or
  3042. + * modify it under the terms of the GNU General Public License
  3043. + * as published by the Free Software Foundation; either version 2
  3044. + * of the License, or (at your option) any later version.
  3045. + *
  3046. + * This program is distributed in the hope that it will be useful,
  3047. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3048. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3049. + * GNU General Public License for more details.
  3050. + */
  3051. +
  3052. +#ifndef __AR8216_H
  3053. +#define __AR8216_H
  3054. +
  3055. +#define BITS(_s, _n) (((1UL << (_n)) - 1) << _s)
  3056. +
  3057. +#define AR8216_PORT_CPU 0
  3058. +#define AR8216_NUM_PORTS 6
  3059. +#define AR8216_NUM_VLANS 16
  3060. +#define AR8316_NUM_VLANS 4096
  3061. +
  3062. +/* Atheros specific MII registers */
  3063. +#define MII_ATH_MMD_ADDR 0x0d
  3064. +#define MII_ATH_MMD_DATA 0x0e
  3065. +#define MII_ATH_DBG_ADDR 0x1d
  3066. +#define MII_ATH_DBG_DATA 0x1e
  3067. +
  3068. +#define AR8216_REG_CTRL 0x0000
  3069. +#define AR8216_CTRL_REVISION BITS(0, 8)
  3070. +#define AR8216_CTRL_REVISION_S 0
  3071. +#define AR8216_CTRL_VERSION BITS(8, 8)
  3072. +#define AR8216_CTRL_VERSION_S 8
  3073. +#define AR8216_CTRL_RESET BIT(31)
  3074. +
  3075. +#define AR8216_REG_FLOOD_MASK 0x002C
  3076. +#define AR8216_FM_UNI_DEST_PORTS BITS(0, 6)
  3077. +#define AR8216_FM_MULTI_DEST_PORTS BITS(16, 6)
  3078. +
  3079. +#define AR8216_REG_GLOBAL_CTRL 0x0030
  3080. +#define AR8216_GCTRL_MTU BITS(0, 11)
  3081. +#define AR8236_GCTRL_MTU BITS(0, 14)
  3082. +#define AR8316_GCTRL_MTU BITS(0, 14)
  3083. +
  3084. +#define AR8216_REG_VTU 0x0040
  3085. +#define AR8216_VTU_OP BITS(0, 3)
  3086. +#define AR8216_VTU_OP_NOOP 0x0
  3087. +#define AR8216_VTU_OP_FLUSH 0x1
  3088. +#define AR8216_VTU_OP_LOAD 0x2
  3089. +#define AR8216_VTU_OP_PURGE 0x3
  3090. +#define AR8216_VTU_OP_REMOVE_PORT 0x4
  3091. +#define AR8216_VTU_ACTIVE BIT(3)
  3092. +#define AR8216_VTU_FULL BIT(4)
  3093. +#define AR8216_VTU_PORT BITS(8, 4)
  3094. +#define AR8216_VTU_PORT_S 8
  3095. +#define AR8216_VTU_VID BITS(16, 12)
  3096. +#define AR8216_VTU_VID_S 16
  3097. +#define AR8216_VTU_PRIO BITS(28, 3)
  3098. +#define AR8216_VTU_PRIO_S 28
  3099. +#define AR8216_VTU_PRIO_EN BIT(31)
  3100. +
  3101. +#define AR8216_REG_VTU_DATA 0x0044
  3102. +#define AR8216_VTUDATA_MEMBER BITS(0, 10)
  3103. +#define AR8236_VTUDATA_MEMBER BITS(0, 7)
  3104. +#define AR8216_VTUDATA_VALID BIT(11)
  3105. +
  3106. +#define AR8216_REG_ATU 0x0050
  3107. +#define AR8216_ATU_OP BITS(0, 3)
  3108. +#define AR8216_ATU_OP_NOOP 0x0
  3109. +#define AR8216_ATU_OP_FLUSH 0x1
  3110. +#define AR8216_ATU_OP_LOAD 0x2
  3111. +#define AR8216_ATU_OP_PURGE 0x3
  3112. +#define AR8216_ATU_OP_FLUSH_LOCKED 0x4
  3113. +#define AR8216_ATU_OP_FLUSH_UNICAST 0x5
  3114. +#define AR8216_ATU_OP_GET_NEXT 0x6
  3115. +#define AR8216_ATU_ACTIVE BIT(3)
  3116. +#define AR8216_ATU_PORT_NUM BITS(8, 4)
  3117. +#define AR8216_ATU_FULL_VIO BIT(12)
  3118. +#define AR8216_ATU_ADDR4 BITS(16, 8)
  3119. +#define AR8216_ATU_ADDR5 BITS(24, 8)
  3120. +
  3121. +#define AR8216_REG_ATU_DATA 0x0054
  3122. +#define AR8216_ATU_ADDR3 BITS(0, 8)
  3123. +#define AR8216_ATU_ADDR2 BITS(8, 8)
  3124. +#define AR8216_ATU_ADDR1 BITS(16, 8)
  3125. +#define AR8216_ATU_ADDR0 BITS(24, 8)
  3126. +
  3127. +#define AR8216_REG_ATU_CTRL 0x005C
  3128. +#define AR8216_ATU_CTRL_AGE_EN BIT(17)
  3129. +#define AR8216_ATU_CTRL_AGE_TIME BITS(0, 16)
  3130. +#define AR8216_ATU_CTRL_AGE_TIME_S 0
  3131. +
  3132. +#define AR8216_REG_MIB_FUNC 0x0080
  3133. +#define AR8216_MIB_TIMER BITS(0, 16)
  3134. +#define AR8216_MIB_AT_HALF_EN BIT(16)
  3135. +#define AR8216_MIB_BUSY BIT(17)
  3136. +#define AR8216_MIB_FUNC BITS(24, 3)
  3137. +#define AR8216_MIB_FUNC_S 24
  3138. +#define AR8216_MIB_FUNC_NO_OP 0x0
  3139. +#define AR8216_MIB_FUNC_FLUSH 0x1
  3140. +#define AR8216_MIB_FUNC_CAPTURE 0x3
  3141. +#define AR8236_MIB_EN BIT(30)
  3142. +
  3143. +#define AR8216_REG_GLOBAL_CPUPORT 0x0078
  3144. +#define AR8216_GLOBAL_CPUPORT_MIRROR_PORT BITS(4, 4)
  3145. +#define AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S 4
  3146. +
  3147. +#define AR8216_PORT_OFFSET(_i) (0x0100 * (_i + 1))
  3148. +#define AR8216_REG_PORT_STATUS(_i) (AR8216_PORT_OFFSET(_i) + 0x0000)
  3149. +#define AR8216_PORT_STATUS_SPEED BITS(0,2)
  3150. +#define AR8216_PORT_STATUS_SPEED_S 0
  3151. +#define AR8216_PORT_STATUS_TXMAC BIT(2)
  3152. +#define AR8216_PORT_STATUS_RXMAC BIT(3)
  3153. +#define AR8216_PORT_STATUS_TXFLOW BIT(4)
  3154. +#define AR8216_PORT_STATUS_RXFLOW BIT(5)
  3155. +#define AR8216_PORT_STATUS_DUPLEX BIT(6)
  3156. +#define AR8216_PORT_STATUS_LINK_UP BIT(8)
  3157. +#define AR8216_PORT_STATUS_LINK_AUTO BIT(9)
  3158. +#define AR8216_PORT_STATUS_LINK_PAUSE BIT(10)
  3159. +
  3160. +#define AR8216_REG_PORT_CTRL(_i) (AR8216_PORT_OFFSET(_i) + 0x0004)
  3161. +
  3162. +/* port forwarding state */
  3163. +#define AR8216_PORT_CTRL_STATE BITS(0, 3)
  3164. +#define AR8216_PORT_CTRL_STATE_S 0
  3165. +
  3166. +#define AR8216_PORT_CTRL_LEARN_LOCK BIT(7)
  3167. +
  3168. +/* egress 802.1q mode */
  3169. +#define AR8216_PORT_CTRL_VLAN_MODE BITS(8, 2)
  3170. +#define AR8216_PORT_CTRL_VLAN_MODE_S 8
  3171. +
  3172. +#define AR8216_PORT_CTRL_IGMP_SNOOP BIT(10)
  3173. +#define AR8216_PORT_CTRL_HEADER BIT(11)
  3174. +#define AR8216_PORT_CTRL_MAC_LOOP BIT(12)
  3175. +#define AR8216_PORT_CTRL_SINGLE_VLAN BIT(13)
  3176. +#define AR8216_PORT_CTRL_LEARN BIT(14)
  3177. +#define AR8216_PORT_CTRL_MIRROR_TX BIT(16)
  3178. +#define AR8216_PORT_CTRL_MIRROR_RX BIT(17)
  3179. +
  3180. +#define AR8216_REG_PORT_VLAN(_i) (AR8216_PORT_OFFSET(_i) + 0x0008)
  3181. +
  3182. +#define AR8216_PORT_VLAN_DEFAULT_ID BITS(0, 12)
  3183. +#define AR8216_PORT_VLAN_DEFAULT_ID_S 0
  3184. +
  3185. +#define AR8216_PORT_VLAN_DEST_PORTS BITS(16, 9)
  3186. +#define AR8216_PORT_VLAN_DEST_PORTS_S 16
  3187. +
  3188. +/* bit0 added to the priority field of egress frames */
  3189. +#define AR8216_PORT_VLAN_TX_PRIO BIT(27)
  3190. +
  3191. +/* port default priority */
  3192. +#define AR8216_PORT_VLAN_PRIORITY BITS(28, 2)
  3193. +#define AR8216_PORT_VLAN_PRIORITY_S 28
  3194. +
  3195. +/* ingress 802.1q mode */
  3196. +#define AR8216_PORT_VLAN_MODE BITS(30, 2)
  3197. +#define AR8216_PORT_VLAN_MODE_S 30
  3198. +
  3199. +#define AR8216_REG_PORT_RATE(_i) (AR8216_PORT_OFFSET(_i) + 0x000c)
  3200. +#define AR8216_REG_PORT_PRIO(_i) (AR8216_PORT_OFFSET(_i) + 0x0010)
  3201. +
  3202. +#define AR8216_REG_PORT_STATS_BASE(_i) (0x19000 + (_i) * 0xa0)
  3203. +
  3204. +#define AR8216_STATS_RXBROAD 0x00
  3205. +#define AR8216_STATS_RXPAUSE 0x04
  3206. +#define AR8216_STATS_RXMULTI 0x08
  3207. +#define AR8216_STATS_RXFCSERR 0x0c
  3208. +#define AR8216_STATS_RXALIGNERR 0x10
  3209. +#define AR8216_STATS_RXRUNT 0x14
  3210. +#define AR8216_STATS_RXFRAGMENT 0x18
  3211. +#define AR8216_STATS_RX64BYTE 0x1c
  3212. +#define AR8216_STATS_RX128BYTE 0x20
  3213. +#define AR8216_STATS_RX256BYTE 0x24
  3214. +#define AR8216_STATS_RX512BYTE 0x28
  3215. +#define AR8216_STATS_RX1024BYTE 0x2c
  3216. +#define AR8216_STATS_RXMAXBYTE 0x30
  3217. +#define AR8216_STATS_RXTOOLONG 0x34
  3218. +#define AR8216_STATS_RXGOODBYTE 0x38
  3219. +#define AR8216_STATS_RXBADBYTE 0x40
  3220. +#define AR8216_STATS_RXOVERFLOW 0x48
  3221. +#define AR8216_STATS_FILTERED 0x4c
  3222. +#define AR8216_STATS_TXBROAD 0x50
  3223. +#define AR8216_STATS_TXPAUSE 0x54
  3224. +#define AR8216_STATS_TXMULTI 0x58
  3225. +#define AR8216_STATS_TXUNDERRUN 0x5c
  3226. +#define AR8216_STATS_TX64BYTE 0x60
  3227. +#define AR8216_STATS_TX128BYTE 0x64
  3228. +#define AR8216_STATS_TX256BYTE 0x68
  3229. +#define AR8216_STATS_TX512BYTE 0x6c
  3230. +#define AR8216_STATS_TX1024BYTE 0x70
  3231. +#define AR8216_STATS_TXMAXBYTE 0x74
  3232. +#define AR8216_STATS_TXOVERSIZE 0x78
  3233. +#define AR8216_STATS_TXBYTE 0x7c
  3234. +#define AR8216_STATS_TXCOLLISION 0x84
  3235. +#define AR8216_STATS_TXABORTCOL 0x88
  3236. +#define AR8216_STATS_TXMULTICOL 0x8c
  3237. +#define AR8216_STATS_TXSINGLECOL 0x90
  3238. +#define AR8216_STATS_TXEXCDEFER 0x94
  3239. +#define AR8216_STATS_TXDEFER 0x98
  3240. +#define AR8216_STATS_TXLATECOL 0x9c
  3241. +
  3242. +#define AR8236_REG_PORT_VLAN(_i) (AR8216_PORT_OFFSET((_i)) + 0x0008)
  3243. +#define AR8236_PORT_VLAN_DEFAULT_ID BITS(16, 12)
  3244. +#define AR8236_PORT_VLAN_DEFAULT_ID_S 16
  3245. +#define AR8236_PORT_VLAN_PRIORITY BITS(29, 3)
  3246. +#define AR8236_PORT_VLAN_PRIORITY_S 28
  3247. +
  3248. +#define AR8236_REG_PORT_VLAN2(_i) (AR8216_PORT_OFFSET((_i)) + 0x000c)
  3249. +#define AR8236_PORT_VLAN2_MEMBER BITS(16, 7)
  3250. +#define AR8236_PORT_VLAN2_MEMBER_S 16
  3251. +#define AR8236_PORT_VLAN2_TX_PRIO BIT(23)
  3252. +#define AR8236_PORT_VLAN2_VLAN_MODE BITS(30, 2)
  3253. +#define AR8236_PORT_VLAN2_VLAN_MODE_S 30
  3254. +
  3255. +#define AR8236_REG_PORT_STATS_BASE(_i) (0x20000 + (_i) * 0x100)
  3256. +
  3257. +#define AR8236_STATS_RXBROAD 0x00
  3258. +#define AR8236_STATS_RXPAUSE 0x04
  3259. +#define AR8236_STATS_RXMULTI 0x08
  3260. +#define AR8236_STATS_RXFCSERR 0x0c
  3261. +#define AR8236_STATS_RXALIGNERR 0x10
  3262. +#define AR8236_STATS_RXRUNT 0x14
  3263. +#define AR8236_STATS_RXFRAGMENT 0x18
  3264. +#define AR8236_STATS_RX64BYTE 0x1c
  3265. +#define AR8236_STATS_RX128BYTE 0x20
  3266. +#define AR8236_STATS_RX256BYTE 0x24
  3267. +#define AR8236_STATS_RX512BYTE 0x28
  3268. +#define AR8236_STATS_RX1024BYTE 0x2c
  3269. +#define AR8236_STATS_RX1518BYTE 0x30
  3270. +#define AR8236_STATS_RXMAXBYTE 0x34
  3271. +#define AR8236_STATS_RXTOOLONG 0x38
  3272. +#define AR8236_STATS_RXGOODBYTE 0x3c
  3273. +#define AR8236_STATS_RXBADBYTE 0x44
  3274. +#define AR8236_STATS_RXOVERFLOW 0x4c
  3275. +#define AR8236_STATS_FILTERED 0x50
  3276. +#define AR8236_STATS_TXBROAD 0x54
  3277. +#define AR8236_STATS_TXPAUSE 0x58
  3278. +#define AR8236_STATS_TXMULTI 0x5c
  3279. +#define AR8236_STATS_TXUNDERRUN 0x60
  3280. +#define AR8236_STATS_TX64BYTE 0x64
  3281. +#define AR8236_STATS_TX128BYTE 0x68
  3282. +#define AR8236_STATS_TX256BYTE 0x6c
  3283. +#define AR8236_STATS_TX512BYTE 0x70
  3284. +#define AR8236_STATS_TX1024BYTE 0x74
  3285. +#define AR8236_STATS_TX1518BYTE 0x78
  3286. +#define AR8236_STATS_TXMAXBYTE 0x7c
  3287. +#define AR8236_STATS_TXOVERSIZE 0x80
  3288. +#define AR8236_STATS_TXBYTE 0x84
  3289. +#define AR8236_STATS_TXCOLLISION 0x8c
  3290. +#define AR8236_STATS_TXABORTCOL 0x90
  3291. +#define AR8236_STATS_TXMULTICOL 0x94
  3292. +#define AR8236_STATS_TXSINGLECOL 0x98
  3293. +#define AR8236_STATS_TXEXCDEFER 0x9c
  3294. +#define AR8236_STATS_TXDEFER 0xa0
  3295. +#define AR8236_STATS_TXLATECOL 0xa4
  3296. +
  3297. +#define AR8316_REG_POSTRIP 0x0008
  3298. +#define AR8316_POSTRIP_MAC0_GMII_EN BIT(0)
  3299. +#define AR8316_POSTRIP_MAC0_RGMII_EN BIT(1)
  3300. +#define AR8316_POSTRIP_PHY4_GMII_EN BIT(2)
  3301. +#define AR8316_POSTRIP_PHY4_RGMII_EN BIT(3)
  3302. +#define AR8316_POSTRIP_MAC0_MAC_MODE BIT(4)
  3303. +#define AR8316_POSTRIP_RTL_MODE BIT(5)
  3304. +#define AR8316_POSTRIP_RGMII_RXCLK_DELAY_EN BIT(6)
  3305. +#define AR8316_POSTRIP_RGMII_TXCLK_DELAY_EN BIT(7)
  3306. +#define AR8316_POSTRIP_SERDES_EN BIT(8)
  3307. +#define AR8316_POSTRIP_SEL_ANA_RST BIT(9)
  3308. +#define AR8316_POSTRIP_GATE_25M_EN BIT(10)
  3309. +#define AR8316_POSTRIP_SEL_CLK25M BIT(11)
  3310. +#define AR8316_POSTRIP_HIB_PULSE_HW BIT(12)
  3311. +#define AR8316_POSTRIP_DBG_MODE_I BIT(13)
  3312. +#define AR8316_POSTRIP_MAC5_MAC_MODE BIT(14)
  3313. +#define AR8316_POSTRIP_MAC5_PHY_MODE BIT(15)
  3314. +#define AR8316_POSTRIP_POWER_DOWN_HW BIT(16)
  3315. +#define AR8316_POSTRIP_LPW_STATE_EN BIT(17)
  3316. +#define AR8316_POSTRIP_MAN_EN BIT(18)
  3317. +#define AR8316_POSTRIP_PHY_PLL_ON BIT(19)
  3318. +#define AR8316_POSTRIP_LPW_EXIT BIT(20)
  3319. +#define AR8316_POSTRIP_TXDELAY_S0 BIT(21)
  3320. +#define AR8316_POSTRIP_TXDELAY_S1 BIT(22)
  3321. +#define AR8316_POSTRIP_RXDELAY_S0 BIT(23)
  3322. +#define AR8316_POSTRIP_LED_OPEN_EN BIT(24)
  3323. +#define AR8316_POSTRIP_SPI_EN BIT(25)
  3324. +#define AR8316_POSTRIP_RXDELAY_S1 BIT(26)
  3325. +#define AR8316_POSTRIP_POWER_ON_SEL BIT(31)
  3326. +
  3327. +#define AR8327_NUM_PORTS 7
  3328. +#define AR8327_NUM_LEDS 15
  3329. +#define AR8327_NUM_PHYS 5
  3330. +#define AR8327_PORTS_ALL 0x7f
  3331. +#define AR8327_NUM_LED_CTRL_REGS 4
  3332. +
  3333. +#define AR8327_REG_MASK 0x000
  3334. +
  3335. +#define AR8327_REG_PAD0_MODE 0x004
  3336. +#define AR8327_REG_PAD5_MODE 0x008
  3337. +#define AR8327_REG_PAD6_MODE 0x00c
  3338. +#define AR8327_PAD_MAC_MII_RXCLK_SEL BIT(0)
  3339. +#define AR8327_PAD_MAC_MII_TXCLK_SEL BIT(1)
  3340. +#define AR8327_PAD_MAC_MII_EN BIT(2)
  3341. +#define AR8327_PAD_MAC_GMII_RXCLK_SEL BIT(4)
  3342. +#define AR8327_PAD_MAC_GMII_TXCLK_SEL BIT(5)
  3343. +#define AR8327_PAD_MAC_GMII_EN BIT(6)
  3344. +#define AR8327_PAD_SGMII_EN BIT(7)
  3345. +#define AR8327_PAD_PHY_MII_RXCLK_SEL BIT(8)
  3346. +#define AR8327_PAD_PHY_MII_TXCLK_SEL BIT(9)
  3347. +#define AR8327_PAD_PHY_MII_EN BIT(10)
  3348. +#define AR8327_PAD_PHY_GMII_PIPE_RXCLK_SEL BIT(11)
  3349. +#define AR8327_PAD_PHY_GMII_RXCLK_SEL BIT(12)
  3350. +#define AR8327_PAD_PHY_GMII_TXCLK_SEL BIT(13)
  3351. +#define AR8327_PAD_PHY_GMII_EN BIT(14)
  3352. +#define AR8327_PAD_PHYX_GMII_EN BIT(16)
  3353. +#define AR8327_PAD_PHYX_RGMII_EN BIT(17)
  3354. +#define AR8327_PAD_PHYX_MII_EN BIT(18)
  3355. +#define AR8327_PAD_SGMII_DELAY_EN BIT(19)
  3356. +#define AR8327_PAD_RGMII_RXCLK_DELAY_SEL BITS(20, 2)
  3357. +#define AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S 20
  3358. +#define AR8327_PAD_RGMII_TXCLK_DELAY_SEL BITS(22, 2)
  3359. +#define AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S 22
  3360. +#define AR8327_PAD_RGMII_RXCLK_DELAY_EN BIT(24)
  3361. +#define AR8327_PAD_RGMII_TXCLK_DELAY_EN BIT(25)
  3362. +#define AR8327_PAD_RGMII_EN BIT(26)
  3363. +
  3364. +#define AR8327_REG_POWER_ON_STRIP 0x010
  3365. +#define AR8327_POWER_ON_STRIP_POWER_ON_SEL BIT(31)
  3366. +#define AR8327_POWER_ON_STRIP_LED_OPEN_EN BIT(24)
  3367. +#define AR8327_POWER_ON_STRIP_SERDES_AEN BIT(7)
  3368. +
  3369. +#define AR8327_REG_INT_STATUS0 0x020
  3370. +#define AR8327_INT0_VT_DONE BIT(20)
  3371. +
  3372. +#define AR8327_REG_INT_STATUS1 0x024
  3373. +#define AR8327_REG_INT_MASK0 0x028
  3374. +#define AR8327_REG_INT_MASK1 0x02c
  3375. +
  3376. +#define AR8327_REG_MODULE_EN 0x030
  3377. +#define AR8327_MODULE_EN_MIB BIT(0)
  3378. +
  3379. +#define AR8327_REG_MIB_FUNC 0x034
  3380. +#define AR8327_MIB_CPU_KEEP BIT(20)
  3381. +
  3382. +#define AR8327_REG_SERVICE_TAG 0x048
  3383. +#define AR8327_REG_LED_CTRL(_i) (0x050 + (_i) * 4)
  3384. +#define AR8327_REG_LED_CTRL0 0x050
  3385. +#define AR8327_REG_LED_CTRL1 0x054
  3386. +#define AR8327_REG_LED_CTRL2 0x058
  3387. +#define AR8327_REG_LED_CTRL3 0x05c
  3388. +#define AR8327_REG_MAC_ADDR0 0x060
  3389. +#define AR8327_REG_MAC_ADDR1 0x064
  3390. +
  3391. +#define AR8327_REG_MAX_FRAME_SIZE 0x078
  3392. +#define AR8327_MAX_FRAME_SIZE_MTU BITS(0, 14)
  3393. +
  3394. +#define AR8327_REG_PORT_STATUS(_i) (0x07c + (_i) * 4)
  3395. +
  3396. +#define AR8327_REG_HEADER_CTRL 0x098
  3397. +#define AR8327_REG_PORT_HEADER(_i) (0x09c + (_i) * 4)
  3398. +
  3399. +#define AR8327_REG_SGMII_CTRL 0x0e0
  3400. +#define AR8327_SGMII_CTRL_EN_PLL BIT(1)
  3401. +#define AR8327_SGMII_CTRL_EN_RX BIT(2)
  3402. +#define AR8327_SGMII_CTRL_EN_TX BIT(3)
  3403. +
  3404. +#define AR8327_REG_PORT_VLAN0(_i) (0x420 + (_i) * 0x8)
  3405. +#define AR8327_PORT_VLAN0_DEF_SVID BITS(0, 12)
  3406. +#define AR8327_PORT_VLAN0_DEF_SVID_S 0
  3407. +#define AR8327_PORT_VLAN0_DEF_CVID BITS(16, 12)
  3408. +#define AR8327_PORT_VLAN0_DEF_CVID_S 16
  3409. +
  3410. +#define AR8327_REG_PORT_VLAN1(_i) (0x424 + (_i) * 0x8)
  3411. +#define AR8327_PORT_VLAN1_PORT_VLAN_PROP BIT(6)
  3412. +#define AR8327_PORT_VLAN1_OUT_MODE BITS(12, 2)
  3413. +#define AR8327_PORT_VLAN1_OUT_MODE_S 12
  3414. +#define AR8327_PORT_VLAN1_OUT_MODE_UNMOD 0
  3415. +#define AR8327_PORT_VLAN1_OUT_MODE_UNTAG 1
  3416. +#define AR8327_PORT_VLAN1_OUT_MODE_TAG 2
  3417. +#define AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH 3
  3418. +
  3419. +#define AR8327_REG_ATU_DATA0 0x600
  3420. +#define AR8327_REG_ATU_DATA1 0x604
  3421. +#define AR8327_REG_ATU_DATA2 0x608
  3422. +
  3423. +#define AR8327_REG_ATU_FUNC 0x60c
  3424. +#define AR8327_ATU_FUNC_OP BITS(0, 4)
  3425. +#define AR8327_ATU_FUNC_OP_NOOP 0x0
  3426. +#define AR8327_ATU_FUNC_OP_FLUSH 0x1
  3427. +#define AR8327_ATU_FUNC_OP_LOAD 0x2
  3428. +#define AR8327_ATU_FUNC_OP_PURGE 0x3
  3429. +#define AR8327_ATU_FUNC_OP_FLUSH_LOCKED 0x4
  3430. +#define AR8327_ATU_FUNC_OP_FLUSH_UNICAST 0x5
  3431. +#define AR8327_ATU_FUNC_OP_GET_NEXT 0x6
  3432. +#define AR8327_ATU_FUNC_OP_SEARCH_MAC 0x7
  3433. +#define AR8327_ATU_FUNC_OP_CHANGE_TRUNK 0x8
  3434. +#define AR8327_ATU_FUNC_BUSY BIT(31)
  3435. +
  3436. +#define AR8327_REG_VTU_FUNC0 0x0610
  3437. +#define AR8327_VTU_FUNC0_EG_MODE BITS(4, 14)
  3438. +#define AR8327_VTU_FUNC0_EG_MODE_S(_i) (4 + (_i) * 2)
  3439. +#define AR8327_VTU_FUNC0_EG_MODE_KEEP 0
  3440. +#define AR8327_VTU_FUNC0_EG_MODE_UNTAG 1
  3441. +#define AR8327_VTU_FUNC0_EG_MODE_TAG 2
  3442. +#define AR8327_VTU_FUNC0_EG_MODE_NOT 3
  3443. +#define AR8327_VTU_FUNC0_IVL BIT(19)
  3444. +#define AR8327_VTU_FUNC0_VALID BIT(20)
  3445. +
  3446. +#define AR8327_REG_VTU_FUNC1 0x0614
  3447. +#define AR8327_VTU_FUNC1_OP BITS(0, 3)
  3448. +#define AR8327_VTU_FUNC1_OP_NOOP 0
  3449. +#define AR8327_VTU_FUNC1_OP_FLUSH 1
  3450. +#define AR8327_VTU_FUNC1_OP_LOAD 2
  3451. +#define AR8327_VTU_FUNC1_OP_PURGE 3
  3452. +#define AR8327_VTU_FUNC1_OP_REMOVE_PORT 4
  3453. +#define AR8327_VTU_FUNC1_OP_GET_NEXT 5
  3454. +#define AR8327_VTU_FUNC1_OP_GET_ONE 6
  3455. +#define AR8327_VTU_FUNC1_FULL BIT(4)
  3456. +#define AR8327_VTU_FUNC1_PORT BIT(8, 4)
  3457. +#define AR8327_VTU_FUNC1_PORT_S 8
  3458. +#define AR8327_VTU_FUNC1_VID BIT(16, 12)
  3459. +#define AR8327_VTU_FUNC1_VID_S 16
  3460. +#define AR8327_VTU_FUNC1_BUSY BIT(31)
  3461. +
  3462. +#define AR8327_REG_FWD_CTRL0 0x620
  3463. +#define AR8327_FWD_CTRL0_CPU_PORT_EN BIT(10)
  3464. +#define AR8327_FWD_CTRL0_MIRROR_PORT BITS(4, 4)
  3465. +#define AR8327_FWD_CTRL0_MIRROR_PORT_S 4
  3466. +
  3467. +#define AR8327_REG_FWD_CTRL1 0x624
  3468. +#define AR8327_FWD_CTRL1_UC_FLOOD BITS(0, 7)
  3469. +#define AR8327_FWD_CTRL1_UC_FLOOD_S 0
  3470. +#define AR8327_FWD_CTRL1_MC_FLOOD BITS(8, 7)
  3471. +#define AR8327_FWD_CTRL1_MC_FLOOD_S 8
  3472. +#define AR8327_FWD_CTRL1_BC_FLOOD BITS(16, 7)
  3473. +#define AR8327_FWD_CTRL1_BC_FLOOD_S 16
  3474. +#define AR8327_FWD_CTRL1_IGMP BITS(24, 7)
  3475. +#define AR8327_FWD_CTRL1_IGMP_S 24
  3476. +
  3477. +#define AR8327_REG_PORT_LOOKUP(_i) (0x660 + (_i) * 0xc)
  3478. +#define AR8327_PORT_LOOKUP_MEMBER BITS(0, 7)
  3479. +#define AR8327_PORT_LOOKUP_IN_MODE BITS(8, 2)
  3480. +#define AR8327_PORT_LOOKUP_IN_MODE_S 8
  3481. +#define AR8327_PORT_LOOKUP_STATE BITS(16, 3)
  3482. +#define AR8327_PORT_LOOKUP_STATE_S 16
  3483. +#define AR8327_PORT_LOOKUP_LEARN BIT(20)
  3484. +#define AR8327_PORT_LOOKUP_ING_MIRROR_EN BIT(25)
  3485. +
  3486. +#define AR8327_REG_PORT_PRIO(_i) (0x664 + (_i) * 0xc)
  3487. +
  3488. +#define AR8327_REG_PORT_HOL_CTRL1(_i) (0x974 + (_i) * 0x8)
  3489. +#define AR8327_PORT_HOL_CTRL1_EG_MIRROR_EN BIT(16)
  3490. +
  3491. +#define AR8327_REG_PORT_STATS_BASE(_i) (0x1000 + (_i) * 0x100)
  3492. +
  3493. +#define AR8337_PAD_MAC06_EXCHANGE_EN BIT(31)
  3494. +
  3495. +/* port speed */
  3496. +enum {
  3497. + AR8216_PORT_SPEED_10M = 0,
  3498. + AR8216_PORT_SPEED_100M = 1,
  3499. + AR8216_PORT_SPEED_1000M = 2,
  3500. + AR8216_PORT_SPEED_ERR = 3,
  3501. +};
  3502. +
  3503. +/* ingress 802.1q mode */
  3504. +enum {
  3505. + AR8216_IN_PORT_ONLY = 0,
  3506. + AR8216_IN_PORT_FALLBACK = 1,
  3507. + AR8216_IN_VLAN_ONLY = 2,
  3508. + AR8216_IN_SECURE = 3
  3509. +};
  3510. +
  3511. +/* egress 802.1q mode */
  3512. +enum {
  3513. + AR8216_OUT_KEEP = 0,
  3514. + AR8216_OUT_STRIP_VLAN = 1,
  3515. + AR8216_OUT_ADD_VLAN = 2
  3516. +};
  3517. +
  3518. +/* port forwarding state */
  3519. +enum {
  3520. + AR8216_PORT_STATE_DISABLED = 0,
  3521. + AR8216_PORT_STATE_BLOCK = 1,
  3522. + AR8216_PORT_STATE_LISTEN = 2,
  3523. + AR8216_PORT_STATE_LEARN = 3,
  3524. + AR8216_PORT_STATE_FORWARD = 4
  3525. +};
  3526. +
  3527. +#endif
  3528. diff --git a/include/linux/ar8216_platform.h b/include/linux/ar8216_platform.h
  3529. new file mode 100644
  3530. index 0000000..4935ad3
  3531. --- /dev/null
  3532. +++ b/include/linux/ar8216_platform.h
  3533. @@ -0,0 +1,131 @@
  3534. +/*
  3535. + * AR8216 switch driver platform data
  3536. + *
  3537. + * Copyright (C) 2012 Gabor Juhos <juhosg@openwrt.org>
  3538. + *
  3539. + * This program is free software; you can redistribute it and/or
  3540. + * modify it under the terms of the GNU General Public License
  3541. + * as published by the Free Software Foundation; either version 2
  3542. + * of the License, or (at your option) any later version.
  3543. + *
  3544. + * This program is distributed in the hope that it will be useful,
  3545. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3546. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3547. + * GNU General Public License for more details.
  3548. + */
  3549. +
  3550. +#ifndef AR8216_PLATFORM_H
  3551. +#define AR8216_PLATFORM_H
  3552. +
  3553. +enum ar8327_pad_mode {
  3554. + AR8327_PAD_NC = 0,
  3555. + AR8327_PAD_MAC2MAC_MII,
  3556. + AR8327_PAD_MAC2MAC_GMII,
  3557. + AR8327_PAD_MAC_SGMII,
  3558. + AR8327_PAD_MAC2PHY_MII,
  3559. + AR8327_PAD_MAC2PHY_GMII,
  3560. + AR8327_PAD_MAC_RGMII,
  3561. + AR8327_PAD_PHY_GMII,
  3562. + AR8327_PAD_PHY_RGMII,
  3563. + AR8327_PAD_PHY_MII,
  3564. +};
  3565. +
  3566. +enum ar8327_clk_delay_sel {
  3567. + AR8327_CLK_DELAY_SEL0 = 0,
  3568. + AR8327_CLK_DELAY_SEL1,
  3569. + AR8327_CLK_DELAY_SEL2,
  3570. + AR8327_CLK_DELAY_SEL3,
  3571. +};
  3572. +
  3573. +struct ar8327_pad_cfg {
  3574. + enum ar8327_pad_mode mode;
  3575. + bool rxclk_sel;
  3576. + bool txclk_sel;
  3577. + bool pipe_rxclk_sel;
  3578. + bool txclk_delay_en;
  3579. + bool rxclk_delay_en;
  3580. + bool sgmii_delay_en;
  3581. + enum ar8327_clk_delay_sel txclk_delay_sel;
  3582. + enum ar8327_clk_delay_sel rxclk_delay_sel;
  3583. +};
  3584. +
  3585. +enum ar8327_port_speed {
  3586. + AR8327_PORT_SPEED_10 = 0,
  3587. + AR8327_PORT_SPEED_100,
  3588. + AR8327_PORT_SPEED_1000,
  3589. +};
  3590. +
  3591. +struct ar8327_port_cfg {
  3592. + int force_link:1;
  3593. + enum ar8327_port_speed speed;
  3594. + int txpause:1;
  3595. + int rxpause:1;
  3596. + int duplex:1;
  3597. +};
  3598. +
  3599. +struct ar8327_sgmii_cfg {
  3600. + u32 sgmii_ctrl;
  3601. + bool serdes_aen;
  3602. +};
  3603. +
  3604. +struct ar8327_led_cfg {
  3605. + u32 led_ctrl0;
  3606. + u32 led_ctrl1;
  3607. + u32 led_ctrl2;
  3608. + u32 led_ctrl3;
  3609. + bool open_drain;
  3610. +};
  3611. +
  3612. +enum ar8327_led_num {
  3613. + AR8327_LED_PHY0_0 = 0,
  3614. + AR8327_LED_PHY0_1,
  3615. + AR8327_LED_PHY0_2,
  3616. + AR8327_LED_PHY1_0,
  3617. + AR8327_LED_PHY1_1,
  3618. + AR8327_LED_PHY1_2,
  3619. + AR8327_LED_PHY2_0,
  3620. + AR8327_LED_PHY2_1,
  3621. + AR8327_LED_PHY2_2,
  3622. + AR8327_LED_PHY3_0,
  3623. + AR8327_LED_PHY3_1,
  3624. + AR8327_LED_PHY3_2,
  3625. + AR8327_LED_PHY4_0,
  3626. + AR8327_LED_PHY4_1,
  3627. + AR8327_LED_PHY4_2,
  3628. +};
  3629. +
  3630. +enum ar8327_led_mode {
  3631. + AR8327_LED_MODE_HW = 0,
  3632. + AR8327_LED_MODE_SW,
  3633. +};
  3634. +
  3635. +struct ar8327_led_info {
  3636. + const char *name;
  3637. + const char *default_trigger;
  3638. + bool active_low;
  3639. + enum ar8327_led_num led_num;
  3640. + enum ar8327_led_mode mode;
  3641. +};
  3642. +
  3643. +#define AR8327_LED_INFO(_led, _mode, _name) { \
  3644. + .name = (_name), \
  3645. + .led_num = AR8327_LED_ ## _led, \
  3646. + .mode = AR8327_LED_MODE_ ## _mode \
  3647. +}
  3648. +
  3649. +struct ar8327_platform_data {
  3650. + struct ar8327_pad_cfg *pad0_cfg;
  3651. + struct ar8327_pad_cfg *pad5_cfg;
  3652. + struct ar8327_pad_cfg *pad6_cfg;
  3653. + struct ar8327_sgmii_cfg *sgmii_cfg;
  3654. + struct ar8327_port_cfg port0_cfg;
  3655. + struct ar8327_port_cfg port6_cfg;
  3656. + struct ar8327_led_cfg *led_cfg;
  3657. +
  3658. + int (*get_port_link)(unsigned port);
  3659. +
  3660. + unsigned num_leds;
  3661. + const struct ar8327_led_info *leds;
  3662. +};
  3663. +
  3664. +#endif /* AR8216_PLATFORM_H */
  3665. \ No newline at end of file
  3666. --
  3667. 1.8.5.3