ag71xx_ar7240.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /*
  2. * Driver for the built-in ethernet switch of the Atheros AR7240 SoC
  3. * Copyright (c) 2010 Gabor Juhos <[email protected]>
  4. * Copyright (c) 2010 Felix Fietkau <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/etherdevice.h>
  12. #include <linux/list.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/phy.h>
  15. #include <linux/mii.h>
  16. #include <linux/bitops.h>
  17. #include <linux/switch.h>
  18. #include "ag71xx.h"
  19. #define BITM(_count) (BIT(_count) - 1)
  20. #define BITS(_shift, _count) (BITM(_count) << _shift)
  21. #define AR7240_REG_MASK_CTRL 0x00
  22. #define AR7240_MASK_CTRL_REVISION_M BITM(8)
  23. #define AR7240_MASK_CTRL_VERSION_M BITM(8)
  24. #define AR7240_MASK_CTRL_VERSION_S 8
  25. #define AR7240_MASK_CTRL_VERSION_AR7240 0x01
  26. #define AR7240_MASK_CTRL_VERSION_AR934X 0x02
  27. #define AR7240_MASK_CTRL_SOFT_RESET BIT(31)
  28. #define AR7240_REG_MAC_ADDR0 0x20
  29. #define AR7240_REG_MAC_ADDR1 0x24
  30. #define AR7240_REG_FLOOD_MASK 0x2c
  31. #define AR7240_FLOOD_MASK_BROAD_TO_CPU BIT(26)
  32. #define AR7240_REG_GLOBAL_CTRL 0x30
  33. #define AR7240_GLOBAL_CTRL_MTU_M BITM(12)
  34. #define AR7240_REG_VTU 0x0040
  35. #define AR7240_VTU_OP BITM(3)
  36. #define AR7240_VTU_OP_NOOP 0x0
  37. #define AR7240_VTU_OP_FLUSH 0x1
  38. #define AR7240_VTU_OP_LOAD 0x2
  39. #define AR7240_VTU_OP_PURGE 0x3
  40. #define AR7240_VTU_OP_REMOVE_PORT 0x4
  41. #define AR7240_VTU_ACTIVE BIT(3)
  42. #define AR7240_VTU_FULL BIT(4)
  43. #define AR7240_VTU_PORT BITS(8, 4)
  44. #define AR7240_VTU_PORT_S 8
  45. #define AR7240_VTU_VID BITS(16, 12)
  46. #define AR7240_VTU_VID_S 16
  47. #define AR7240_VTU_PRIO BITS(28, 3)
  48. #define AR7240_VTU_PRIO_S 28
  49. #define AR7240_VTU_PRIO_EN BIT(31)
  50. #define AR7240_REG_VTU_DATA 0x0044
  51. #define AR7240_VTUDATA_MEMBER BITS(0, 10)
  52. #define AR7240_VTUDATA_VALID BIT(11)
  53. #define AR7240_REG_ATU 0x50
  54. #define AR7240_ATU_FLUSH_ALL 0x1
  55. #define AR7240_REG_AT_CTRL 0x5c
  56. #define AR7240_AT_CTRL_AGE_TIME BITS(0, 15)
  57. #define AR7240_AT_CTRL_AGE_EN BIT(17)
  58. #define AR7240_AT_CTRL_LEARN_CHANGE BIT(18)
  59. #define AR7240_AT_CTRL_RESERVED BIT(19)
  60. #define AR7240_AT_CTRL_ARP_EN BIT(20)
  61. #define AR7240_REG_TAG_PRIORITY 0x70
  62. #define AR7240_REG_SERVICE_TAG 0x74
  63. #define AR7240_SERVICE_TAG_M BITM(16)
  64. #define AR7240_REG_CPU_PORT 0x78
  65. #define AR7240_MIRROR_PORT_S 4
  66. #define AR7240_CPU_PORT_EN BIT(8)
  67. #define AR7240_REG_MIB_FUNCTION0 0x80
  68. #define AR7240_MIB_TIMER_M BITM(16)
  69. #define AR7240_MIB_AT_HALF_EN BIT(16)
  70. #define AR7240_MIB_BUSY BIT(17)
  71. #define AR7240_MIB_FUNC_S 24
  72. #define AR7240_MIB_FUNC_NO_OP 0x0
  73. #define AR7240_MIB_FUNC_FLUSH 0x1
  74. #define AR7240_MIB_FUNC_CAPTURE 0x3
  75. #define AR7240_REG_MDIO_CTRL 0x98
  76. #define AR7240_MDIO_CTRL_DATA_M BITM(16)
  77. #define AR7240_MDIO_CTRL_REG_ADDR_S 16
  78. #define AR7240_MDIO_CTRL_PHY_ADDR_S 21
  79. #define AR7240_MDIO_CTRL_CMD_WRITE 0
  80. #define AR7240_MDIO_CTRL_CMD_READ BIT(27)
  81. #define AR7240_MDIO_CTRL_MASTER_EN BIT(30)
  82. #define AR7240_MDIO_CTRL_BUSY BIT(31)
  83. #define AR7240_REG_PORT_BASE(_port) (0x100 + (_port) * 0x100)
  84. #define AR7240_REG_PORT_STATUS(_port) (AR7240_REG_PORT_BASE((_port)) + 0x00)
  85. #define AR7240_PORT_STATUS_SPEED_S 0
  86. #define AR7240_PORT_STATUS_SPEED_M BITM(2)
  87. #define AR7240_PORT_STATUS_SPEED_10 0
  88. #define AR7240_PORT_STATUS_SPEED_100 1
  89. #define AR7240_PORT_STATUS_SPEED_1000 2
  90. #define AR7240_PORT_STATUS_TXMAC BIT(2)
  91. #define AR7240_PORT_STATUS_RXMAC BIT(3)
  92. #define AR7240_PORT_STATUS_TXFLOW BIT(4)
  93. #define AR7240_PORT_STATUS_RXFLOW BIT(5)
  94. #define AR7240_PORT_STATUS_DUPLEX BIT(6)
  95. #define AR7240_PORT_STATUS_LINK_UP BIT(8)
  96. #define AR7240_PORT_STATUS_LINK_AUTO BIT(9)
  97. #define AR7240_PORT_STATUS_LINK_PAUSE BIT(10)
  98. #define AR7240_REG_PORT_CTRL(_port) (AR7240_REG_PORT_BASE((_port)) + 0x04)
  99. #define AR7240_PORT_CTRL_STATE_M BITM(3)
  100. #define AR7240_PORT_CTRL_STATE_DISABLED 0
  101. #define AR7240_PORT_CTRL_STATE_BLOCK 1
  102. #define AR7240_PORT_CTRL_STATE_LISTEN 2
  103. #define AR7240_PORT_CTRL_STATE_LEARN 3
  104. #define AR7240_PORT_CTRL_STATE_FORWARD 4
  105. #define AR7240_PORT_CTRL_LEARN_LOCK BIT(7)
  106. #define AR7240_PORT_CTRL_VLAN_MODE_S 8
  107. #define AR7240_PORT_CTRL_VLAN_MODE_KEEP 0
  108. #define AR7240_PORT_CTRL_VLAN_MODE_STRIP 1
  109. #define AR7240_PORT_CTRL_VLAN_MODE_ADD 2
  110. #define AR7240_PORT_CTRL_VLAN_MODE_DOUBLE_TAG 3
  111. #define AR7240_PORT_CTRL_IGMP_SNOOP BIT(10)
  112. #define AR7240_PORT_CTRL_HEADER BIT(11)
  113. #define AR7240_PORT_CTRL_MAC_LOOP BIT(12)
  114. #define AR7240_PORT_CTRL_SINGLE_VLAN BIT(13)
  115. #define AR7240_PORT_CTRL_LEARN BIT(14)
  116. #define AR7240_PORT_CTRL_DOUBLE_TAG BIT(15)
  117. #define AR7240_PORT_CTRL_MIRROR_TX BIT(16)
  118. #define AR7240_PORT_CTRL_MIRROR_RX BIT(17)
  119. #define AR7240_REG_PORT_VLAN(_port) (AR7240_REG_PORT_BASE((_port)) + 0x08)
  120. #define AR7240_PORT_VLAN_DEFAULT_ID_S 0
  121. #define AR7240_PORT_VLAN_DEST_PORTS_S 16
  122. #define AR7240_PORT_VLAN_MODE_S 30
  123. #define AR7240_PORT_VLAN_MODE_PORT_ONLY 0
  124. #define AR7240_PORT_VLAN_MODE_PORT_FALLBACK 1
  125. #define AR7240_PORT_VLAN_MODE_VLAN_ONLY 2
  126. #define AR7240_PORT_VLAN_MODE_SECURE 3
  127. #define AR7240_REG_STATS_BASE(_port) (0x20000 + (_port) * 0x100)
  128. #define AR7240_STATS_RXBROAD 0x00
  129. #define AR7240_STATS_RXPAUSE 0x04
  130. #define AR7240_STATS_RXMULTI 0x08
  131. #define AR7240_STATS_RXFCSERR 0x0c
  132. #define AR7240_STATS_RXALIGNERR 0x10
  133. #define AR7240_STATS_RXRUNT 0x14
  134. #define AR7240_STATS_RXFRAGMENT 0x18
  135. #define AR7240_STATS_RX64BYTE 0x1c
  136. #define AR7240_STATS_RX128BYTE 0x20
  137. #define AR7240_STATS_RX256BYTE 0x24
  138. #define AR7240_STATS_RX512BYTE 0x28
  139. #define AR7240_STATS_RX1024BYTE 0x2c
  140. #define AR7240_STATS_RX1518BYTE 0x30
  141. #define AR7240_STATS_RXMAXBYTE 0x34
  142. #define AR7240_STATS_RXTOOLONG 0x38
  143. #define AR7240_STATS_RXGOODBYTE 0x3c
  144. #define AR7240_STATS_RXBADBYTE 0x44
  145. #define AR7240_STATS_RXOVERFLOW 0x4c
  146. #define AR7240_STATS_FILTERED 0x50
  147. #define AR7240_STATS_TXBROAD 0x54
  148. #define AR7240_STATS_TXPAUSE 0x58
  149. #define AR7240_STATS_TXMULTI 0x5c
  150. #define AR7240_STATS_TXUNDERRUN 0x60
  151. #define AR7240_STATS_TX64BYTE 0x64
  152. #define AR7240_STATS_TX128BYTE 0x68
  153. #define AR7240_STATS_TX256BYTE 0x6c
  154. #define AR7240_STATS_TX512BYTE 0x70
  155. #define AR7240_STATS_TX1024BYTE 0x74
  156. #define AR7240_STATS_TX1518BYTE 0x78
  157. #define AR7240_STATS_TXMAXBYTE 0x7c
  158. #define AR7240_STATS_TXOVERSIZE 0x80
  159. #define AR7240_STATS_TXBYTE 0x84
  160. #define AR7240_STATS_TXCOLLISION 0x8c
  161. #define AR7240_STATS_TXABORTCOL 0x90
  162. #define AR7240_STATS_TXMULTICOL 0x94
  163. #define AR7240_STATS_TXSINGLECOL 0x98
  164. #define AR7240_STATS_TXEXCDEFER 0x9c
  165. #define AR7240_STATS_TXDEFER 0xa0
  166. #define AR7240_STATS_TXLATECOL 0xa4
  167. #define AR7240_PORT_CPU 0
  168. #define AR7240_NUM_PORTS 6
  169. #define AR7240_NUM_PHYS 5
  170. #define AR7240_PHY_ID1 0x004d
  171. #define AR7240_PHY_ID2 0xd041
  172. #define AR934X_PHY_ID1 0x004d
  173. #define AR934X_PHY_ID2 0xd042
  174. #define AR7240_MAX_VLANS 16
  175. #define AR934X_REG_OPER_MODE0 0x04
  176. #define AR934X_OPER_MODE0_MAC_GMII_EN BIT(6)
  177. #define AR934X_OPER_MODE0_PHY_MII_EN BIT(10)
  178. #define AR934X_REG_OPER_MODE1 0x08
  179. #define AR934X_REG_OPER_MODE1_PHY4_MII_EN BIT(28)
  180. #define AR934X_REG_PORT_BASE(_port) (0x100 + (_port) * 0x100)
  181. #define AR934X_REG_PORT_VLAN1(_port) (AR934X_REG_PORT_BASE((_port)) + 0x08)
  182. #define AR934X_PORT_VLAN1_DEFAULT_SVID_S 0
  183. #define AR934X_PORT_VLAN1_FORCE_DEFAULT_VID_EN BIT(12)
  184. #define AR934X_PORT_VLAN1_PORT_TLS_MODE BIT(13)
  185. #define AR934X_PORT_VLAN1_PORT_VLAN_PROP_EN BIT(14)
  186. #define AR934X_PORT_VLAN1_PORT_CLONE_EN BIT(15)
  187. #define AR934X_PORT_VLAN1_DEFAULT_CVID_S 16
  188. #define AR934X_PORT_VLAN1_FORCE_PORT_VLAN_EN BIT(28)
  189. #define AR934X_PORT_VLAN1_ING_PORT_PRI_S 29
  190. #define AR934X_REG_PORT_VLAN2(_port) (AR934X_REG_PORT_BASE((_port)) + 0x0c)
  191. #define AR934X_PORT_VLAN2_PORT_VID_MEM_S 16
  192. #define AR934X_PORT_VLAN2_8021Q_MODE_S 30
  193. #define AR934X_PORT_VLAN2_8021Q_MODE_PORT_ONLY 0
  194. #define AR934X_PORT_VLAN2_8021Q_MODE_PORT_FALLBACK 1
  195. #define AR934X_PORT_VLAN2_8021Q_MODE_VLAN_ONLY 2
  196. #define AR934X_PORT_VLAN2_8021Q_MODE_SECURE 3
  197. #define sw_to_ar7240(_dev) container_of(_dev, struct ar7240sw, swdev)
  198. struct ar7240sw {
  199. struct mii_bus *mii_bus;
  200. struct ag71xx_switch_platform_data *swdata;
  201. struct switch_dev swdev;
  202. int num_ports;
  203. u8 ver;
  204. bool vlan;
  205. u16 vlan_id[AR7240_MAX_VLANS];
  206. u8 vlan_table[AR7240_MAX_VLANS];
  207. u8 vlan_tagged;
  208. u16 pvid[AR7240_NUM_PORTS];
  209. char buf[80];
  210. };
  211. struct ar7240sw_hw_stat {
  212. char string[ETH_GSTRING_LEN];
  213. int sizeof_stat;
  214. int reg;
  215. };
  216. static DEFINE_MUTEX(reg_mutex);
  217. static inline int sw_is_ar7240(struct ar7240sw *as)
  218. {
  219. return as->ver == AR7240_MASK_CTRL_VERSION_AR7240;
  220. }
  221. static inline int sw_is_ar934x(struct ar7240sw *as)
  222. {
  223. return as->ver == AR7240_MASK_CTRL_VERSION_AR934X;
  224. }
  225. static inline u32 ar7240sw_port_mask(struct ar7240sw *as, int port)
  226. {
  227. return BIT(port);
  228. }
  229. static inline u32 ar7240sw_port_mask_all(struct ar7240sw *as)
  230. {
  231. return BIT(as->swdev.ports) - 1;
  232. }
  233. static inline u32 ar7240sw_port_mask_but(struct ar7240sw *as, int port)
  234. {
  235. return ar7240sw_port_mask_all(as) & ~BIT(port);
  236. }
  237. static inline u16 mk_phy_addr(u32 reg)
  238. {
  239. return 0x17 & ((reg >> 4) | 0x10);
  240. }
  241. static inline u16 mk_phy_reg(u32 reg)
  242. {
  243. return (reg << 1) & 0x1e;
  244. }
  245. static inline u16 mk_high_addr(u32 reg)
  246. {
  247. return (reg >> 7) & 0x1ff;
  248. }
  249. static u32 __ar7240sw_reg_read(struct mii_bus *mii, u32 reg)
  250. {
  251. unsigned long flags;
  252. u16 phy_addr;
  253. u16 phy_reg;
  254. u32 hi, lo;
  255. reg = (reg & 0xfffffffc) >> 2;
  256. phy_addr = mk_phy_addr(reg);
  257. phy_reg = mk_phy_reg(reg);
  258. local_irq_save(flags);
  259. ag71xx_mdio_mii_write(mii->priv, 0x1f, 0x10, mk_high_addr(reg));
  260. lo = (u32) ag71xx_mdio_mii_read(mii->priv, phy_addr, phy_reg);
  261. hi = (u32) ag71xx_mdio_mii_read(mii->priv, phy_addr, phy_reg + 1);
  262. local_irq_restore(flags);
  263. return (hi << 16) | lo;
  264. }
  265. static void __ar7240sw_reg_write(struct mii_bus *mii, u32 reg, u32 val)
  266. {
  267. unsigned long flags;
  268. u16 phy_addr;
  269. u16 phy_reg;
  270. reg = (reg & 0xfffffffc) >> 2;
  271. phy_addr = mk_phy_addr(reg);
  272. phy_reg = mk_phy_reg(reg);
  273. local_irq_save(flags);
  274. ag71xx_mdio_mii_write(mii->priv, 0x1f, 0x10, mk_high_addr(reg));
  275. ag71xx_mdio_mii_write(mii->priv, phy_addr, phy_reg + 1, (val >> 16));
  276. ag71xx_mdio_mii_write(mii->priv, phy_addr, phy_reg, (val & 0xffff));
  277. local_irq_restore(flags);
  278. }
  279. static u32 ar7240sw_reg_read(struct mii_bus *mii, u32 reg_addr)
  280. {
  281. u32 ret;
  282. mutex_lock(&reg_mutex);
  283. ret = __ar7240sw_reg_read(mii, reg_addr);
  284. mutex_unlock(&reg_mutex);
  285. return ret;
  286. }
  287. static void ar7240sw_reg_write(struct mii_bus *mii, u32 reg_addr, u32 reg_val)
  288. {
  289. mutex_lock(&reg_mutex);
  290. __ar7240sw_reg_write(mii, reg_addr, reg_val);
  291. mutex_unlock(&reg_mutex);
  292. }
  293. static u32 ar7240sw_reg_rmw(struct mii_bus *mii, u32 reg, u32 mask, u32 val)
  294. {
  295. u32 t;
  296. mutex_lock(&reg_mutex);
  297. t = __ar7240sw_reg_read(mii, reg);
  298. t &= ~mask;
  299. t |= val;
  300. __ar7240sw_reg_write(mii, reg, t);
  301. mutex_unlock(&reg_mutex);
  302. return t;
  303. }
  304. static void ar7240sw_reg_set(struct mii_bus *mii, u32 reg, u32 val)
  305. {
  306. u32 t;
  307. mutex_lock(&reg_mutex);
  308. t = __ar7240sw_reg_read(mii, reg);
  309. t |= val;
  310. __ar7240sw_reg_write(mii, reg, t);
  311. mutex_unlock(&reg_mutex);
  312. }
  313. static int __ar7240sw_reg_wait(struct mii_bus *mii, u32 reg, u32 mask, u32 val,
  314. unsigned timeout)
  315. {
  316. int i;
  317. for (i = 0; i < timeout; i++) {
  318. u32 t;
  319. t = __ar7240sw_reg_read(mii, reg);
  320. if ((t & mask) == val)
  321. return 0;
  322. msleep(1);
  323. }
  324. return -ETIMEDOUT;
  325. }
  326. static int ar7240sw_reg_wait(struct mii_bus *mii, u32 reg, u32 mask, u32 val,
  327. unsigned timeout)
  328. {
  329. int ret;
  330. mutex_lock(&reg_mutex);
  331. ret = __ar7240sw_reg_wait(mii, reg, mask, val, timeout);
  332. mutex_unlock(&reg_mutex);
  333. return ret;
  334. }
  335. u16 ar7240sw_phy_read(struct mii_bus *mii, unsigned phy_addr,
  336. unsigned reg_addr)
  337. {
  338. u32 t, val = 0xffff;
  339. int err;
  340. if (phy_addr >= AR7240_NUM_PHYS)
  341. return 0xffff;
  342. mutex_lock(&reg_mutex);
  343. t = (reg_addr << AR7240_MDIO_CTRL_REG_ADDR_S) |
  344. (phy_addr << AR7240_MDIO_CTRL_PHY_ADDR_S) |
  345. AR7240_MDIO_CTRL_MASTER_EN |
  346. AR7240_MDIO_CTRL_BUSY |
  347. AR7240_MDIO_CTRL_CMD_READ;
  348. __ar7240sw_reg_write(mii, AR7240_REG_MDIO_CTRL, t);
  349. err = __ar7240sw_reg_wait(mii, AR7240_REG_MDIO_CTRL,
  350. AR7240_MDIO_CTRL_BUSY, 0, 5);
  351. if (!err)
  352. val = __ar7240sw_reg_read(mii, AR7240_REG_MDIO_CTRL);
  353. mutex_unlock(&reg_mutex);
  354. return val & AR7240_MDIO_CTRL_DATA_M;
  355. }
  356. int ar7240sw_phy_write(struct mii_bus *mii, unsigned phy_addr,
  357. unsigned reg_addr, u16 reg_val)
  358. {
  359. u32 t;
  360. int ret;
  361. if (phy_addr >= AR7240_NUM_PHYS)
  362. return -EINVAL;
  363. mutex_lock(&reg_mutex);
  364. t = (phy_addr << AR7240_MDIO_CTRL_PHY_ADDR_S) |
  365. (reg_addr << AR7240_MDIO_CTRL_REG_ADDR_S) |
  366. AR7240_MDIO_CTRL_MASTER_EN |
  367. AR7240_MDIO_CTRL_BUSY |
  368. AR7240_MDIO_CTRL_CMD_WRITE |
  369. reg_val;
  370. __ar7240sw_reg_write(mii, AR7240_REG_MDIO_CTRL, t);
  371. ret = __ar7240sw_reg_wait(mii, AR7240_REG_MDIO_CTRL,
  372. AR7240_MDIO_CTRL_BUSY, 0, 5);
  373. mutex_unlock(&reg_mutex);
  374. return ret;
  375. }
  376. static void ar7240sw_disable_port(struct ar7240sw *as, unsigned port)
  377. {
  378. ar7240sw_reg_write(as->mii_bus, AR7240_REG_PORT_CTRL(port),
  379. AR7240_PORT_CTRL_STATE_DISABLED);
  380. }
  381. static void ar7240sw_setup(struct ar7240sw *as)
  382. {
  383. struct mii_bus *mii = as->mii_bus;
  384. /* Enable CPU port, and disable mirror port */
  385. ar7240sw_reg_write(mii, AR7240_REG_CPU_PORT,
  386. AR7240_CPU_PORT_EN |
  387. (15 << AR7240_MIRROR_PORT_S));
  388. /* Setup TAG priority mapping */
  389. ar7240sw_reg_write(mii, AR7240_REG_TAG_PRIORITY, 0xfa50);
  390. /* Enable ARP frame acknowledge, aging, MAC replacing */
  391. ar7240sw_reg_write(mii, AR7240_REG_AT_CTRL,
  392. AR7240_AT_CTRL_RESERVED |
  393. 0x2b /* 5 min age time */ |
  394. AR7240_AT_CTRL_AGE_EN |
  395. AR7240_AT_CTRL_ARP_EN |
  396. AR7240_AT_CTRL_LEARN_CHANGE);
  397. /* Enable Broadcast frames transmitted to the CPU */
  398. ar7240sw_reg_set(mii, AR7240_REG_FLOOD_MASK,
  399. AR7240_FLOOD_MASK_BROAD_TO_CPU);
  400. /* setup MTU */
  401. ar7240sw_reg_rmw(mii, AR7240_REG_GLOBAL_CTRL, AR7240_GLOBAL_CTRL_MTU_M,
  402. 1536);
  403. /* setup Service TAG */
  404. ar7240sw_reg_rmw(mii, AR7240_REG_SERVICE_TAG, AR7240_SERVICE_TAG_M, 0);
  405. }
  406. static int ar7240sw_reset(struct ar7240sw *as)
  407. {
  408. struct mii_bus *mii = as->mii_bus;
  409. int ret;
  410. int i;
  411. /* Set all ports to disabled state. */
  412. for (i = 0; i < AR7240_NUM_PORTS; i++)
  413. ar7240sw_disable_port(as, i);
  414. /* Wait for transmit queues to drain. */
  415. msleep(2);
  416. /* Reset the switch. */
  417. ar7240sw_reg_write(mii, AR7240_REG_MASK_CTRL,
  418. AR7240_MASK_CTRL_SOFT_RESET);
  419. ret = ar7240sw_reg_wait(mii, AR7240_REG_MASK_CTRL,
  420. AR7240_MASK_CTRL_SOFT_RESET, 0, 1000);
  421. ar7240sw_setup(as);
  422. return ret;
  423. }
  424. static void ar7240sw_setup_port(struct ar7240sw *as, unsigned port, u8 portmask)
  425. {
  426. struct mii_bus *mii = as->mii_bus;
  427. u32 ctrl;
  428. u32 vid, mode;
  429. ctrl = AR7240_PORT_CTRL_STATE_FORWARD | AR7240_PORT_CTRL_LEARN |
  430. AR7240_PORT_CTRL_SINGLE_VLAN;
  431. if (port == AR7240_PORT_CPU) {
  432. ar7240sw_reg_write(mii, AR7240_REG_PORT_STATUS(port),
  433. AR7240_PORT_STATUS_SPEED_1000 |
  434. AR7240_PORT_STATUS_TXFLOW |
  435. AR7240_PORT_STATUS_RXFLOW |
  436. AR7240_PORT_STATUS_TXMAC |
  437. AR7240_PORT_STATUS_RXMAC |
  438. AR7240_PORT_STATUS_DUPLEX);
  439. } else {
  440. ar7240sw_reg_write(mii, AR7240_REG_PORT_STATUS(port),
  441. AR7240_PORT_STATUS_LINK_AUTO);
  442. }
  443. /* Set the default VID for this port */
  444. if (as->vlan) {
  445. vid = as->vlan_id[as->pvid[port]];
  446. mode = AR7240_PORT_VLAN_MODE_SECURE;
  447. } else {
  448. vid = port;
  449. mode = AR7240_PORT_VLAN_MODE_PORT_ONLY;
  450. }
  451. if (as->vlan && (as->vlan_tagged & BIT(port))) {
  452. ctrl |= AR7240_PORT_CTRL_VLAN_MODE_ADD <<
  453. AR7240_PORT_CTRL_VLAN_MODE_S;
  454. } else {
  455. ctrl |= AR7240_PORT_CTRL_VLAN_MODE_STRIP <<
  456. AR7240_PORT_CTRL_VLAN_MODE_S;
  457. }
  458. if (!portmask) {
  459. if (port == AR7240_PORT_CPU)
  460. portmask = ar7240sw_port_mask_but(as, AR7240_PORT_CPU);
  461. else
  462. portmask = ar7240sw_port_mask(as, AR7240_PORT_CPU);
  463. }
  464. /* allow the port to talk to all other ports, but exclude its
  465. * own ID to prevent frames from being reflected back to the
  466. * port that they came from */
  467. portmask &= ar7240sw_port_mask_but(as, port);
  468. ar7240sw_reg_write(mii, AR7240_REG_PORT_CTRL(port), ctrl);
  469. if (sw_is_ar934x(as)) {
  470. u32 vlan1, vlan2;
  471. vlan1 = (vid << AR934X_PORT_VLAN1_DEFAULT_CVID_S);
  472. vlan2 = (portmask << AR934X_PORT_VLAN2_PORT_VID_MEM_S) |
  473. (mode << AR934X_PORT_VLAN2_8021Q_MODE_S);
  474. ar7240sw_reg_write(mii, AR934X_REG_PORT_VLAN1(port), vlan1);
  475. ar7240sw_reg_write(mii, AR934X_REG_PORT_VLAN2(port), vlan2);
  476. } else {
  477. u32 vlan;
  478. vlan = vid | (mode << AR7240_PORT_VLAN_MODE_S) |
  479. (portmask << AR7240_PORT_VLAN_DEST_PORTS_S);
  480. ar7240sw_reg_write(mii, AR7240_REG_PORT_VLAN(port), vlan);
  481. }
  482. }
  483. static int ar7240_set_addr(struct ar7240sw *as, u8 *addr)
  484. {
  485. struct mii_bus *mii = as->mii_bus;
  486. u32 t;
  487. t = (addr[4] << 8) | addr[5];
  488. ar7240sw_reg_write(mii, AR7240_REG_MAC_ADDR0, t);
  489. t = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
  490. ar7240sw_reg_write(mii, AR7240_REG_MAC_ADDR1, t);
  491. return 0;
  492. }
  493. static int
  494. ar7240_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
  495. struct switch_val *val)
  496. {
  497. struct ar7240sw *as = sw_to_ar7240(dev);
  498. as->vlan_id[val->port_vlan] = val->value.i;
  499. return 0;
  500. }
  501. static int
  502. ar7240_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
  503. struct switch_val *val)
  504. {
  505. struct ar7240sw *as = sw_to_ar7240(dev);
  506. val->value.i = as->vlan_id[val->port_vlan];
  507. return 0;
  508. }
  509. static int
  510. ar7240_set_pvid(struct switch_dev *dev, int port, int vlan)
  511. {
  512. struct ar7240sw *as = sw_to_ar7240(dev);
  513. /* make sure no invalid PVIDs get set */
  514. if (vlan >= dev->vlans)
  515. return -EINVAL;
  516. as->pvid[port] = vlan;
  517. return 0;
  518. }
  519. static int
  520. ar7240_get_pvid(struct switch_dev *dev, int port, int *vlan)
  521. {
  522. struct ar7240sw *as = sw_to_ar7240(dev);
  523. *vlan = as->pvid[port];
  524. return 0;
  525. }
  526. static int
  527. ar7240_get_ports(struct switch_dev *dev, struct switch_val *val)
  528. {
  529. struct ar7240sw *as = sw_to_ar7240(dev);
  530. u8 ports = as->vlan_table[val->port_vlan];
  531. int i;
  532. val->len = 0;
  533. for (i = 0; i < as->swdev.ports; i++) {
  534. struct switch_port *p;
  535. if (!(ports & (1 << i)))
  536. continue;
  537. p = &val->value.ports[val->len++];
  538. p->id = i;
  539. if (as->vlan_tagged & (1 << i))
  540. p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
  541. else
  542. p->flags = 0;
  543. }
  544. return 0;
  545. }
  546. static int
  547. ar7240_set_ports(struct switch_dev *dev, struct switch_val *val)
  548. {
  549. struct ar7240sw *as = sw_to_ar7240(dev);
  550. u8 *vt = &as->vlan_table[val->port_vlan];
  551. int i, j;
  552. *vt = 0;
  553. for (i = 0; i < val->len; i++) {
  554. struct switch_port *p = &val->value.ports[i];
  555. if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
  556. as->vlan_tagged |= (1 << p->id);
  557. else {
  558. as->vlan_tagged &= ~(1 << p->id);
  559. as->pvid[p->id] = val->port_vlan;
  560. /* make sure that an untagged port does not
  561. * appear in other vlans */
  562. for (j = 0; j < AR7240_MAX_VLANS; j++) {
  563. if (j == val->port_vlan)
  564. continue;
  565. as->vlan_table[j] &= ~(1 << p->id);
  566. }
  567. }
  568. *vt |= 1 << p->id;
  569. }
  570. return 0;
  571. }
  572. static int
  573. ar7240_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  574. struct switch_val *val)
  575. {
  576. struct ar7240sw *as = sw_to_ar7240(dev);
  577. as->vlan = !!val->value.i;
  578. return 0;
  579. }
  580. static int
  581. ar7240_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
  582. struct switch_val *val)
  583. {
  584. struct ar7240sw *as = sw_to_ar7240(dev);
  585. val->value.i = as->vlan;
  586. return 0;
  587. }
  588. static const char *
  589. ar7240_speed_str(u32 status)
  590. {
  591. u32 speed;
  592. speed = (status >> AR7240_PORT_STATUS_SPEED_S) &
  593. AR7240_PORT_STATUS_SPEED_M;
  594. switch (speed) {
  595. case AR7240_PORT_STATUS_SPEED_10:
  596. return "10baseT";
  597. case AR7240_PORT_STATUS_SPEED_100:
  598. return "100baseT";
  599. case AR7240_PORT_STATUS_SPEED_1000:
  600. return "1000baseT";
  601. }
  602. return "unknown";
  603. }
  604. static int
  605. ar7240_port_get_link(struct switch_dev *dev, const struct switch_attr *attr,
  606. struct switch_val *val)
  607. {
  608. struct ar7240sw *as = sw_to_ar7240(dev);
  609. struct mii_bus *mii = as->mii_bus;
  610. u32 len;
  611. u32 status;
  612. int port;
  613. port = val->port_vlan;
  614. memset(as->buf, '\0', sizeof(as->buf));
  615. status = ar7240sw_reg_read(mii, AR7240_REG_PORT_STATUS(port));
  616. if (status & AR7240_PORT_STATUS_LINK_UP) {
  617. len = snprintf(as->buf, sizeof(as->buf),
  618. "port:%d link:up speed:%s %s-duplex %s%s%s",
  619. port,
  620. ar7240_speed_str(status),
  621. (status & AR7240_PORT_STATUS_DUPLEX) ?
  622. "full" : "half",
  623. (status & AR7240_PORT_STATUS_TXFLOW) ?
  624. "txflow ": "",
  625. (status & AR7240_PORT_STATUS_RXFLOW) ?
  626. "rxflow " : "",
  627. (status & AR7240_PORT_STATUS_LINK_AUTO) ?
  628. "auto ": "");
  629. } else {
  630. len = snprintf(as->buf, sizeof(as->buf),
  631. "port:%d link:down", port);
  632. }
  633. val->value.s = as->buf;
  634. val->len = len;
  635. return 0;
  636. }
  637. static void
  638. ar7240_vtu_op(struct ar7240sw *as, u32 op, u32 val)
  639. {
  640. struct mii_bus *mii = as->mii_bus;
  641. if (ar7240sw_reg_wait(mii, AR7240_REG_VTU, AR7240_VTU_ACTIVE, 0, 5))
  642. return;
  643. if ((op & AR7240_VTU_OP) == AR7240_VTU_OP_LOAD) {
  644. val &= AR7240_VTUDATA_MEMBER;
  645. val |= AR7240_VTUDATA_VALID;
  646. ar7240sw_reg_write(mii, AR7240_REG_VTU_DATA, val);
  647. }
  648. op |= AR7240_VTU_ACTIVE;
  649. ar7240sw_reg_write(mii, AR7240_REG_VTU, op);
  650. }
  651. static int
  652. ar7240_hw_apply(struct switch_dev *dev)
  653. {
  654. struct ar7240sw *as = sw_to_ar7240(dev);
  655. u8 portmask[AR7240_NUM_PORTS];
  656. int i, j;
  657. /* flush all vlan translation unit entries */
  658. ar7240_vtu_op(as, AR7240_VTU_OP_FLUSH, 0);
  659. memset(portmask, 0, sizeof(portmask));
  660. if (as->vlan) {
  661. /* calculate the port destination masks and load vlans
  662. * into the vlan translation unit */
  663. for (j = 0; j < AR7240_MAX_VLANS; j++) {
  664. u8 vp = as->vlan_table[j];
  665. if (!vp)
  666. continue;
  667. for (i = 0; i < as->swdev.ports; i++) {
  668. u8 mask = (1 << i);
  669. if (vp & mask)
  670. portmask[i] |= vp & ~mask;
  671. }
  672. ar7240_vtu_op(as,
  673. AR7240_VTU_OP_LOAD |
  674. (as->vlan_id[j] << AR7240_VTU_VID_S),
  675. as->vlan_table[j]);
  676. }
  677. } else {
  678. /* vlan disabled:
  679. * isolate all ports, but connect them to the cpu port */
  680. for (i = 0; i < as->swdev.ports; i++) {
  681. if (i == AR7240_PORT_CPU)
  682. continue;
  683. portmask[i] = 1 << AR7240_PORT_CPU;
  684. portmask[AR7240_PORT_CPU] |= (1 << i);
  685. }
  686. }
  687. /* update the port destination mask registers and tag settings */
  688. for (i = 0; i < as->swdev.ports; i++)
  689. ar7240sw_setup_port(as, i, portmask[i]);
  690. return 0;
  691. }
  692. static int
  693. ar7240_reset_switch(struct switch_dev *dev)
  694. {
  695. struct ar7240sw *as = sw_to_ar7240(dev);
  696. ar7240sw_reset(as);
  697. return 0;
  698. }
  699. static struct switch_attr ar7240_globals[] = {
  700. {
  701. .type = SWITCH_TYPE_INT,
  702. .name = "enable_vlan",
  703. .description = "Enable VLAN mode",
  704. .set = ar7240_set_vlan,
  705. .get = ar7240_get_vlan,
  706. .max = 1
  707. },
  708. };
  709. static struct switch_attr ar7240_port[] = {
  710. {
  711. .type = SWITCH_TYPE_STRING,
  712. .name = "link",
  713. .description = "Get port link information",
  714. .max = 1,
  715. .set = NULL,
  716. .get = ar7240_port_get_link,
  717. },
  718. };
  719. static struct switch_attr ar7240_vlan[] = {
  720. {
  721. .type = SWITCH_TYPE_INT,
  722. .name = "vid",
  723. .description = "VLAN ID",
  724. .set = ar7240_set_vid,
  725. .get = ar7240_get_vid,
  726. .max = 4094,
  727. },
  728. };
  729. static const struct switch_dev_ops ar7240_ops = {
  730. .attr_global = {
  731. .attr = ar7240_globals,
  732. .n_attr = ARRAY_SIZE(ar7240_globals),
  733. },
  734. .attr_port = {
  735. .attr = ar7240_port,
  736. .n_attr = ARRAY_SIZE(ar7240_port),
  737. },
  738. .attr_vlan = {
  739. .attr = ar7240_vlan,
  740. .n_attr = ARRAY_SIZE(ar7240_vlan),
  741. },
  742. .get_port_pvid = ar7240_get_pvid,
  743. .set_port_pvid = ar7240_set_pvid,
  744. .get_vlan_ports = ar7240_get_ports,
  745. .set_vlan_ports = ar7240_set_ports,
  746. .apply_config = ar7240_hw_apply,
  747. .reset_switch = ar7240_reset_switch,
  748. };
  749. static struct ar7240sw *ar7240_probe(struct ag71xx *ag)
  750. {
  751. struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
  752. struct mii_bus *mii = ag->mii_bus;
  753. struct ar7240sw *as;
  754. struct switch_dev *swdev;
  755. u32 ctrl;
  756. u16 phy_id1;
  757. u16 phy_id2;
  758. int i;
  759. phy_id1 = ar7240sw_phy_read(mii, 0, MII_PHYSID1);
  760. phy_id2 = ar7240sw_phy_read(mii, 0, MII_PHYSID2);
  761. if ((phy_id1 != AR7240_PHY_ID1 || phy_id2 != AR7240_PHY_ID2) &&
  762. (phy_id1 != AR934X_PHY_ID1 || phy_id2 != AR934X_PHY_ID2)) {
  763. pr_err("%s: unknown phy id '%04x:%04x'\n",
  764. ag->dev->name, phy_id1, phy_id2);
  765. return NULL;
  766. }
  767. as = kzalloc(sizeof(*as), GFP_KERNEL);
  768. if (!as)
  769. return NULL;
  770. as->mii_bus = mii;
  771. as->swdata = pdata->switch_data;
  772. swdev = &as->swdev;
  773. ctrl = ar7240sw_reg_read(mii, AR7240_REG_MASK_CTRL);
  774. as->ver = (ctrl >> AR7240_MASK_CTRL_VERSION_S) &
  775. AR7240_MASK_CTRL_VERSION_M;
  776. if (sw_is_ar7240(as)) {
  777. swdev->name = "AR7240/AR9330 built-in switch";
  778. } else if (sw_is_ar934x(as)) {
  779. swdev->name = "AR934X built-in switch";
  780. if (pdata->phy_if_mode == PHY_INTERFACE_MODE_GMII) {
  781. ar7240sw_reg_set(mii, AR934X_REG_OPER_MODE0,
  782. AR934X_OPER_MODE0_MAC_GMII_EN);
  783. } else if (pdata->phy_if_mode == PHY_INTERFACE_MODE_MII) {
  784. ar7240sw_reg_set(mii, AR934X_REG_OPER_MODE0,
  785. AR934X_OPER_MODE0_PHY_MII_EN);
  786. } else {
  787. pr_err("%s: invalid PHY interface mode\n",
  788. ag->dev->name);
  789. goto err_free;
  790. }
  791. if (as->swdata->phy4_mii_en)
  792. ar7240sw_reg_set(mii, AR934X_REG_OPER_MODE1,
  793. AR934X_REG_OPER_MODE1_PHY4_MII_EN);
  794. } else {
  795. pr_err("%s: unsupported chip, ctrl=%08x\n",
  796. ag->dev->name, ctrl);
  797. goto err_free;
  798. }
  799. swdev->ports = AR7240_NUM_PORTS - 1;
  800. swdev->cpu_port = AR7240_PORT_CPU;
  801. swdev->vlans = AR7240_MAX_VLANS;
  802. swdev->ops = &ar7240_ops;
  803. if (register_switch(&as->swdev, ag->dev) < 0)
  804. goto err_free;
  805. pr_info("%s: Found an %s\n", ag->dev->name, swdev->name);
  806. /* initialize defaults */
  807. for (i = 0; i < AR7240_MAX_VLANS; i++)
  808. as->vlan_id[i] = i;
  809. as->vlan_table[0] = ar7240sw_port_mask_all(as);
  810. return as;
  811. err_free:
  812. kfree(as);
  813. return NULL;
  814. }
  815. static void link_function(struct work_struct *work) {
  816. struct ag71xx *ag = container_of(work, struct ag71xx, link_work.work);
  817. unsigned long flags;
  818. int i;
  819. int status = 0;
  820. for (i = 0; i < 4; i++) {
  821. int link = ar7240sw_phy_read(ag->mii_bus, i, MII_BMSR);
  822. if(link & BMSR_LSTATUS) {
  823. status = 1;
  824. break;
  825. }
  826. }
  827. spin_lock_irqsave(&ag->lock, flags);
  828. if(status != ag->link) {
  829. ag->link = status;
  830. ag71xx_link_adjust(ag);
  831. }
  832. spin_unlock_irqrestore(&ag->lock, flags);
  833. schedule_delayed_work(&ag->link_work, HZ / 2);
  834. }
  835. void ag71xx_ar7240_start(struct ag71xx *ag)
  836. {
  837. struct ar7240sw *as = ag->phy_priv;
  838. ar7240sw_reset(as);
  839. ag->speed = SPEED_1000;
  840. ag->duplex = 1;
  841. ar7240_set_addr(as, ag->dev->dev_addr);
  842. ar7240_hw_apply(&as->swdev);
  843. schedule_delayed_work(&ag->link_work, HZ / 10);
  844. }
  845. void ag71xx_ar7240_stop(struct ag71xx *ag)
  846. {
  847. cancel_delayed_work_sync(&ag->link_work);
  848. }
  849. int __devinit ag71xx_ar7240_init(struct ag71xx *ag)
  850. {
  851. struct ar7240sw *as;
  852. as = ar7240_probe(ag);
  853. if (!as)
  854. return -ENODEV;
  855. ag->phy_priv = as;
  856. ar7240sw_reset(as);
  857. INIT_DELAYED_WORK(&ag->link_work, link_function);
  858. return 0;
  859. }
  860. void ag71xx_ar7240_cleanup(struct ag71xx *ag)
  861. {
  862. struct ar7240sw *as = ag->phy_priv;
  863. if (!as)
  864. return;
  865. unregister_switch(&as->swdev);
  866. kfree(as);
  867. ag->phy_priv = NULL;
  868. }