cpsw.c 49 KB

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