cpsw.c 55 KB

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