cpsw.c 50 KB

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