cpsw.c 53 KB

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