cpsw.c 53 KB

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