cpsw.c 54 KB

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