cpsw.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273
  1. /*
  2. * Texas Instruments Ethernet Switch Driver
  3. *
  4. * Copyright (C) 2012 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/io.h>
  17. #include <linux/clk.h>
  18. #include <linux/timer.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/irqreturn.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/if_ether.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/net_tstamp.h>
  27. #include <linux/phy.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/delay.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/of.h>
  32. #include <linux/of_net.h>
  33. #include <linux/of_device.h>
  34. #include <linux/if_vlan.h>
  35. #include <linux/platform_data/cpsw.h>
  36. #include <linux/pinctrl/consumer.h>
  37. #include "cpsw_ale.h"
  38. #include "cpts.h"
  39. #include "davinci_cpdma.h"
  40. #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
  41. NETIF_MSG_DRV | NETIF_MSG_LINK | \
  42. NETIF_MSG_IFUP | NETIF_MSG_INTR | \
  43. NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
  44. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
  45. NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
  46. NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
  47. NETIF_MSG_RX_STATUS)
  48. #define cpsw_info(priv, type, format, ...) \
  49. do { \
  50. if (netif_msg_##type(priv) && net_ratelimit()) \
  51. dev_info(priv->dev, format, ## __VA_ARGS__); \
  52. } while (0)
  53. #define cpsw_err(priv, type, format, ...) \
  54. do { \
  55. if (netif_msg_##type(priv) && net_ratelimit()) \
  56. dev_err(priv->dev, format, ## __VA_ARGS__); \
  57. } while (0)
  58. #define cpsw_dbg(priv, type, format, ...) \
  59. do { \
  60. if (netif_msg_##type(priv) && net_ratelimit()) \
  61. dev_dbg(priv->dev, format, ## __VA_ARGS__); \
  62. } while (0)
  63. #define cpsw_notice(priv, type, format, ...) \
  64. do { \
  65. if (netif_msg_##type(priv) && net_ratelimit()) \
  66. dev_notice(priv->dev, format, ## __VA_ARGS__); \
  67. } while (0)
  68. #define ALE_ALL_PORTS 0x7
  69. #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
  70. #define CPSW_MINOR_VERSION(reg) (reg & 0xff)
  71. #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
  72. #define CPSW_VERSION_1 0x19010a
  73. #define CPSW_VERSION_2 0x19010c
  74. #define HOST_PORT_NUM 0
  75. #define SLIVER_SIZE 0x40
  76. #define CPSW1_HOST_PORT_OFFSET 0x028
  77. #define CPSW1_SLAVE_OFFSET 0x050
  78. #define CPSW1_SLAVE_SIZE 0x040
  79. #define CPSW1_CPDMA_OFFSET 0x100
  80. #define CPSW1_STATERAM_OFFSET 0x200
  81. #define CPSW1_HW_STATS 0x400
  82. #define CPSW1_CPTS_OFFSET 0x500
  83. #define CPSW1_ALE_OFFSET 0x600
  84. #define CPSW1_SLIVER_OFFSET 0x700
  85. #define CPSW2_HOST_PORT_OFFSET 0x108
  86. #define CPSW2_SLAVE_OFFSET 0x200
  87. #define CPSW2_SLAVE_SIZE 0x100
  88. #define CPSW2_CPDMA_OFFSET 0x800
  89. #define CPSW2_HW_STATS 0x900
  90. #define CPSW2_STATERAM_OFFSET 0xa00
  91. #define CPSW2_CPTS_OFFSET 0xc00
  92. #define CPSW2_ALE_OFFSET 0xd00
  93. #define CPSW2_SLIVER_OFFSET 0xd80
  94. #define CPSW2_BD_OFFSET 0x2000
  95. #define CPDMA_RXTHRESH 0x0c0
  96. #define CPDMA_RXFREE 0x0e0
  97. #define CPDMA_TXHDP 0x00
  98. #define CPDMA_RXHDP 0x20
  99. #define CPDMA_TXCP 0x40
  100. #define CPDMA_RXCP 0x60
  101. #define CPSW_POLL_WEIGHT 64
  102. #define CPSW_MIN_PACKET_SIZE 60
  103. #define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
  104. #define RX_PRIORITY_MAPPING 0x76543210
  105. #define TX_PRIORITY_MAPPING 0x33221100
  106. #define CPDMA_TX_PRIORITY_MAP 0x76543210
  107. #define CPSW_VLAN_AWARE BIT(1)
  108. #define CPSW_ALE_VLAN_AWARE 1
  109. #define CPSW_FIFO_NORMAL_MODE (0 << 15)
  110. #define CPSW_FIFO_DUAL_MAC_MODE (1 << 15)
  111. #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 15)
  112. #define CPSW_INTPACEEN (0x3f << 16)
  113. #define CPSW_INTPRESCALE_MASK (0x7FF << 0)
  114. #define CPSW_CMINTMAX_CNT 63
  115. #define CPSW_CMINTMIN_CNT 2
  116. #define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT)
  117. #define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1)
  118. #define cpsw_enable_irq(priv) \
  119. do { \
  120. u32 i; \
  121. for (i = 0; i < priv->num_irqs; i++) \
  122. enable_irq(priv->irqs_table[i]); \
  123. } while (0);
  124. #define cpsw_disable_irq(priv) \
  125. do { \
  126. u32 i; \
  127. for (i = 0; i < priv->num_irqs; i++) \
  128. disable_irq_nosync(priv->irqs_table[i]); \
  129. } while (0);
  130. #define cpsw_slave_index(priv) \
  131. ((priv->data.dual_emac) ? priv->emac_port : \
  132. priv->data.active_slave)
  133. static int debug_level;
  134. module_param(debug_level, int, 0);
  135. MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
  136. static int ale_ageout = 10;
  137. module_param(ale_ageout, int, 0);
  138. MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
  139. static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
  140. module_param(rx_packet_max, int, 0);
  141. MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
  142. struct cpsw_wr_regs {
  143. u32 id_ver;
  144. u32 soft_reset;
  145. u32 control;
  146. u32 int_control;
  147. u32 rx_thresh_en;
  148. u32 rx_en;
  149. u32 tx_en;
  150. u32 misc_en;
  151. u32 mem_allign1[8];
  152. u32 rx_thresh_stat;
  153. u32 rx_stat;
  154. u32 tx_stat;
  155. u32 misc_stat;
  156. u32 mem_allign2[8];
  157. u32 rx_imax;
  158. u32 tx_imax;
  159. };
  160. struct cpsw_ss_regs {
  161. u32 id_ver;
  162. u32 control;
  163. u32 soft_reset;
  164. u32 stat_port_en;
  165. u32 ptype;
  166. u32 soft_idle;
  167. u32 thru_rate;
  168. u32 gap_thresh;
  169. u32 tx_start_wds;
  170. u32 flow_control;
  171. u32 vlan_ltype;
  172. u32 ts_ltype;
  173. u32 dlr_ltype;
  174. };
  175. /* CPSW_PORT_V1 */
  176. #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
  177. #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
  178. #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
  179. #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
  180. #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
  181. #define CPSW1_TS_CTL 0x14 /* Time Sync Control */
  182. #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
  183. #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
  184. /* CPSW_PORT_V2 */
  185. #define CPSW2_CONTROL 0x00 /* Control Register */
  186. #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
  187. #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
  188. #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
  189. #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
  190. #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
  191. #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
  192. /* CPSW_PORT_V1 and V2 */
  193. #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
  194. #define SA_HI 0x24 /* CPGMAC_SL Source Address High */
  195. #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
  196. /* CPSW_PORT_V2 only */
  197. #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
  198. #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
  199. #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
  200. #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
  201. #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
  202. #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
  203. #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
  204. #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
  205. /* Bit definitions for the CPSW2_CONTROL register */
  206. #define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
  207. #define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
  208. #define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
  209. #define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
  210. #define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
  211. #define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
  212. #define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
  213. #define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
  214. #define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
  215. #define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
  216. #define TS_BIT8 (1<<8) /* ts_ttl_nonzero? */
  217. #define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
  218. #define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
  219. #define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
  220. #define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
  221. #define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
  222. #define CTRL_TS_BITS \
  223. (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 | TS_BIT8 | \
  224. TS_ANNEX_D_EN | TS_LTYPE1_EN)
  225. #define CTRL_ALL_TS_MASK (CTRL_TS_BITS | TS_TX_EN | TS_RX_EN)
  226. #define CTRL_TX_TS_BITS (CTRL_TS_BITS | TS_TX_EN)
  227. #define CTRL_RX_TS_BITS (CTRL_TS_BITS | TS_RX_EN)
  228. /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
  229. #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
  230. #define TS_SEQ_ID_OFFSET_MASK (0x3f)
  231. #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
  232. #define TS_MSG_TYPE_EN_MASK (0xffff)
  233. /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
  234. #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
  235. /* Bit definitions for the CPSW1_TS_CTL register */
  236. #define CPSW_V1_TS_RX_EN BIT(0)
  237. #define CPSW_V1_TS_TX_EN BIT(4)
  238. #define CPSW_V1_MSG_TYPE_OFS 16
  239. /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
  240. #define CPSW_V1_SEQ_ID_OFS_SHIFT 16
  241. struct cpsw_host_regs {
  242. u32 max_blks;
  243. u32 blk_cnt;
  244. u32 tx_in_ctl;
  245. u32 port_vlan;
  246. u32 tx_pri_map;
  247. u32 cpdma_tx_pri_map;
  248. u32 cpdma_rx_chan_map;
  249. };
  250. struct cpsw_sliver_regs {
  251. u32 id_ver;
  252. u32 mac_control;
  253. u32 mac_status;
  254. u32 soft_reset;
  255. u32 rx_maxlen;
  256. u32 __reserved_0;
  257. u32 rx_pause;
  258. u32 tx_pause;
  259. u32 __reserved_1;
  260. u32 rx_pri_map;
  261. };
  262. struct cpsw_hw_stats {
  263. u32 rxgoodframes;
  264. u32 rxbroadcastframes;
  265. u32 rxmulticastframes;
  266. u32 rxpauseframes;
  267. u32 rxcrcerrors;
  268. u32 rxaligncodeerrors;
  269. u32 rxoversizedframes;
  270. u32 rxjabberframes;
  271. u32 rxundersizedframes;
  272. u32 rxfragments;
  273. u32 __pad_0[2];
  274. u32 rxoctets;
  275. u32 txgoodframes;
  276. u32 txbroadcastframes;
  277. u32 txmulticastframes;
  278. u32 txpauseframes;
  279. u32 txdeferredframes;
  280. u32 txcollisionframes;
  281. u32 txsinglecollframes;
  282. u32 txmultcollframes;
  283. u32 txexcessivecollisions;
  284. u32 txlatecollisions;
  285. u32 txunderrun;
  286. u32 txcarriersenseerrors;
  287. u32 txoctets;
  288. u32 octetframes64;
  289. u32 octetframes65t127;
  290. u32 octetframes128t255;
  291. u32 octetframes256t511;
  292. u32 octetframes512t1023;
  293. u32 octetframes1024tup;
  294. u32 netoctets;
  295. u32 rxsofoverruns;
  296. u32 rxmofoverruns;
  297. u32 rxdmaoverruns;
  298. };
  299. struct cpsw_slave {
  300. void __iomem *regs;
  301. struct cpsw_sliver_regs __iomem *sliver;
  302. int slave_num;
  303. u32 mac_control;
  304. struct cpsw_slave_data *data;
  305. struct phy_device *phy;
  306. struct net_device *ndev;
  307. u32 port_vlan;
  308. u32 open_stat;
  309. };
  310. static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
  311. {
  312. return __raw_readl(slave->regs + offset);
  313. }
  314. static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
  315. {
  316. __raw_writel(val, slave->regs + offset);
  317. }
  318. struct cpsw_priv {
  319. spinlock_t lock;
  320. struct platform_device *pdev;
  321. struct net_device *ndev;
  322. struct resource *cpsw_res;
  323. struct resource *cpsw_wr_res;
  324. struct napi_struct napi;
  325. struct device *dev;
  326. struct cpsw_platform_data data;
  327. struct cpsw_ss_regs __iomem *regs;
  328. struct cpsw_wr_regs __iomem *wr_regs;
  329. u8 __iomem *hw_stats;
  330. struct cpsw_host_regs __iomem *host_port_regs;
  331. u32 msg_enable;
  332. u32 version;
  333. u32 coal_intvl;
  334. u32 bus_freq_mhz;
  335. struct net_device_stats stats;
  336. int rx_packet_max;
  337. int host_port;
  338. struct clk *clk;
  339. u8 mac_addr[ETH_ALEN];
  340. struct cpsw_slave *slaves;
  341. struct cpdma_ctlr *dma;
  342. struct cpdma_chan *txch, *rxch;
  343. struct cpsw_ale *ale;
  344. /* snapshot of IRQ numbers */
  345. u32 irqs_table[4];
  346. u32 num_irqs;
  347. bool irq_enabled;
  348. struct cpts *cpts;
  349. u32 emac_port;
  350. };
  351. struct cpsw_stats {
  352. char stat_string[ETH_GSTRING_LEN];
  353. int type;
  354. int sizeof_stat;
  355. int stat_offset;
  356. };
  357. enum {
  358. CPSW_STATS,
  359. CPDMA_RX_STATS,
  360. CPDMA_TX_STATS,
  361. };
  362. #define CPSW_STAT(m) CPSW_STATS, \
  363. sizeof(((struct cpsw_hw_stats *)0)->m), \
  364. offsetof(struct cpsw_hw_stats, m)
  365. #define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
  366. sizeof(((struct cpdma_chan_stats *)0)->m), \
  367. offsetof(struct cpdma_chan_stats, m)
  368. #define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
  369. sizeof(((struct cpdma_chan_stats *)0)->m), \
  370. offsetof(struct cpdma_chan_stats, m)
  371. static const struct cpsw_stats cpsw_gstrings_stats[] = {
  372. { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
  373. { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
  374. { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
  375. { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
  376. { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
  377. { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
  378. { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
  379. { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
  380. { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
  381. { "Rx Fragments", CPSW_STAT(rxfragments) },
  382. { "Rx Octets", CPSW_STAT(rxoctets) },
  383. { "Good Tx Frames", CPSW_STAT(txgoodframes) },
  384. { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
  385. { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
  386. { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
  387. { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
  388. { "Collisions", CPSW_STAT(txcollisionframes) },
  389. { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
  390. { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
  391. { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
  392. { "Late Collisions", CPSW_STAT(txlatecollisions) },
  393. { "Tx Underrun", CPSW_STAT(txunderrun) },
  394. { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
  395. { "Tx Octets", CPSW_STAT(txoctets) },
  396. { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
  397. { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
  398. { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
  399. { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
  400. { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
  401. { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
  402. { "Net Octets", CPSW_STAT(netoctets) },
  403. { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
  404. { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
  405. { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
  406. { "Rx DMA chan: head_enqueue", CPDMA_RX_STAT(head_enqueue) },
  407. { "Rx DMA chan: tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
  408. { "Rx DMA chan: pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
  409. { "Rx DMA chan: misqueued", CPDMA_RX_STAT(misqueued) },
  410. { "Rx DMA chan: desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
  411. { "Rx DMA chan: pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
  412. { "Rx DMA chan: runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
  413. { "Rx DMA chan: runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
  414. { "Rx DMA chan: empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
  415. { "Rx DMA chan: busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
  416. { "Rx DMA chan: good_dequeue", CPDMA_RX_STAT(good_dequeue) },
  417. { "Rx DMA chan: requeue", CPDMA_RX_STAT(requeue) },
  418. { "Rx DMA chan: teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
  419. { "Tx DMA chan: head_enqueue", CPDMA_TX_STAT(head_enqueue) },
  420. { "Tx DMA chan: tail_enqueue", CPDMA_TX_STAT(tail_enqueue) },
  421. { "Tx DMA chan: pad_enqueue", CPDMA_TX_STAT(pad_enqueue) },
  422. { "Tx DMA chan: misqueued", CPDMA_TX_STAT(misqueued) },
  423. { "Tx DMA chan: desc_alloc_fail", CPDMA_TX_STAT(desc_alloc_fail) },
  424. { "Tx DMA chan: pad_alloc_fail", CPDMA_TX_STAT(pad_alloc_fail) },
  425. { "Tx DMA chan: runt_receive_buf", CPDMA_TX_STAT(runt_receive_buff) },
  426. { "Tx DMA chan: runt_transmit_buf", CPDMA_TX_STAT(runt_transmit_buff) },
  427. { "Tx DMA chan: empty_dequeue", CPDMA_TX_STAT(empty_dequeue) },
  428. { "Tx DMA chan: busy_dequeue", CPDMA_TX_STAT(busy_dequeue) },
  429. { "Tx DMA chan: good_dequeue", CPDMA_TX_STAT(good_dequeue) },
  430. { "Tx DMA chan: requeue", CPDMA_TX_STAT(requeue) },
  431. { "Tx DMA chan: teardown_dequeue", CPDMA_TX_STAT(teardown_dequeue) },
  432. };
  433. #define CPSW_STATS_LEN ARRAY_SIZE(cpsw_gstrings_stats)
  434. #define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
  435. #define for_each_slave(priv, func, arg...) \
  436. do { \
  437. struct cpsw_slave *slave; \
  438. int n; \
  439. if (priv->data.dual_emac) \
  440. (func)((priv)->slaves + priv->emac_port, ##arg);\
  441. else \
  442. for (n = (priv)->data.slaves, \
  443. slave = (priv)->slaves; \
  444. n; n--) \
  445. (func)(slave++, ##arg); \
  446. } while (0)
  447. #define cpsw_get_slave_ndev(priv, __slave_no__) \
  448. (priv->slaves[__slave_no__].ndev)
  449. #define cpsw_get_slave_priv(priv, __slave_no__) \
  450. ((priv->slaves[__slave_no__].ndev) ? \
  451. netdev_priv(priv->slaves[__slave_no__].ndev) : NULL) \
  452. #define cpsw_dual_emac_src_port_detect(status, priv, ndev, skb) \
  453. do { \
  454. if (!priv->data.dual_emac) \
  455. break; \
  456. if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
  457. ndev = cpsw_get_slave_ndev(priv, 0); \
  458. priv = netdev_priv(ndev); \
  459. skb->dev = ndev; \
  460. } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
  461. ndev = cpsw_get_slave_ndev(priv, 1); \
  462. priv = netdev_priv(ndev); \
  463. skb->dev = ndev; \
  464. } \
  465. } while (0)
  466. #define cpsw_add_mcast(priv, addr) \
  467. do { \
  468. if (priv->data.dual_emac) { \
  469. struct cpsw_slave *slave = priv->slaves + \
  470. priv->emac_port; \
  471. int slave_port = cpsw_get_slave_port(priv, \
  472. slave->slave_num); \
  473. cpsw_ale_add_mcast(priv->ale, addr, \
  474. 1 << slave_port | 1 << priv->host_port, \
  475. ALE_VLAN, slave->port_vlan, 0); \
  476. } else { \
  477. cpsw_ale_add_mcast(priv->ale, addr, \
  478. ALE_ALL_PORTS << priv->host_port, \
  479. 0, 0, 0); \
  480. } \
  481. } while (0)
  482. static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
  483. {
  484. if (priv->host_port == 0)
  485. return slave_num + 1;
  486. else
  487. return slave_num;
  488. }
  489. static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
  490. {
  491. struct cpsw_priv *priv = netdev_priv(ndev);
  492. if (ndev->flags & IFF_PROMISC) {
  493. /* Enable promiscuous mode */
  494. dev_err(priv->dev, "Ignoring Promiscuous mode\n");
  495. return;
  496. }
  497. /* Clear all mcast from ALE */
  498. cpsw_ale_flush_multicast(priv->ale, ALE_ALL_PORTS << priv->host_port);
  499. if (!netdev_mc_empty(ndev)) {
  500. struct netdev_hw_addr *ha;
  501. /* program multicast address list into ALE register */
  502. netdev_for_each_mc_addr(ha, ndev) {
  503. cpsw_add_mcast(priv, (u8 *)ha->addr);
  504. }
  505. }
  506. }
  507. static void cpsw_intr_enable(struct cpsw_priv *priv)
  508. {
  509. __raw_writel(0xFF, &priv->wr_regs->tx_en);
  510. __raw_writel(0xFF, &priv->wr_regs->rx_en);
  511. cpdma_ctlr_int_ctrl(priv->dma, true);
  512. return;
  513. }
  514. static void cpsw_intr_disable(struct cpsw_priv *priv)
  515. {
  516. __raw_writel(0, &priv->wr_regs->tx_en);
  517. __raw_writel(0, &priv->wr_regs->rx_en);
  518. cpdma_ctlr_int_ctrl(priv->dma, false);
  519. return;
  520. }
  521. void cpsw_tx_handler(void *token, int len, int status)
  522. {
  523. struct sk_buff *skb = token;
  524. struct net_device *ndev = skb->dev;
  525. struct cpsw_priv *priv = netdev_priv(ndev);
  526. /* Check whether the queue is stopped due to stalled tx dma, if the
  527. * queue is stopped then start the queue as we have free desc for tx
  528. */
  529. if (unlikely(netif_queue_stopped(ndev)))
  530. netif_wake_queue(ndev);
  531. cpts_tx_timestamp(priv->cpts, skb);
  532. priv->stats.tx_packets++;
  533. priv->stats.tx_bytes += len;
  534. dev_kfree_skb_any(skb);
  535. }
  536. void cpsw_rx_handler(void *token, int len, int status)
  537. {
  538. struct sk_buff *skb = token;
  539. struct sk_buff *new_skb;
  540. struct net_device *ndev = skb->dev;
  541. struct cpsw_priv *priv = netdev_priv(ndev);
  542. int ret = 0;
  543. cpsw_dual_emac_src_port_detect(status, priv, ndev, skb);
  544. if (unlikely(status < 0)) {
  545. /* the interface is going down, skbs are purged */
  546. dev_kfree_skb_any(skb);
  547. return;
  548. }
  549. new_skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
  550. if (new_skb) {
  551. skb_put(skb, len);
  552. cpts_rx_timestamp(priv->cpts, skb);
  553. skb->protocol = eth_type_trans(skb, ndev);
  554. netif_receive_skb(skb);
  555. priv->stats.rx_bytes += len;
  556. priv->stats.rx_packets++;
  557. } else {
  558. priv->stats.rx_dropped++;
  559. new_skb = skb;
  560. }
  561. ret = cpdma_chan_submit(priv->rxch, new_skb, new_skb->data,
  562. skb_tailroom(new_skb), 0);
  563. if (WARN_ON(ret < 0))
  564. dev_kfree_skb_any(new_skb);
  565. }
  566. static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
  567. {
  568. struct cpsw_priv *priv = dev_id;
  569. u32 rx, tx, rx_thresh;
  570. rx_thresh = __raw_readl(&priv->wr_regs->rx_thresh_stat);
  571. rx = __raw_readl(&priv->wr_regs->rx_stat);
  572. tx = __raw_readl(&priv->wr_regs->tx_stat);
  573. if (!rx_thresh && !rx && !tx)
  574. return IRQ_NONE;
  575. cpsw_intr_disable(priv);
  576. if (priv->irq_enabled == true) {
  577. cpsw_disable_irq(priv);
  578. priv->irq_enabled = false;
  579. }
  580. if (netif_running(priv->ndev)) {
  581. napi_schedule(&priv->napi);
  582. return IRQ_HANDLED;
  583. }
  584. priv = cpsw_get_slave_priv(priv, 1);
  585. if (!priv)
  586. return IRQ_NONE;
  587. if (netif_running(priv->ndev)) {
  588. napi_schedule(&priv->napi);
  589. return IRQ_HANDLED;
  590. }
  591. return IRQ_NONE;
  592. }
  593. static int cpsw_poll(struct napi_struct *napi, int budget)
  594. {
  595. struct cpsw_priv *priv = napi_to_priv(napi);
  596. int num_tx, num_rx;
  597. num_tx = cpdma_chan_process(priv->txch, 128);
  598. if (num_tx)
  599. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
  600. num_rx = cpdma_chan_process(priv->rxch, budget);
  601. if (num_rx < budget) {
  602. struct cpsw_priv *prim_cpsw;
  603. napi_complete(napi);
  604. cpsw_intr_enable(priv);
  605. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
  606. prim_cpsw = cpsw_get_slave_priv(priv, 0);
  607. if (prim_cpsw->irq_enabled == false) {
  608. prim_cpsw->irq_enabled = true;
  609. cpsw_enable_irq(priv);
  610. }
  611. }
  612. if (num_rx || num_tx)
  613. cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n",
  614. num_rx, num_tx);
  615. return num_rx;
  616. }
  617. static inline void soft_reset(const char *module, void __iomem *reg)
  618. {
  619. unsigned long timeout = jiffies + HZ;
  620. __raw_writel(1, reg);
  621. do {
  622. cpu_relax();
  623. } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
  624. WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
  625. }
  626. #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
  627. ((mac)[2] << 16) | ((mac)[3] << 24))
  628. #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
  629. static void cpsw_set_slave_mac(struct cpsw_slave *slave,
  630. struct cpsw_priv *priv)
  631. {
  632. slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
  633. slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
  634. }
  635. static void _cpsw_adjust_link(struct cpsw_slave *slave,
  636. struct cpsw_priv *priv, bool *link)
  637. {
  638. struct phy_device *phy = slave->phy;
  639. u32 mac_control = 0;
  640. u32 slave_port;
  641. if (!phy)
  642. return;
  643. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  644. if (phy->link) {
  645. mac_control = priv->data.mac_control;
  646. /* enable forwarding */
  647. cpsw_ale_control_set(priv->ale, slave_port,
  648. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  649. if (phy->speed == 1000)
  650. mac_control |= BIT(7); /* GIGABITEN */
  651. if (phy->duplex)
  652. mac_control |= BIT(0); /* FULLDUPLEXEN */
  653. /* set speed_in input in case RMII mode is used in 100Mbps */
  654. if (phy->speed == 100)
  655. mac_control |= BIT(15);
  656. *link = true;
  657. } else {
  658. mac_control = 0;
  659. /* disable forwarding */
  660. cpsw_ale_control_set(priv->ale, slave_port,
  661. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  662. }
  663. if (mac_control != slave->mac_control) {
  664. phy_print_status(phy);
  665. __raw_writel(mac_control, &slave->sliver->mac_control);
  666. }
  667. slave->mac_control = mac_control;
  668. }
  669. static void cpsw_adjust_link(struct net_device *ndev)
  670. {
  671. struct cpsw_priv *priv = netdev_priv(ndev);
  672. bool link = false;
  673. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  674. if (link) {
  675. netif_carrier_on(ndev);
  676. if (netif_running(ndev))
  677. netif_wake_queue(ndev);
  678. } else {
  679. netif_carrier_off(ndev);
  680. netif_stop_queue(ndev);
  681. }
  682. }
  683. static int cpsw_get_coalesce(struct net_device *ndev,
  684. struct ethtool_coalesce *coal)
  685. {
  686. struct cpsw_priv *priv = netdev_priv(ndev);
  687. coal->rx_coalesce_usecs = priv->coal_intvl;
  688. return 0;
  689. }
  690. static int cpsw_set_coalesce(struct net_device *ndev,
  691. struct ethtool_coalesce *coal)
  692. {
  693. struct cpsw_priv *priv = netdev_priv(ndev);
  694. u32 int_ctrl;
  695. u32 num_interrupts = 0;
  696. u32 prescale = 0;
  697. u32 addnl_dvdr = 1;
  698. u32 coal_intvl = 0;
  699. if (!coal->rx_coalesce_usecs)
  700. return -EINVAL;
  701. coal_intvl = coal->rx_coalesce_usecs;
  702. int_ctrl = readl(&priv->wr_regs->int_control);
  703. prescale = priv->bus_freq_mhz * 4;
  704. if (coal_intvl < CPSW_CMINTMIN_INTVL)
  705. coal_intvl = CPSW_CMINTMIN_INTVL;
  706. if (coal_intvl > CPSW_CMINTMAX_INTVL) {
  707. /* Interrupt pacer works with 4us Pulse, we can
  708. * throttle further by dilating the 4us pulse.
  709. */
  710. addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
  711. if (addnl_dvdr > 1) {
  712. prescale *= addnl_dvdr;
  713. if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
  714. coal_intvl = (CPSW_CMINTMAX_INTVL
  715. * addnl_dvdr);
  716. } else {
  717. addnl_dvdr = 1;
  718. coal_intvl = CPSW_CMINTMAX_INTVL;
  719. }
  720. }
  721. num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
  722. writel(num_interrupts, &priv->wr_regs->rx_imax);
  723. writel(num_interrupts, &priv->wr_regs->tx_imax);
  724. int_ctrl |= CPSW_INTPACEEN;
  725. int_ctrl &= (~CPSW_INTPRESCALE_MASK);
  726. int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
  727. writel(int_ctrl, &priv->wr_regs->int_control);
  728. cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
  729. if (priv->data.dual_emac) {
  730. int i;
  731. for (i = 0; i < priv->data.slaves; i++) {
  732. priv = netdev_priv(priv->slaves[i].ndev);
  733. priv->coal_intvl = coal_intvl;
  734. }
  735. } else {
  736. priv->coal_intvl = coal_intvl;
  737. }
  738. return 0;
  739. }
  740. static int cpsw_get_sset_count(struct net_device *ndev, int sset)
  741. {
  742. switch (sset) {
  743. case ETH_SS_STATS:
  744. return CPSW_STATS_LEN;
  745. default:
  746. return -EOPNOTSUPP;
  747. }
  748. }
  749. static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  750. {
  751. u8 *p = data;
  752. int i;
  753. switch (stringset) {
  754. case ETH_SS_STATS:
  755. for (i = 0; i < CPSW_STATS_LEN; i++) {
  756. memcpy(p, cpsw_gstrings_stats[i].stat_string,
  757. ETH_GSTRING_LEN);
  758. p += ETH_GSTRING_LEN;
  759. }
  760. break;
  761. }
  762. }
  763. static void cpsw_get_ethtool_stats(struct net_device *ndev,
  764. struct ethtool_stats *stats, u64 *data)
  765. {
  766. struct cpsw_priv *priv = netdev_priv(ndev);
  767. struct cpdma_chan_stats rx_stats;
  768. struct cpdma_chan_stats tx_stats;
  769. u32 val;
  770. u8 *p;
  771. int i;
  772. /* Collect Davinci CPDMA stats for Rx and Tx Channel */
  773. cpdma_chan_get_stats(priv->rxch, &rx_stats);
  774. cpdma_chan_get_stats(priv->txch, &tx_stats);
  775. for (i = 0; i < CPSW_STATS_LEN; i++) {
  776. switch (cpsw_gstrings_stats[i].type) {
  777. case CPSW_STATS:
  778. val = readl(priv->hw_stats +
  779. cpsw_gstrings_stats[i].stat_offset);
  780. data[i] = val;
  781. break;
  782. case CPDMA_RX_STATS:
  783. p = (u8 *)&rx_stats +
  784. cpsw_gstrings_stats[i].stat_offset;
  785. data[i] = *(u32 *)p;
  786. break;
  787. case CPDMA_TX_STATS:
  788. p = (u8 *)&tx_stats +
  789. cpsw_gstrings_stats[i].stat_offset;
  790. data[i] = *(u32 *)p;
  791. break;
  792. }
  793. }
  794. }
  795. static inline int __show_stat(char *buf, int maxlen, const char *name, u32 val)
  796. {
  797. static char *leader = "........................................";
  798. if (!val)
  799. return 0;
  800. else
  801. return snprintf(buf, maxlen, "%s %s %10d\n", name,
  802. leader + strlen(name), val);
  803. }
  804. static int cpsw_common_res_usage_state(struct cpsw_priv *priv)
  805. {
  806. u32 i;
  807. u32 usage_count = 0;
  808. if (!priv->data.dual_emac)
  809. return 0;
  810. for (i = 0; i < priv->data.slaves; i++)
  811. if (priv->slaves[i].open_stat)
  812. usage_count++;
  813. return usage_count;
  814. }
  815. static inline int cpsw_tx_packet_submit(struct net_device *ndev,
  816. struct cpsw_priv *priv, struct sk_buff *skb)
  817. {
  818. if (!priv->data.dual_emac)
  819. return cpdma_chan_submit(priv->txch, skb, skb->data,
  820. skb->len, 0);
  821. if (ndev == cpsw_get_slave_ndev(priv, 0))
  822. return cpdma_chan_submit(priv->txch, skb, skb->data,
  823. skb->len, 1);
  824. else
  825. return cpdma_chan_submit(priv->txch, skb, skb->data,
  826. skb->len, 2);
  827. }
  828. static inline void cpsw_add_dual_emac_def_ale_entries(
  829. struct cpsw_priv *priv, struct cpsw_slave *slave,
  830. u32 slave_port)
  831. {
  832. u32 port_mask = 1 << slave_port | 1 << priv->host_port;
  833. if (priv->version == CPSW_VERSION_1)
  834. slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
  835. else
  836. slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
  837. cpsw_ale_add_vlan(priv->ale, slave->port_vlan, port_mask,
  838. port_mask, port_mask, 0);
  839. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  840. port_mask, ALE_VLAN, slave->port_vlan, 0);
  841. cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
  842. priv->host_port, ALE_VLAN, slave->port_vlan);
  843. }
  844. static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
  845. {
  846. char name[32];
  847. u32 slave_port;
  848. sprintf(name, "slave-%d", slave->slave_num);
  849. soft_reset(name, &slave->sliver->soft_reset);
  850. /* setup priority mapping */
  851. __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
  852. switch (priv->version) {
  853. case CPSW_VERSION_1:
  854. slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
  855. break;
  856. case CPSW_VERSION_2:
  857. slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
  858. break;
  859. }
  860. /* setup max packet size, and mac address */
  861. __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
  862. cpsw_set_slave_mac(slave, priv);
  863. slave->mac_control = 0; /* no link yet */
  864. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  865. if (priv->data.dual_emac)
  866. cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
  867. else
  868. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  869. 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
  870. slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
  871. &cpsw_adjust_link, slave->data->phy_if);
  872. if (IS_ERR(slave->phy)) {
  873. dev_err(priv->dev, "phy %s not found on slave %d\n",
  874. slave->data->phy_id, slave->slave_num);
  875. slave->phy = NULL;
  876. } else {
  877. dev_info(priv->dev, "phy found : id is : 0x%x\n",
  878. slave->phy->phy_id);
  879. phy_start(slave->phy);
  880. }
  881. }
  882. static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
  883. {
  884. const int vlan = priv->data.default_vlan;
  885. const int port = priv->host_port;
  886. u32 reg;
  887. int i;
  888. reg = (priv->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
  889. CPSW2_PORT_VLAN;
  890. writel(vlan, &priv->host_port_regs->port_vlan);
  891. for (i = 0; i < priv->data.slaves; i++)
  892. slave_write(priv->slaves + i, vlan, reg);
  893. cpsw_ale_add_vlan(priv->ale, vlan, ALE_ALL_PORTS << port,
  894. ALE_ALL_PORTS << port, ALE_ALL_PORTS << port,
  895. (ALE_PORT_1 | ALE_PORT_2) << port);
  896. }
  897. static void cpsw_init_host_port(struct cpsw_priv *priv)
  898. {
  899. u32 control_reg;
  900. u32 fifo_mode;
  901. /* soft reset the controller and initialize ale */
  902. soft_reset("cpsw", &priv->regs->soft_reset);
  903. cpsw_ale_start(priv->ale);
  904. /* switch to vlan unaware mode */
  905. cpsw_ale_control_set(priv->ale, priv->host_port, ALE_VLAN_AWARE,
  906. CPSW_ALE_VLAN_AWARE);
  907. control_reg = readl(&priv->regs->control);
  908. control_reg |= CPSW_VLAN_AWARE;
  909. writel(control_reg, &priv->regs->control);
  910. fifo_mode = (priv->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
  911. CPSW_FIFO_NORMAL_MODE;
  912. writel(fifo_mode, &priv->host_port_regs->tx_in_ctl);
  913. /* setup host port priority mapping */
  914. __raw_writel(CPDMA_TX_PRIORITY_MAP,
  915. &priv->host_port_regs->cpdma_tx_pri_map);
  916. __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
  917. cpsw_ale_control_set(priv->ale, priv->host_port,
  918. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  919. if (!priv->data.dual_emac) {
  920. cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port,
  921. 0, 0);
  922. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  923. 1 << priv->host_port, 0, 0, ALE_MCAST_FWD_2);
  924. }
  925. }
  926. static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
  927. {
  928. if (!slave->phy)
  929. return;
  930. phy_stop(slave->phy);
  931. phy_disconnect(slave->phy);
  932. slave->phy = NULL;
  933. }
  934. static int cpsw_ndo_open(struct net_device *ndev)
  935. {
  936. struct cpsw_priv *priv = netdev_priv(ndev);
  937. struct cpsw_priv *prim_cpsw;
  938. int i, ret;
  939. u32 reg;
  940. if (!cpsw_common_res_usage_state(priv))
  941. cpsw_intr_disable(priv);
  942. netif_carrier_off(ndev);
  943. pm_runtime_get_sync(&priv->pdev->dev);
  944. reg = priv->version;
  945. dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
  946. CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
  947. CPSW_RTL_VERSION(reg));
  948. /* initialize host and slave ports */
  949. if (!cpsw_common_res_usage_state(priv))
  950. cpsw_init_host_port(priv);
  951. for_each_slave(priv, cpsw_slave_open, priv);
  952. /* Add default VLAN */
  953. if (!priv->data.dual_emac)
  954. cpsw_add_default_vlan(priv);
  955. if (!cpsw_common_res_usage_state(priv)) {
  956. /* setup tx dma to fixed prio and zero offset */
  957. cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
  958. cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
  959. /* disable priority elevation */
  960. __raw_writel(0, &priv->regs->ptype);
  961. /* enable statistics collection only on all ports */
  962. __raw_writel(0x7, &priv->regs->stat_port_en);
  963. if (WARN_ON(!priv->data.rx_descs))
  964. priv->data.rx_descs = 128;
  965. for (i = 0; i < priv->data.rx_descs; i++) {
  966. struct sk_buff *skb;
  967. ret = -ENOMEM;
  968. skb = __netdev_alloc_skb_ip_align(priv->ndev,
  969. priv->rx_packet_max, GFP_KERNEL);
  970. if (!skb)
  971. goto err_cleanup;
  972. ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
  973. skb_tailroom(skb), 0);
  974. if (ret < 0) {
  975. kfree_skb(skb);
  976. goto err_cleanup;
  977. }
  978. }
  979. /* continue even if we didn't manage to submit all
  980. * receive descs
  981. */
  982. cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
  983. }
  984. /* Enable Interrupt pacing if configured */
  985. if (priv->coal_intvl != 0) {
  986. struct ethtool_coalesce coal;
  987. coal.rx_coalesce_usecs = (priv->coal_intvl << 4);
  988. cpsw_set_coalesce(ndev, &coal);
  989. }
  990. prim_cpsw = cpsw_get_slave_priv(priv, 0);
  991. if (prim_cpsw->irq_enabled == false) {
  992. if ((priv == prim_cpsw) || !netif_running(prim_cpsw->ndev)) {
  993. prim_cpsw->irq_enabled = true;
  994. cpsw_enable_irq(prim_cpsw);
  995. }
  996. }
  997. cpdma_ctlr_start(priv->dma);
  998. cpsw_intr_enable(priv);
  999. napi_enable(&priv->napi);
  1000. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
  1001. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
  1002. if (priv->data.dual_emac)
  1003. priv->slaves[priv->emac_port].open_stat = true;
  1004. return 0;
  1005. err_cleanup:
  1006. cpdma_ctlr_stop(priv->dma);
  1007. for_each_slave(priv, cpsw_slave_stop, priv);
  1008. pm_runtime_put_sync(&priv->pdev->dev);
  1009. netif_carrier_off(priv->ndev);
  1010. return ret;
  1011. }
  1012. static int cpsw_ndo_stop(struct net_device *ndev)
  1013. {
  1014. struct cpsw_priv *priv = netdev_priv(ndev);
  1015. cpsw_info(priv, ifdown, "shutting down cpsw device\n");
  1016. netif_stop_queue(priv->ndev);
  1017. napi_disable(&priv->napi);
  1018. netif_carrier_off(priv->ndev);
  1019. if (cpsw_common_res_usage_state(priv) <= 1) {
  1020. cpsw_intr_disable(priv);
  1021. cpdma_ctlr_int_ctrl(priv->dma, false);
  1022. cpdma_ctlr_stop(priv->dma);
  1023. cpsw_ale_stop(priv->ale);
  1024. }
  1025. for_each_slave(priv, cpsw_slave_stop, priv);
  1026. pm_runtime_put_sync(&priv->pdev->dev);
  1027. if (priv->data.dual_emac)
  1028. priv->slaves[priv->emac_port].open_stat = false;
  1029. return 0;
  1030. }
  1031. static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
  1032. struct net_device *ndev)
  1033. {
  1034. struct cpsw_priv *priv = netdev_priv(ndev);
  1035. int ret;
  1036. ndev->trans_start = jiffies;
  1037. if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
  1038. cpsw_err(priv, tx_err, "packet pad failed\n");
  1039. priv->stats.tx_dropped++;
  1040. return NETDEV_TX_OK;
  1041. }
  1042. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
  1043. priv->cpts->tx_enable)
  1044. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1045. skb_tx_timestamp(skb);
  1046. ret = cpsw_tx_packet_submit(ndev, priv, skb);
  1047. if (unlikely(ret != 0)) {
  1048. cpsw_err(priv, tx_err, "desc submit failed\n");
  1049. goto fail;
  1050. }
  1051. /* If there is no more tx desc left free then we need to
  1052. * tell the kernel to stop sending us tx frames.
  1053. */
  1054. if (unlikely(!cpdma_check_free_tx_desc(priv->txch)))
  1055. netif_stop_queue(ndev);
  1056. return NETDEV_TX_OK;
  1057. fail:
  1058. priv->stats.tx_dropped++;
  1059. netif_stop_queue(ndev);
  1060. return NETDEV_TX_BUSY;
  1061. }
  1062. static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
  1063. {
  1064. /*
  1065. * The switch cannot operate in promiscuous mode without substantial
  1066. * headache. For promiscuous mode to work, we would need to put the
  1067. * ALE in bypass mode and route all traffic to the host port.
  1068. * Subsequently, the host will need to operate as a "bridge", learn,
  1069. * and flood as needed. For now, we simply complain here and
  1070. * do nothing about it :-)
  1071. */
  1072. if ((flags & IFF_PROMISC) && (ndev->flags & IFF_PROMISC))
  1073. dev_err(&ndev->dev, "promiscuity ignored!\n");
  1074. /*
  1075. * The switch cannot filter multicast traffic unless it is configured
  1076. * in "VLAN Aware" mode. Unfortunately, VLAN awareness requires a
  1077. * whole bunch of additional logic that this driver does not implement
  1078. * at present.
  1079. */
  1080. if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
  1081. dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
  1082. }
  1083. #ifdef CONFIG_TI_CPTS
  1084. static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
  1085. {
  1086. struct cpsw_slave *slave = &priv->slaves[priv->data.active_slave];
  1087. u32 ts_en, seq_id;
  1088. if (!priv->cpts->tx_enable && !priv->cpts->rx_enable) {
  1089. slave_write(slave, 0, CPSW1_TS_CTL);
  1090. return;
  1091. }
  1092. seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
  1093. ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
  1094. if (priv->cpts->tx_enable)
  1095. ts_en |= CPSW_V1_TS_TX_EN;
  1096. if (priv->cpts->rx_enable)
  1097. ts_en |= CPSW_V1_TS_RX_EN;
  1098. slave_write(slave, ts_en, CPSW1_TS_CTL);
  1099. slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
  1100. }
  1101. static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
  1102. {
  1103. struct cpsw_slave *slave;
  1104. u32 ctrl, mtype;
  1105. if (priv->data.dual_emac)
  1106. slave = &priv->slaves[priv->emac_port];
  1107. else
  1108. slave = &priv->slaves[priv->data.active_slave];
  1109. ctrl = slave_read(slave, CPSW2_CONTROL);
  1110. ctrl &= ~CTRL_ALL_TS_MASK;
  1111. if (priv->cpts->tx_enable)
  1112. ctrl |= CTRL_TX_TS_BITS;
  1113. if (priv->cpts->rx_enable)
  1114. ctrl |= CTRL_RX_TS_BITS;
  1115. mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
  1116. slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
  1117. slave_write(slave, ctrl, CPSW2_CONTROL);
  1118. __raw_writel(ETH_P_1588, &priv->regs->ts_ltype);
  1119. }
  1120. static int cpsw_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
  1121. {
  1122. struct cpsw_priv *priv = netdev_priv(dev);
  1123. struct cpts *cpts = priv->cpts;
  1124. struct hwtstamp_config cfg;
  1125. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  1126. return -EFAULT;
  1127. /* reserved for future extensions */
  1128. if (cfg.flags)
  1129. return -EINVAL;
  1130. switch (cfg.tx_type) {
  1131. case HWTSTAMP_TX_OFF:
  1132. cpts->tx_enable = 0;
  1133. break;
  1134. case HWTSTAMP_TX_ON:
  1135. cpts->tx_enable = 1;
  1136. break;
  1137. default:
  1138. return -ERANGE;
  1139. }
  1140. switch (cfg.rx_filter) {
  1141. case HWTSTAMP_FILTER_NONE:
  1142. cpts->rx_enable = 0;
  1143. break;
  1144. case HWTSTAMP_FILTER_ALL:
  1145. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1146. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1147. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1148. return -ERANGE;
  1149. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1150. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1151. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1152. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1153. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1154. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1155. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1156. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1157. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1158. cpts->rx_enable = 1;
  1159. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  1160. break;
  1161. default:
  1162. return -ERANGE;
  1163. }
  1164. switch (priv->version) {
  1165. case CPSW_VERSION_1:
  1166. cpsw_hwtstamp_v1(priv);
  1167. break;
  1168. case CPSW_VERSION_2:
  1169. cpsw_hwtstamp_v2(priv);
  1170. break;
  1171. default:
  1172. return -ENOTSUPP;
  1173. }
  1174. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  1175. }
  1176. #endif /*CONFIG_TI_CPTS*/
  1177. static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  1178. {
  1179. struct cpsw_priv *priv = netdev_priv(dev);
  1180. struct mii_ioctl_data *data = if_mii(req);
  1181. int slave_no = cpsw_slave_index(priv);
  1182. if (!netif_running(dev))
  1183. return -EINVAL;
  1184. switch (cmd) {
  1185. #ifdef CONFIG_TI_CPTS
  1186. case SIOCSHWTSTAMP:
  1187. return cpsw_hwtstamp_ioctl(dev, req);
  1188. #endif
  1189. case SIOCGMIIPHY:
  1190. data->phy_id = priv->slaves[slave_no].phy->addr;
  1191. break;
  1192. default:
  1193. return -ENOTSUPP;
  1194. }
  1195. return 0;
  1196. }
  1197. static void cpsw_ndo_tx_timeout(struct net_device *ndev)
  1198. {
  1199. struct cpsw_priv *priv = netdev_priv(ndev);
  1200. cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
  1201. priv->stats.tx_errors++;
  1202. cpsw_intr_disable(priv);
  1203. cpdma_ctlr_int_ctrl(priv->dma, false);
  1204. cpdma_chan_stop(priv->txch);
  1205. cpdma_chan_start(priv->txch);
  1206. cpdma_ctlr_int_ctrl(priv->dma, true);
  1207. cpsw_intr_enable(priv);
  1208. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
  1209. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
  1210. }
  1211. static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
  1212. {
  1213. struct cpsw_priv *priv = netdev_priv(ndev);
  1214. struct sockaddr *addr = (struct sockaddr *)p;
  1215. int flags = 0;
  1216. u16 vid = 0;
  1217. if (!is_valid_ether_addr(addr->sa_data))
  1218. return -EADDRNOTAVAIL;
  1219. if (priv->data.dual_emac) {
  1220. vid = priv->slaves[priv->emac_port].port_vlan;
  1221. flags = ALE_VLAN;
  1222. }
  1223. cpsw_ale_del_ucast(priv->ale, priv->mac_addr, priv->host_port,
  1224. flags, vid);
  1225. cpsw_ale_add_ucast(priv->ale, addr->sa_data, priv->host_port,
  1226. flags, vid);
  1227. memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
  1228. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  1229. for_each_slave(priv, cpsw_set_slave_mac, priv);
  1230. return 0;
  1231. }
  1232. static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev)
  1233. {
  1234. struct cpsw_priv *priv = netdev_priv(ndev);
  1235. return &priv->stats;
  1236. }
  1237. #ifdef CONFIG_NET_POLL_CONTROLLER
  1238. static void cpsw_ndo_poll_controller(struct net_device *ndev)
  1239. {
  1240. struct cpsw_priv *priv = netdev_priv(ndev);
  1241. cpsw_intr_disable(priv);
  1242. cpdma_ctlr_int_ctrl(priv->dma, false);
  1243. cpsw_interrupt(ndev->irq, priv);
  1244. cpdma_ctlr_int_ctrl(priv->dma, true);
  1245. cpsw_intr_enable(priv);
  1246. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
  1247. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
  1248. }
  1249. #endif
  1250. static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
  1251. unsigned short vid)
  1252. {
  1253. int ret;
  1254. ret = cpsw_ale_add_vlan(priv->ale, vid,
  1255. ALE_ALL_PORTS << priv->host_port,
  1256. 0, ALE_ALL_PORTS << priv->host_port,
  1257. (ALE_PORT_1 | ALE_PORT_2) << priv->host_port);
  1258. if (ret != 0)
  1259. return ret;
  1260. ret = cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
  1261. priv->host_port, ALE_VLAN, vid);
  1262. if (ret != 0)
  1263. goto clean_vid;
  1264. ret = cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  1265. ALE_ALL_PORTS << priv->host_port,
  1266. ALE_VLAN, vid, 0);
  1267. if (ret != 0)
  1268. goto clean_vlan_ucast;
  1269. return 0;
  1270. clean_vlan_ucast:
  1271. cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
  1272. priv->host_port, ALE_VLAN, vid);
  1273. clean_vid:
  1274. cpsw_ale_del_vlan(priv->ale, vid, 0);
  1275. return ret;
  1276. }
  1277. static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
  1278. __be16 proto, u16 vid)
  1279. {
  1280. struct cpsw_priv *priv = netdev_priv(ndev);
  1281. if (vid == priv->data.default_vlan)
  1282. return 0;
  1283. dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
  1284. return cpsw_add_vlan_ale_entry(priv, vid);
  1285. }
  1286. static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
  1287. __be16 proto, u16 vid)
  1288. {
  1289. struct cpsw_priv *priv = netdev_priv(ndev);
  1290. int ret;
  1291. if (vid == priv->data.default_vlan)
  1292. return 0;
  1293. dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
  1294. ret = cpsw_ale_del_vlan(priv->ale, vid, 0);
  1295. if (ret != 0)
  1296. return ret;
  1297. ret = cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
  1298. priv->host_port, ALE_VLAN, vid);
  1299. if (ret != 0)
  1300. return ret;
  1301. return cpsw_ale_del_mcast(priv->ale, priv->ndev->broadcast,
  1302. 0, ALE_VLAN, vid);
  1303. }
  1304. static const struct net_device_ops cpsw_netdev_ops = {
  1305. .ndo_open = cpsw_ndo_open,
  1306. .ndo_stop = cpsw_ndo_stop,
  1307. .ndo_start_xmit = cpsw_ndo_start_xmit,
  1308. .ndo_change_rx_flags = cpsw_ndo_change_rx_flags,
  1309. .ndo_set_mac_address = cpsw_ndo_set_mac_address,
  1310. .ndo_do_ioctl = cpsw_ndo_ioctl,
  1311. .ndo_validate_addr = eth_validate_addr,
  1312. .ndo_change_mtu = eth_change_mtu,
  1313. .ndo_tx_timeout = cpsw_ndo_tx_timeout,
  1314. .ndo_get_stats = cpsw_ndo_get_stats,
  1315. .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
  1316. #ifdef CONFIG_NET_POLL_CONTROLLER
  1317. .ndo_poll_controller = cpsw_ndo_poll_controller,
  1318. #endif
  1319. .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
  1320. .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
  1321. };
  1322. static void cpsw_get_drvinfo(struct net_device *ndev,
  1323. struct ethtool_drvinfo *info)
  1324. {
  1325. struct cpsw_priv *priv = netdev_priv(ndev);
  1326. strlcpy(info->driver, "TI CPSW Driver v1.0", sizeof(info->driver));
  1327. strlcpy(info->version, "1.0", sizeof(info->version));
  1328. strlcpy(info->bus_info, priv->pdev->name, sizeof(info->bus_info));
  1329. }
  1330. static u32 cpsw_get_msglevel(struct net_device *ndev)
  1331. {
  1332. struct cpsw_priv *priv = netdev_priv(ndev);
  1333. return priv->msg_enable;
  1334. }
  1335. static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
  1336. {
  1337. struct cpsw_priv *priv = netdev_priv(ndev);
  1338. priv->msg_enable = value;
  1339. }
  1340. static int cpsw_get_ts_info(struct net_device *ndev,
  1341. struct ethtool_ts_info *info)
  1342. {
  1343. #ifdef CONFIG_TI_CPTS
  1344. struct cpsw_priv *priv = netdev_priv(ndev);
  1345. info->so_timestamping =
  1346. SOF_TIMESTAMPING_TX_HARDWARE |
  1347. SOF_TIMESTAMPING_TX_SOFTWARE |
  1348. SOF_TIMESTAMPING_RX_HARDWARE |
  1349. SOF_TIMESTAMPING_RX_SOFTWARE |
  1350. SOF_TIMESTAMPING_SOFTWARE |
  1351. SOF_TIMESTAMPING_RAW_HARDWARE;
  1352. info->phc_index = priv->cpts->phc_index;
  1353. info->tx_types =
  1354. (1 << HWTSTAMP_TX_OFF) |
  1355. (1 << HWTSTAMP_TX_ON);
  1356. info->rx_filters =
  1357. (1 << HWTSTAMP_FILTER_NONE) |
  1358. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
  1359. #else
  1360. info->so_timestamping =
  1361. SOF_TIMESTAMPING_TX_SOFTWARE |
  1362. SOF_TIMESTAMPING_RX_SOFTWARE |
  1363. SOF_TIMESTAMPING_SOFTWARE;
  1364. info->phc_index = -1;
  1365. info->tx_types = 0;
  1366. info->rx_filters = 0;
  1367. #endif
  1368. return 0;
  1369. }
  1370. static int cpsw_get_settings(struct net_device *ndev,
  1371. struct ethtool_cmd *ecmd)
  1372. {
  1373. struct cpsw_priv *priv = netdev_priv(ndev);
  1374. int slave_no = cpsw_slave_index(priv);
  1375. if (priv->slaves[slave_no].phy)
  1376. return phy_ethtool_gset(priv->slaves[slave_no].phy, ecmd);
  1377. else
  1378. return -EOPNOTSUPP;
  1379. }
  1380. static int cpsw_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
  1381. {
  1382. struct cpsw_priv *priv = netdev_priv(ndev);
  1383. int slave_no = cpsw_slave_index(priv);
  1384. if (priv->slaves[slave_no].phy)
  1385. return phy_ethtool_sset(priv->slaves[slave_no].phy, ecmd);
  1386. else
  1387. return -EOPNOTSUPP;
  1388. }
  1389. static const struct ethtool_ops cpsw_ethtool_ops = {
  1390. .get_drvinfo = cpsw_get_drvinfo,
  1391. .get_msglevel = cpsw_get_msglevel,
  1392. .set_msglevel = cpsw_set_msglevel,
  1393. .get_link = ethtool_op_get_link,
  1394. .get_ts_info = cpsw_get_ts_info,
  1395. .get_settings = cpsw_get_settings,
  1396. .set_settings = cpsw_set_settings,
  1397. .get_coalesce = cpsw_get_coalesce,
  1398. .set_coalesce = cpsw_set_coalesce,
  1399. .get_sset_count = cpsw_get_sset_count,
  1400. .get_strings = cpsw_get_strings,
  1401. .get_ethtool_stats = cpsw_get_ethtool_stats,
  1402. };
  1403. static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv,
  1404. u32 slave_reg_ofs, u32 sliver_reg_ofs)
  1405. {
  1406. void __iomem *regs = priv->regs;
  1407. int slave_num = slave->slave_num;
  1408. struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
  1409. slave->data = data;
  1410. slave->regs = regs + slave_reg_ofs;
  1411. slave->sliver = regs + sliver_reg_ofs;
  1412. slave->port_vlan = data->dual_emac_res_vlan;
  1413. }
  1414. static int cpsw_probe_dt(struct cpsw_platform_data *data,
  1415. struct platform_device *pdev)
  1416. {
  1417. struct device_node *node = pdev->dev.of_node;
  1418. struct device_node *slave_node;
  1419. int i = 0, ret;
  1420. u32 prop;
  1421. if (!node)
  1422. return -EINVAL;
  1423. if (of_property_read_u32(node, "slaves", &prop)) {
  1424. pr_err("Missing slaves property in the DT.\n");
  1425. return -EINVAL;
  1426. }
  1427. data->slaves = prop;
  1428. if (of_property_read_u32(node, "active_slave", &prop)) {
  1429. pr_err("Missing active_slave property in the DT.\n");
  1430. ret = -EINVAL;
  1431. goto error_ret;
  1432. }
  1433. data->active_slave = prop;
  1434. if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
  1435. pr_err("Missing cpts_clock_mult property in the DT.\n");
  1436. ret = -EINVAL;
  1437. goto error_ret;
  1438. }
  1439. data->cpts_clock_mult = prop;
  1440. if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
  1441. pr_err("Missing cpts_clock_shift property in the DT.\n");
  1442. ret = -EINVAL;
  1443. goto error_ret;
  1444. }
  1445. data->cpts_clock_shift = prop;
  1446. data->slave_data = kcalloc(data->slaves, sizeof(struct cpsw_slave_data),
  1447. GFP_KERNEL);
  1448. if (!data->slave_data)
  1449. return -EINVAL;
  1450. if (of_property_read_u32(node, "cpdma_channels", &prop)) {
  1451. pr_err("Missing cpdma_channels property in the DT.\n");
  1452. ret = -EINVAL;
  1453. goto error_ret;
  1454. }
  1455. data->channels = prop;
  1456. if (of_property_read_u32(node, "ale_entries", &prop)) {
  1457. pr_err("Missing ale_entries property in the DT.\n");
  1458. ret = -EINVAL;
  1459. goto error_ret;
  1460. }
  1461. data->ale_entries = prop;
  1462. if (of_property_read_u32(node, "bd_ram_size", &prop)) {
  1463. pr_err("Missing bd_ram_size property in the DT.\n");
  1464. ret = -EINVAL;
  1465. goto error_ret;
  1466. }
  1467. data->bd_ram_size = prop;
  1468. if (of_property_read_u32(node, "rx_descs", &prop)) {
  1469. pr_err("Missing rx_descs property in the DT.\n");
  1470. ret = -EINVAL;
  1471. goto error_ret;
  1472. }
  1473. data->rx_descs = prop;
  1474. if (of_property_read_u32(node, "mac_control", &prop)) {
  1475. pr_err("Missing mac_control property in the DT.\n");
  1476. ret = -EINVAL;
  1477. goto error_ret;
  1478. }
  1479. data->mac_control = prop;
  1480. if (!of_property_read_u32(node, "dual_emac", &prop))
  1481. data->dual_emac = prop;
  1482. /*
  1483. * Populate all the child nodes here...
  1484. */
  1485. ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
  1486. /* We do not want to force this, as in some cases may not have child */
  1487. if (ret)
  1488. pr_warn("Doesn't have any child node\n");
  1489. for_each_node_by_name(slave_node, "slave") {
  1490. struct cpsw_slave_data *slave_data = data->slave_data + i;
  1491. const void *mac_addr = NULL;
  1492. u32 phyid;
  1493. int lenp;
  1494. const __be32 *parp;
  1495. struct device_node *mdio_node;
  1496. struct platform_device *mdio;
  1497. parp = of_get_property(slave_node, "phy_id", &lenp);
  1498. if ((parp == NULL) || (lenp != (sizeof(void *) * 2))) {
  1499. pr_err("Missing slave[%d] phy_id property\n", i);
  1500. ret = -EINVAL;
  1501. goto error_ret;
  1502. }
  1503. mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
  1504. phyid = be32_to_cpup(parp+1);
  1505. mdio = of_find_device_by_node(mdio_node);
  1506. snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
  1507. PHY_ID_FMT, mdio->name, phyid);
  1508. mac_addr = of_get_mac_address(slave_node);
  1509. if (mac_addr)
  1510. memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
  1511. slave_data->phy_if = of_get_phy_mode(slave_node);
  1512. if (data->dual_emac) {
  1513. if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
  1514. &prop)) {
  1515. pr_err("Missing dual_emac_res_vlan in DT.\n");
  1516. slave_data->dual_emac_res_vlan = i+1;
  1517. pr_err("Using %d as Reserved VLAN for %d slave\n",
  1518. slave_data->dual_emac_res_vlan, i);
  1519. } else {
  1520. slave_data->dual_emac_res_vlan = prop;
  1521. }
  1522. }
  1523. i++;
  1524. }
  1525. return 0;
  1526. error_ret:
  1527. kfree(data->slave_data);
  1528. return ret;
  1529. }
  1530. static int cpsw_probe_dual_emac(struct platform_device *pdev,
  1531. struct cpsw_priv *priv)
  1532. {
  1533. struct cpsw_platform_data *data = &priv->data;
  1534. struct net_device *ndev;
  1535. struct cpsw_priv *priv_sl2;
  1536. int ret = 0, i;
  1537. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  1538. if (!ndev) {
  1539. pr_err("cpsw: error allocating net_device\n");
  1540. return -ENOMEM;
  1541. }
  1542. priv_sl2 = netdev_priv(ndev);
  1543. spin_lock_init(&priv_sl2->lock);
  1544. priv_sl2->data = *data;
  1545. priv_sl2->pdev = pdev;
  1546. priv_sl2->ndev = ndev;
  1547. priv_sl2->dev = &ndev->dev;
  1548. priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  1549. priv_sl2->rx_packet_max = max(rx_packet_max, 128);
  1550. if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
  1551. memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
  1552. ETH_ALEN);
  1553. pr_info("cpsw: Detected MACID = %pM\n", priv_sl2->mac_addr);
  1554. } else {
  1555. random_ether_addr(priv_sl2->mac_addr);
  1556. pr_info("cpsw: Random MACID = %pM\n", priv_sl2->mac_addr);
  1557. }
  1558. memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
  1559. priv_sl2->slaves = priv->slaves;
  1560. priv_sl2->clk = priv->clk;
  1561. priv_sl2->coal_intvl = 0;
  1562. priv_sl2->bus_freq_mhz = priv->bus_freq_mhz;
  1563. priv_sl2->cpsw_res = priv->cpsw_res;
  1564. priv_sl2->regs = priv->regs;
  1565. priv_sl2->host_port = priv->host_port;
  1566. priv_sl2->host_port_regs = priv->host_port_regs;
  1567. priv_sl2->wr_regs = priv->wr_regs;
  1568. priv_sl2->hw_stats = priv->hw_stats;
  1569. priv_sl2->dma = priv->dma;
  1570. priv_sl2->txch = priv->txch;
  1571. priv_sl2->rxch = priv->rxch;
  1572. priv_sl2->ale = priv->ale;
  1573. priv_sl2->emac_port = 1;
  1574. priv->slaves[1].ndev = ndev;
  1575. priv_sl2->cpts = priv->cpts;
  1576. priv_sl2->version = priv->version;
  1577. for (i = 0; i < priv->num_irqs; i++) {
  1578. priv_sl2->irqs_table[i] = priv->irqs_table[i];
  1579. priv_sl2->num_irqs = priv->num_irqs;
  1580. }
  1581. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1582. ndev->netdev_ops = &cpsw_netdev_ops;
  1583. SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
  1584. netif_napi_add(ndev, &priv_sl2->napi, cpsw_poll, CPSW_POLL_WEIGHT);
  1585. /* register the network device */
  1586. SET_NETDEV_DEV(ndev, &pdev->dev);
  1587. ret = register_netdev(ndev);
  1588. if (ret) {
  1589. pr_err("cpsw: error registering net device\n");
  1590. free_netdev(ndev);
  1591. ret = -ENODEV;
  1592. }
  1593. return ret;
  1594. }
  1595. static int cpsw_probe(struct platform_device *pdev)
  1596. {
  1597. struct cpsw_platform_data *data;
  1598. struct net_device *ndev;
  1599. struct cpsw_priv *priv;
  1600. struct cpdma_params dma_params;
  1601. struct cpsw_ale_params ale_params;
  1602. void __iomem *ss_regs, *wr_regs;
  1603. struct resource *res;
  1604. u32 slave_offset, sliver_offset, slave_size;
  1605. int ret = 0, i, k = 0;
  1606. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  1607. if (!ndev) {
  1608. pr_err("error allocating net_device\n");
  1609. return -ENOMEM;
  1610. }
  1611. platform_set_drvdata(pdev, ndev);
  1612. priv = netdev_priv(ndev);
  1613. spin_lock_init(&priv->lock);
  1614. priv->pdev = pdev;
  1615. priv->ndev = ndev;
  1616. priv->dev = &ndev->dev;
  1617. priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  1618. priv->rx_packet_max = max(rx_packet_max, 128);
  1619. priv->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL);
  1620. priv->irq_enabled = true;
  1621. if (!priv->cpts) {
  1622. pr_err("error allocating cpts\n");
  1623. goto clean_ndev_ret;
  1624. }
  1625. /*
  1626. * This may be required here for child devices.
  1627. */
  1628. pm_runtime_enable(&pdev->dev);
  1629. /* Select default pin state */
  1630. pinctrl_pm_select_default_state(&pdev->dev);
  1631. if (cpsw_probe_dt(&priv->data, pdev)) {
  1632. pr_err("cpsw: platform data missing\n");
  1633. ret = -ENODEV;
  1634. goto clean_ndev_ret;
  1635. }
  1636. data = &priv->data;
  1637. if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
  1638. memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
  1639. pr_info("Detected MACID = %pM\n", priv->mac_addr);
  1640. } else {
  1641. eth_random_addr(priv->mac_addr);
  1642. pr_info("Random MACID = %pM\n", priv->mac_addr);
  1643. }
  1644. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  1645. priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves,
  1646. GFP_KERNEL);
  1647. if (!priv->slaves) {
  1648. ret = -EBUSY;
  1649. goto clean_ndev_ret;
  1650. }
  1651. for (i = 0; i < data->slaves; i++)
  1652. priv->slaves[i].slave_num = i;
  1653. priv->slaves[0].ndev = ndev;
  1654. priv->emac_port = 0;
  1655. priv->clk = clk_get(&pdev->dev, "fck");
  1656. if (IS_ERR(priv->clk)) {
  1657. dev_err(&pdev->dev, "fck is not found\n");
  1658. ret = -ENODEV;
  1659. goto clean_slave_ret;
  1660. }
  1661. priv->coal_intvl = 0;
  1662. priv->bus_freq_mhz = clk_get_rate(priv->clk) / 1000000;
  1663. priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1664. if (!priv->cpsw_res) {
  1665. dev_err(priv->dev, "error getting i/o resource\n");
  1666. ret = -ENOENT;
  1667. goto clean_clk_ret;
  1668. }
  1669. if (!request_mem_region(priv->cpsw_res->start,
  1670. resource_size(priv->cpsw_res), ndev->name)) {
  1671. dev_err(priv->dev, "failed request i/o region\n");
  1672. ret = -ENXIO;
  1673. goto clean_clk_ret;
  1674. }
  1675. ss_regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res));
  1676. if (!ss_regs) {
  1677. dev_err(priv->dev, "unable to map i/o region\n");
  1678. goto clean_cpsw_iores_ret;
  1679. }
  1680. priv->regs = ss_regs;
  1681. priv->version = __raw_readl(&priv->regs->id_ver);
  1682. priv->host_port = HOST_PORT_NUM;
  1683. priv->cpsw_wr_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1684. if (!priv->cpsw_wr_res) {
  1685. dev_err(priv->dev, "error getting i/o resource\n");
  1686. ret = -ENOENT;
  1687. goto clean_iomap_ret;
  1688. }
  1689. if (!request_mem_region(priv->cpsw_wr_res->start,
  1690. resource_size(priv->cpsw_wr_res), ndev->name)) {
  1691. dev_err(priv->dev, "failed request i/o region\n");
  1692. ret = -ENXIO;
  1693. goto clean_iomap_ret;
  1694. }
  1695. wr_regs = ioremap(priv->cpsw_wr_res->start,
  1696. resource_size(priv->cpsw_wr_res));
  1697. if (!wr_regs) {
  1698. dev_err(priv->dev, "unable to map i/o region\n");
  1699. goto clean_cpsw_wr_iores_ret;
  1700. }
  1701. priv->wr_regs = wr_regs;
  1702. memset(&dma_params, 0, sizeof(dma_params));
  1703. memset(&ale_params, 0, sizeof(ale_params));
  1704. switch (priv->version) {
  1705. case CPSW_VERSION_1:
  1706. priv->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
  1707. priv->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET;
  1708. priv->hw_stats = ss_regs + CPSW1_HW_STATS;
  1709. dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
  1710. dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
  1711. ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
  1712. slave_offset = CPSW1_SLAVE_OFFSET;
  1713. slave_size = CPSW1_SLAVE_SIZE;
  1714. sliver_offset = CPSW1_SLIVER_OFFSET;
  1715. dma_params.desc_mem_phys = 0;
  1716. break;
  1717. case CPSW_VERSION_2:
  1718. priv->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
  1719. priv->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET;
  1720. priv->hw_stats = ss_regs + CPSW2_HW_STATS;
  1721. dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
  1722. dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
  1723. ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
  1724. slave_offset = CPSW2_SLAVE_OFFSET;
  1725. slave_size = CPSW2_SLAVE_SIZE;
  1726. sliver_offset = CPSW2_SLIVER_OFFSET;
  1727. dma_params.desc_mem_phys =
  1728. (u32 __force) priv->cpsw_res->start + CPSW2_BD_OFFSET;
  1729. break;
  1730. default:
  1731. dev_err(priv->dev, "unknown version 0x%08x\n", priv->version);
  1732. ret = -ENODEV;
  1733. goto clean_cpsw_wr_iores_ret;
  1734. }
  1735. for (i = 0; i < priv->data.slaves; i++) {
  1736. struct cpsw_slave *slave = &priv->slaves[i];
  1737. cpsw_slave_init(slave, priv, slave_offset, sliver_offset);
  1738. slave_offset += slave_size;
  1739. sliver_offset += SLIVER_SIZE;
  1740. }
  1741. dma_params.dev = &pdev->dev;
  1742. dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
  1743. dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
  1744. dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
  1745. dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
  1746. dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
  1747. dma_params.num_chan = data->channels;
  1748. dma_params.has_soft_reset = true;
  1749. dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
  1750. dma_params.desc_mem_size = data->bd_ram_size;
  1751. dma_params.desc_align = 16;
  1752. dma_params.has_ext_regs = true;
  1753. dma_params.desc_hw_addr = dma_params.desc_mem_phys;
  1754. priv->dma = cpdma_ctlr_create(&dma_params);
  1755. if (!priv->dma) {
  1756. dev_err(priv->dev, "error initializing dma\n");
  1757. ret = -ENOMEM;
  1758. goto clean_wr_iomap_ret;
  1759. }
  1760. priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
  1761. cpsw_tx_handler);
  1762. priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
  1763. cpsw_rx_handler);
  1764. if (WARN_ON(!priv->txch || !priv->rxch)) {
  1765. dev_err(priv->dev, "error initializing dma channels\n");
  1766. ret = -ENOMEM;
  1767. goto clean_dma_ret;
  1768. }
  1769. ale_params.dev = &ndev->dev;
  1770. ale_params.ale_ageout = ale_ageout;
  1771. ale_params.ale_entries = data->ale_entries;
  1772. ale_params.ale_ports = data->slaves;
  1773. priv->ale = cpsw_ale_create(&ale_params);
  1774. if (!priv->ale) {
  1775. dev_err(priv->dev, "error initializing ale engine\n");
  1776. ret = -ENODEV;
  1777. goto clean_dma_ret;
  1778. }
  1779. ndev->irq = platform_get_irq(pdev, 0);
  1780. if (ndev->irq < 0) {
  1781. dev_err(priv->dev, "error getting irq resource\n");
  1782. ret = -ENOENT;
  1783. goto clean_ale_ret;
  1784. }
  1785. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
  1786. for (i = res->start; i <= res->end; i++) {
  1787. if (request_irq(i, cpsw_interrupt, IRQF_DISABLED,
  1788. dev_name(&pdev->dev), priv)) {
  1789. dev_err(priv->dev, "error attaching irq\n");
  1790. goto clean_ale_ret;
  1791. }
  1792. priv->irqs_table[k] = i;
  1793. priv->num_irqs = k + 1;
  1794. }
  1795. k++;
  1796. }
  1797. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1798. ndev->netdev_ops = &cpsw_netdev_ops;
  1799. SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
  1800. netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
  1801. /* register the network device */
  1802. SET_NETDEV_DEV(ndev, &pdev->dev);
  1803. ret = register_netdev(ndev);
  1804. if (ret) {
  1805. dev_err(priv->dev, "error registering net device\n");
  1806. ret = -ENODEV;
  1807. goto clean_irq_ret;
  1808. }
  1809. if (cpts_register(&pdev->dev, priv->cpts,
  1810. data->cpts_clock_mult, data->cpts_clock_shift))
  1811. dev_err(priv->dev, "error registering cpts device\n");
  1812. cpsw_notice(priv, probe, "initialized device (regs %x, irq %d)\n",
  1813. priv->cpsw_res->start, ndev->irq);
  1814. if (priv->data.dual_emac) {
  1815. ret = cpsw_probe_dual_emac(pdev, priv);
  1816. if (ret) {
  1817. cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
  1818. goto clean_irq_ret;
  1819. }
  1820. }
  1821. return 0;
  1822. clean_irq_ret:
  1823. for (i = 0; i < priv->num_irqs; i++)
  1824. free_irq(priv->irqs_table[i], priv);
  1825. clean_ale_ret:
  1826. cpsw_ale_destroy(priv->ale);
  1827. clean_dma_ret:
  1828. cpdma_chan_destroy(priv->txch);
  1829. cpdma_chan_destroy(priv->rxch);
  1830. cpdma_ctlr_destroy(priv->dma);
  1831. clean_wr_iomap_ret:
  1832. iounmap(priv->wr_regs);
  1833. clean_cpsw_wr_iores_ret:
  1834. release_mem_region(priv->cpsw_wr_res->start,
  1835. resource_size(priv->cpsw_wr_res));
  1836. clean_iomap_ret:
  1837. iounmap(priv->regs);
  1838. clean_cpsw_iores_ret:
  1839. release_mem_region(priv->cpsw_res->start,
  1840. resource_size(priv->cpsw_res));
  1841. clean_clk_ret:
  1842. clk_put(priv->clk);
  1843. clean_slave_ret:
  1844. pm_runtime_disable(&pdev->dev);
  1845. kfree(priv->slaves);
  1846. clean_ndev_ret:
  1847. kfree(priv->data.slave_data);
  1848. free_netdev(priv->ndev);
  1849. return ret;
  1850. }
  1851. static int cpsw_remove(struct platform_device *pdev)
  1852. {
  1853. struct net_device *ndev = platform_get_drvdata(pdev);
  1854. struct cpsw_priv *priv = netdev_priv(ndev);
  1855. int i;
  1856. if (priv->data.dual_emac)
  1857. unregister_netdev(cpsw_get_slave_ndev(priv, 1));
  1858. unregister_netdev(ndev);
  1859. cpts_unregister(priv->cpts);
  1860. for (i = 0; i < priv->num_irqs; i++)
  1861. free_irq(priv->irqs_table[i], priv);
  1862. cpsw_ale_destroy(priv->ale);
  1863. cpdma_chan_destroy(priv->txch);
  1864. cpdma_chan_destroy(priv->rxch);
  1865. cpdma_ctlr_destroy(priv->dma);
  1866. iounmap(priv->regs);
  1867. release_mem_region(priv->cpsw_res->start,
  1868. resource_size(priv->cpsw_res));
  1869. iounmap(priv->wr_regs);
  1870. release_mem_region(priv->cpsw_wr_res->start,
  1871. resource_size(priv->cpsw_wr_res));
  1872. pm_runtime_disable(&pdev->dev);
  1873. clk_put(priv->clk);
  1874. kfree(priv->slaves);
  1875. kfree(priv->data.slave_data);
  1876. if (priv->data.dual_emac)
  1877. free_netdev(cpsw_get_slave_ndev(priv, 1));
  1878. free_netdev(ndev);
  1879. return 0;
  1880. }
  1881. static int cpsw_suspend(struct device *dev)
  1882. {
  1883. struct platform_device *pdev = to_platform_device(dev);
  1884. struct net_device *ndev = platform_get_drvdata(pdev);
  1885. struct cpsw_priv *priv = netdev_priv(ndev);
  1886. if (netif_running(ndev))
  1887. cpsw_ndo_stop(ndev);
  1888. soft_reset("sliver 0", &priv->slaves[0].sliver->soft_reset);
  1889. soft_reset("sliver 1", &priv->slaves[1].sliver->soft_reset);
  1890. pm_runtime_put_sync(&pdev->dev);
  1891. /* Select sleep pin state */
  1892. pinctrl_pm_select_sleep_state(&pdev->dev);
  1893. return 0;
  1894. }
  1895. static int cpsw_resume(struct device *dev)
  1896. {
  1897. struct platform_device *pdev = to_platform_device(dev);
  1898. struct net_device *ndev = platform_get_drvdata(pdev);
  1899. pm_runtime_get_sync(&pdev->dev);
  1900. /* Select default pin state */
  1901. pinctrl_pm_select_default_state(&pdev->dev);
  1902. if (netif_running(ndev))
  1903. cpsw_ndo_open(ndev);
  1904. return 0;
  1905. }
  1906. static const struct dev_pm_ops cpsw_pm_ops = {
  1907. .suspend = cpsw_suspend,
  1908. .resume = cpsw_resume,
  1909. };
  1910. static const struct of_device_id cpsw_of_mtable[] = {
  1911. { .compatible = "ti,cpsw", },
  1912. { /* sentinel */ },
  1913. };
  1914. MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
  1915. static struct platform_driver cpsw_driver = {
  1916. .driver = {
  1917. .name = "cpsw",
  1918. .owner = THIS_MODULE,
  1919. .pm = &cpsw_pm_ops,
  1920. .of_match_table = of_match_ptr(cpsw_of_mtable),
  1921. },
  1922. .probe = cpsw_probe,
  1923. .remove = cpsw_remove,
  1924. };
  1925. static int __init cpsw_init(void)
  1926. {
  1927. return platform_driver_register(&cpsw_driver);
  1928. }
  1929. late_initcall(cpsw_init);
  1930. static void __exit cpsw_exit(void)
  1931. {
  1932. platform_driver_unregister(&cpsw_driver);
  1933. }
  1934. module_exit(cpsw_exit);
  1935. MODULE_LICENSE("GPL");
  1936. MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
  1937. MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
  1938. MODULE_DESCRIPTION("TI CPSW Ethernet driver");