cpsw.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563
  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/mfd/syscon.h>
  36. #include <linux/regmap.h>
  37. #include <linux/pinctrl/consumer.h>
  38. #include "cpsw.h"
  39. #include "cpsw_ale.h"
  40. #include "cpts.h"
  41. #include "davinci_cpdma.h"
  42. #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
  43. NETIF_MSG_DRV | NETIF_MSG_LINK | \
  44. NETIF_MSG_IFUP | NETIF_MSG_INTR | \
  45. NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
  46. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
  47. NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
  48. NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
  49. NETIF_MSG_RX_STATUS)
  50. #define cpsw_info(priv, type, format, ...) \
  51. do { \
  52. if (netif_msg_##type(priv) && net_ratelimit()) \
  53. dev_info(priv->dev, format, ## __VA_ARGS__); \
  54. } while (0)
  55. #define cpsw_err(priv, type, format, ...) \
  56. do { \
  57. if (netif_msg_##type(priv) && net_ratelimit()) \
  58. dev_err(priv->dev, format, ## __VA_ARGS__); \
  59. } while (0)
  60. #define cpsw_dbg(priv, type, format, ...) \
  61. do { \
  62. if (netif_msg_##type(priv) && net_ratelimit()) \
  63. dev_dbg(priv->dev, format, ## __VA_ARGS__); \
  64. } while (0)
  65. #define cpsw_notice(priv, type, format, ...) \
  66. do { \
  67. if (netif_msg_##type(priv) && net_ratelimit()) \
  68. dev_notice(priv->dev, format, ## __VA_ARGS__); \
  69. } while (0)
  70. #define ALE_ALL_PORTS 0x7
  71. #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
  72. #define CPSW_MINOR_VERSION(reg) (reg & 0xff)
  73. #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
  74. #define CPSW_VERSION_1 0x19010a
  75. #define CPSW_VERSION_2 0x19010c
  76. #define CPSW_VERSION_3 0x19010f
  77. #define CPSW_VERSION_4 0x190112
  78. #define HOST_PORT_NUM 0
  79. #define SLIVER_SIZE 0x40
  80. #define CPSW1_HOST_PORT_OFFSET 0x028
  81. #define CPSW1_SLAVE_OFFSET 0x050
  82. #define CPSW1_SLAVE_SIZE 0x040
  83. #define CPSW1_CPDMA_OFFSET 0x100
  84. #define CPSW1_STATERAM_OFFSET 0x200
  85. #define CPSW1_HW_STATS 0x400
  86. #define CPSW1_CPTS_OFFSET 0x500
  87. #define CPSW1_ALE_OFFSET 0x600
  88. #define CPSW1_SLIVER_OFFSET 0x700
  89. #define CPSW2_HOST_PORT_OFFSET 0x108
  90. #define CPSW2_SLAVE_OFFSET 0x200
  91. #define CPSW2_SLAVE_SIZE 0x100
  92. #define CPSW2_CPDMA_OFFSET 0x800
  93. #define CPSW2_HW_STATS 0x900
  94. #define CPSW2_STATERAM_OFFSET 0xa00
  95. #define CPSW2_CPTS_OFFSET 0xc00
  96. #define CPSW2_ALE_OFFSET 0xd00
  97. #define CPSW2_SLIVER_OFFSET 0xd80
  98. #define CPSW2_BD_OFFSET 0x2000
  99. #define CPDMA_RXTHRESH 0x0c0
  100. #define CPDMA_RXFREE 0x0e0
  101. #define CPDMA_TXHDP 0x00
  102. #define CPDMA_RXHDP 0x20
  103. #define CPDMA_TXCP 0x40
  104. #define CPDMA_RXCP 0x60
  105. #define CPSW_POLL_WEIGHT 64
  106. #define CPSW_MIN_PACKET_SIZE 60
  107. #define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
  108. #define RX_PRIORITY_MAPPING 0x76543210
  109. #define TX_PRIORITY_MAPPING 0x33221100
  110. #define CPDMA_TX_PRIORITY_MAP 0x76543210
  111. #define CPSW_VLAN_AWARE BIT(1)
  112. #define CPSW_ALE_VLAN_AWARE 1
  113. #define CPSW_FIFO_NORMAL_MODE (0 << 16)
  114. #define CPSW_FIFO_DUAL_MAC_MODE (1 << 16)
  115. #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 16)
  116. #define CPSW_INTPACEEN (0x3f << 16)
  117. #define CPSW_INTPRESCALE_MASK (0x7FF << 0)
  118. #define CPSW_CMINTMAX_CNT 63
  119. #define CPSW_CMINTMIN_CNT 2
  120. #define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT)
  121. #define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1)
  122. #define cpsw_enable_irq(priv) \
  123. do { \
  124. u32 i; \
  125. for (i = 0; i < priv->num_irqs; i++) \
  126. enable_irq(priv->irqs_table[i]); \
  127. } while (0)
  128. #define cpsw_disable_irq(priv) \
  129. do { \
  130. u32 i; \
  131. for (i = 0; i < priv->num_irqs; i++) \
  132. disable_irq_nosync(priv->irqs_table[i]); \
  133. } while (0)
  134. #define cpsw_slave_index(priv) \
  135. ((priv->data.dual_emac) ? priv->emac_port : \
  136. priv->data.active_slave)
  137. static int debug_level;
  138. module_param(debug_level, int, 0);
  139. MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
  140. static int ale_ageout = 10;
  141. module_param(ale_ageout, int, 0);
  142. MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
  143. static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
  144. module_param(rx_packet_max, int, 0);
  145. MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
  146. struct cpsw_wr_regs {
  147. u32 id_ver;
  148. u32 soft_reset;
  149. u32 control;
  150. u32 int_control;
  151. u32 rx_thresh_en;
  152. u32 rx_en;
  153. u32 tx_en;
  154. u32 misc_en;
  155. u32 mem_allign1[8];
  156. u32 rx_thresh_stat;
  157. u32 rx_stat;
  158. u32 tx_stat;
  159. u32 misc_stat;
  160. u32 mem_allign2[8];
  161. u32 rx_imax;
  162. u32 tx_imax;
  163. };
  164. struct cpsw_ss_regs {
  165. u32 id_ver;
  166. u32 control;
  167. u32 soft_reset;
  168. u32 stat_port_en;
  169. u32 ptype;
  170. u32 soft_idle;
  171. u32 thru_rate;
  172. u32 gap_thresh;
  173. u32 tx_start_wds;
  174. u32 flow_control;
  175. u32 vlan_ltype;
  176. u32 ts_ltype;
  177. u32 dlr_ltype;
  178. };
  179. /* CPSW_PORT_V1 */
  180. #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
  181. #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
  182. #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
  183. #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
  184. #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
  185. #define CPSW1_TS_CTL 0x14 /* Time Sync Control */
  186. #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
  187. #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
  188. /* CPSW_PORT_V2 */
  189. #define CPSW2_CONTROL 0x00 /* Control Register */
  190. #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
  191. #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
  192. #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
  193. #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
  194. #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
  195. #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
  196. /* CPSW_PORT_V1 and V2 */
  197. #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
  198. #define SA_HI 0x24 /* CPGMAC_SL Source Address High */
  199. #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
  200. /* CPSW_PORT_V2 only */
  201. #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
  202. #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
  203. #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
  204. #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
  205. #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
  206. #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
  207. #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
  208. #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
  209. /* Bit definitions for the CPSW2_CONTROL register */
  210. #define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
  211. #define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
  212. #define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
  213. #define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
  214. #define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
  215. #define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
  216. #define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
  217. #define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
  218. #define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
  219. #define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
  220. #define TS_TTL_NONZERO (1<<8) /* Time Sync Time To Live Non-zero enable */
  221. #define TS_ANNEX_F_EN (1<<6) /* Time Sync Annex F enable */
  222. #define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
  223. #define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
  224. #define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
  225. #define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
  226. #define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
  227. #define CTRL_V2_TS_BITS \
  228. (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
  229. TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN)
  230. #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
  231. #define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN)
  232. #define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN)
  233. #define CTRL_V3_TS_BITS \
  234. (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
  235. TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
  236. TS_LTYPE1_EN)
  237. #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
  238. #define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN)
  239. #define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN)
  240. /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
  241. #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
  242. #define TS_SEQ_ID_OFFSET_MASK (0x3f)
  243. #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
  244. #define TS_MSG_TYPE_EN_MASK (0xffff)
  245. /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
  246. #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
  247. /* Bit definitions for the CPSW1_TS_CTL register */
  248. #define CPSW_V1_TS_RX_EN BIT(0)
  249. #define CPSW_V1_TS_TX_EN BIT(4)
  250. #define CPSW_V1_MSG_TYPE_OFS 16
  251. /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
  252. #define CPSW_V1_SEQ_ID_OFS_SHIFT 16
  253. struct cpsw_host_regs {
  254. u32 max_blks;
  255. u32 blk_cnt;
  256. u32 tx_in_ctl;
  257. u32 port_vlan;
  258. u32 tx_pri_map;
  259. u32 cpdma_tx_pri_map;
  260. u32 cpdma_rx_chan_map;
  261. };
  262. struct cpsw_sliver_regs {
  263. u32 id_ver;
  264. u32 mac_control;
  265. u32 mac_status;
  266. u32 soft_reset;
  267. u32 rx_maxlen;
  268. u32 __reserved_0;
  269. u32 rx_pause;
  270. u32 tx_pause;
  271. u32 __reserved_1;
  272. u32 rx_pri_map;
  273. };
  274. struct cpsw_hw_stats {
  275. u32 rxgoodframes;
  276. u32 rxbroadcastframes;
  277. u32 rxmulticastframes;
  278. u32 rxpauseframes;
  279. u32 rxcrcerrors;
  280. u32 rxaligncodeerrors;
  281. u32 rxoversizedframes;
  282. u32 rxjabberframes;
  283. u32 rxundersizedframes;
  284. u32 rxfragments;
  285. u32 __pad_0[2];
  286. u32 rxoctets;
  287. u32 txgoodframes;
  288. u32 txbroadcastframes;
  289. u32 txmulticastframes;
  290. u32 txpauseframes;
  291. u32 txdeferredframes;
  292. u32 txcollisionframes;
  293. u32 txsinglecollframes;
  294. u32 txmultcollframes;
  295. u32 txexcessivecollisions;
  296. u32 txlatecollisions;
  297. u32 txunderrun;
  298. u32 txcarriersenseerrors;
  299. u32 txoctets;
  300. u32 octetframes64;
  301. u32 octetframes65t127;
  302. u32 octetframes128t255;
  303. u32 octetframes256t511;
  304. u32 octetframes512t1023;
  305. u32 octetframes1024tup;
  306. u32 netoctets;
  307. u32 rxsofoverruns;
  308. u32 rxmofoverruns;
  309. u32 rxdmaoverruns;
  310. };
  311. struct cpsw_slave {
  312. void __iomem *regs;
  313. struct cpsw_sliver_regs __iomem *sliver;
  314. int slave_num;
  315. u32 mac_control;
  316. struct cpsw_slave_data *data;
  317. struct phy_device *phy;
  318. struct net_device *ndev;
  319. u32 port_vlan;
  320. u32 open_stat;
  321. };
  322. static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
  323. {
  324. return __raw_readl(slave->regs + offset);
  325. }
  326. static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
  327. {
  328. __raw_writel(val, slave->regs + offset);
  329. }
  330. struct cpsw_priv {
  331. spinlock_t lock;
  332. struct platform_device *pdev;
  333. struct net_device *ndev;
  334. struct napi_struct napi;
  335. struct device *dev;
  336. struct cpsw_platform_data data;
  337. struct cpsw_ss_regs __iomem *regs;
  338. struct cpsw_wr_regs __iomem *wr_regs;
  339. u8 __iomem *hw_stats;
  340. struct cpsw_host_regs __iomem *host_port_regs;
  341. u32 msg_enable;
  342. u32 version;
  343. u32 coal_intvl;
  344. u32 bus_freq_mhz;
  345. int rx_packet_max;
  346. int host_port;
  347. struct clk *clk;
  348. u8 mac_addr[ETH_ALEN];
  349. struct cpsw_slave *slaves;
  350. struct cpdma_ctlr *dma;
  351. struct cpdma_chan *txch, *rxch;
  352. struct cpsw_ale *ale;
  353. bool rx_pause;
  354. bool tx_pause;
  355. /* snapshot of IRQ numbers */
  356. u32 irqs_table[4];
  357. u32 num_irqs;
  358. bool irq_enabled;
  359. struct cpts *cpts;
  360. u32 emac_port;
  361. };
  362. struct cpsw_stats {
  363. char stat_string[ETH_GSTRING_LEN];
  364. int type;
  365. int sizeof_stat;
  366. int stat_offset;
  367. };
  368. enum {
  369. CPSW_STATS,
  370. CPDMA_RX_STATS,
  371. CPDMA_TX_STATS,
  372. };
  373. #define CPSW_STAT(m) CPSW_STATS, \
  374. sizeof(((struct cpsw_hw_stats *)0)->m), \
  375. offsetof(struct cpsw_hw_stats, m)
  376. #define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
  377. sizeof(((struct cpdma_chan_stats *)0)->m), \
  378. offsetof(struct cpdma_chan_stats, m)
  379. #define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
  380. sizeof(((struct cpdma_chan_stats *)0)->m), \
  381. offsetof(struct cpdma_chan_stats, m)
  382. static const struct cpsw_stats cpsw_gstrings_stats[] = {
  383. { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
  384. { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
  385. { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
  386. { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
  387. { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
  388. { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
  389. { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
  390. { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
  391. { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
  392. { "Rx Fragments", CPSW_STAT(rxfragments) },
  393. { "Rx Octets", CPSW_STAT(rxoctets) },
  394. { "Good Tx Frames", CPSW_STAT(txgoodframes) },
  395. { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
  396. { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
  397. { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
  398. { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
  399. { "Collisions", CPSW_STAT(txcollisionframes) },
  400. { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
  401. { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
  402. { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
  403. { "Late Collisions", CPSW_STAT(txlatecollisions) },
  404. { "Tx Underrun", CPSW_STAT(txunderrun) },
  405. { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
  406. { "Tx Octets", CPSW_STAT(txoctets) },
  407. { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
  408. { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
  409. { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
  410. { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
  411. { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
  412. { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
  413. { "Net Octets", CPSW_STAT(netoctets) },
  414. { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
  415. { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
  416. { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
  417. { "Rx DMA chan: head_enqueue", CPDMA_RX_STAT(head_enqueue) },
  418. { "Rx DMA chan: tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
  419. { "Rx DMA chan: pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
  420. { "Rx DMA chan: misqueued", CPDMA_RX_STAT(misqueued) },
  421. { "Rx DMA chan: desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
  422. { "Rx DMA chan: pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
  423. { "Rx DMA chan: runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
  424. { "Rx DMA chan: runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
  425. { "Rx DMA chan: empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
  426. { "Rx DMA chan: busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
  427. { "Rx DMA chan: good_dequeue", CPDMA_RX_STAT(good_dequeue) },
  428. { "Rx DMA chan: requeue", CPDMA_RX_STAT(requeue) },
  429. { "Rx DMA chan: teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
  430. { "Tx DMA chan: head_enqueue", CPDMA_TX_STAT(head_enqueue) },
  431. { "Tx DMA chan: tail_enqueue", CPDMA_TX_STAT(tail_enqueue) },
  432. { "Tx DMA chan: pad_enqueue", CPDMA_TX_STAT(pad_enqueue) },
  433. { "Tx DMA chan: misqueued", CPDMA_TX_STAT(misqueued) },
  434. { "Tx DMA chan: desc_alloc_fail", CPDMA_TX_STAT(desc_alloc_fail) },
  435. { "Tx DMA chan: pad_alloc_fail", CPDMA_TX_STAT(pad_alloc_fail) },
  436. { "Tx DMA chan: runt_receive_buf", CPDMA_TX_STAT(runt_receive_buff) },
  437. { "Tx DMA chan: runt_transmit_buf", CPDMA_TX_STAT(runt_transmit_buff) },
  438. { "Tx DMA chan: empty_dequeue", CPDMA_TX_STAT(empty_dequeue) },
  439. { "Tx DMA chan: busy_dequeue", CPDMA_TX_STAT(busy_dequeue) },
  440. { "Tx DMA chan: good_dequeue", CPDMA_TX_STAT(good_dequeue) },
  441. { "Tx DMA chan: requeue", CPDMA_TX_STAT(requeue) },
  442. { "Tx DMA chan: teardown_dequeue", CPDMA_TX_STAT(teardown_dequeue) },
  443. };
  444. #define CPSW_STATS_LEN ARRAY_SIZE(cpsw_gstrings_stats)
  445. #define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
  446. #define for_each_slave(priv, func, arg...) \
  447. do { \
  448. struct cpsw_slave *slave; \
  449. int n; \
  450. if (priv->data.dual_emac) \
  451. (func)((priv)->slaves + priv->emac_port, ##arg);\
  452. else \
  453. for (n = (priv)->data.slaves, \
  454. slave = (priv)->slaves; \
  455. n; n--) \
  456. (func)(slave++, ##arg); \
  457. } while (0)
  458. #define cpsw_get_slave_ndev(priv, __slave_no__) \
  459. (priv->slaves[__slave_no__].ndev)
  460. #define cpsw_get_slave_priv(priv, __slave_no__) \
  461. ((priv->slaves[__slave_no__].ndev) ? \
  462. netdev_priv(priv->slaves[__slave_no__].ndev) : NULL) \
  463. #define cpsw_dual_emac_src_port_detect(status, priv, ndev, skb) \
  464. do { \
  465. if (!priv->data.dual_emac) \
  466. break; \
  467. if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
  468. ndev = cpsw_get_slave_ndev(priv, 0); \
  469. priv = netdev_priv(ndev); \
  470. skb->dev = ndev; \
  471. } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
  472. ndev = cpsw_get_slave_ndev(priv, 1); \
  473. priv = netdev_priv(ndev); \
  474. skb->dev = ndev; \
  475. } \
  476. } while (0)
  477. #define cpsw_add_mcast(priv, addr) \
  478. do { \
  479. if (priv->data.dual_emac) { \
  480. struct cpsw_slave *slave = priv->slaves + \
  481. priv->emac_port; \
  482. int slave_port = cpsw_get_slave_port(priv, \
  483. slave->slave_num); \
  484. cpsw_ale_add_mcast(priv->ale, addr, \
  485. 1 << slave_port | 1 << priv->host_port, \
  486. ALE_VLAN, slave->port_vlan, 0); \
  487. } else { \
  488. cpsw_ale_add_mcast(priv->ale, addr, \
  489. ALE_ALL_PORTS << priv->host_port, \
  490. 0, 0, 0); \
  491. } \
  492. } while (0)
  493. static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
  494. {
  495. if (priv->host_port == 0)
  496. return slave_num + 1;
  497. else
  498. return slave_num;
  499. }
  500. static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
  501. {
  502. struct cpsw_priv *priv = netdev_priv(ndev);
  503. struct cpsw_ale *ale = priv->ale;
  504. int i;
  505. if (priv->data.dual_emac) {
  506. bool flag = false;
  507. /* Enabling promiscuous mode for one interface will be
  508. * common for both the interface as the interface shares
  509. * the same hardware resource.
  510. */
  511. for (i = 0; i < priv->data.slaves; i++)
  512. if (priv->slaves[i].ndev->flags & IFF_PROMISC)
  513. flag = true;
  514. if (!enable && flag) {
  515. enable = true;
  516. dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
  517. }
  518. if (enable) {
  519. /* Enable Bypass */
  520. cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
  521. dev_dbg(&ndev->dev, "promiscuity enabled\n");
  522. } else {
  523. /* Disable Bypass */
  524. cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
  525. dev_dbg(&ndev->dev, "promiscuity disabled\n");
  526. }
  527. } else {
  528. if (enable) {
  529. unsigned long timeout = jiffies + HZ;
  530. /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
  531. for (i = 0; i <= priv->data.slaves; i++) {
  532. cpsw_ale_control_set(ale, i,
  533. ALE_PORT_NOLEARN, 1);
  534. cpsw_ale_control_set(ale, i,
  535. ALE_PORT_NO_SA_UPDATE, 1);
  536. }
  537. /* Clear All Untouched entries */
  538. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  539. do {
  540. cpu_relax();
  541. if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
  542. break;
  543. } while (time_after(timeout, jiffies));
  544. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  545. /* Clear all mcast from ALE */
  546. cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS <<
  547. priv->host_port, -1);
  548. /* Flood All Unicast Packets to Host port */
  549. cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
  550. dev_dbg(&ndev->dev, "promiscuity enabled\n");
  551. } else {
  552. /* Don't Flood All Unicast Packets to Host port */
  553. cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
  554. /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
  555. for (i = 0; i <= priv->data.slaves; i++) {
  556. cpsw_ale_control_set(ale, i,
  557. ALE_PORT_NOLEARN, 0);
  558. cpsw_ale_control_set(ale, i,
  559. ALE_PORT_NO_SA_UPDATE, 0);
  560. }
  561. dev_dbg(&ndev->dev, "promiscuity disabled\n");
  562. }
  563. }
  564. }
  565. static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
  566. {
  567. struct cpsw_priv *priv = netdev_priv(ndev);
  568. int vid;
  569. if (priv->data.dual_emac)
  570. vid = priv->slaves[priv->emac_port].port_vlan;
  571. else
  572. vid = priv->data.default_vlan;
  573. if (ndev->flags & IFF_PROMISC) {
  574. /* Enable promiscuous mode */
  575. cpsw_set_promiscious(ndev, true);
  576. cpsw_ale_set_allmulti(priv->ale, IFF_ALLMULTI);
  577. return;
  578. } else {
  579. /* Disable promiscuous mode */
  580. cpsw_set_promiscious(ndev, false);
  581. }
  582. /* Restore allmulti on vlans if necessary */
  583. cpsw_ale_set_allmulti(priv->ale, priv->ndev->flags & IFF_ALLMULTI);
  584. /* Clear all mcast from ALE */
  585. cpsw_ale_flush_multicast(priv->ale, ALE_ALL_PORTS << priv->host_port,
  586. vid);
  587. if (!netdev_mc_empty(ndev)) {
  588. struct netdev_hw_addr *ha;
  589. /* program multicast address list into ALE register */
  590. netdev_for_each_mc_addr(ha, ndev) {
  591. cpsw_add_mcast(priv, (u8 *)ha->addr);
  592. }
  593. }
  594. }
  595. static void cpsw_intr_enable(struct cpsw_priv *priv)
  596. {
  597. __raw_writel(0xFF, &priv->wr_regs->tx_en);
  598. __raw_writel(0xFF, &priv->wr_regs->rx_en);
  599. cpdma_ctlr_int_ctrl(priv->dma, true);
  600. return;
  601. }
  602. static void cpsw_intr_disable(struct cpsw_priv *priv)
  603. {
  604. __raw_writel(0, &priv->wr_regs->tx_en);
  605. __raw_writel(0, &priv->wr_regs->rx_en);
  606. cpdma_ctlr_int_ctrl(priv->dma, false);
  607. return;
  608. }
  609. static void cpsw_tx_handler(void *token, int len, int status)
  610. {
  611. struct sk_buff *skb = token;
  612. struct net_device *ndev = skb->dev;
  613. struct cpsw_priv *priv = netdev_priv(ndev);
  614. /* Check whether the queue is stopped due to stalled tx dma, if the
  615. * queue is stopped then start the queue as we have free desc for tx
  616. */
  617. if (unlikely(netif_queue_stopped(ndev)))
  618. netif_wake_queue(ndev);
  619. cpts_tx_timestamp(priv->cpts, skb);
  620. ndev->stats.tx_packets++;
  621. ndev->stats.tx_bytes += len;
  622. dev_kfree_skb_any(skb);
  623. }
  624. static void cpsw_rx_handler(void *token, int len, int status)
  625. {
  626. struct sk_buff *skb = token;
  627. struct sk_buff *new_skb;
  628. struct net_device *ndev = skb->dev;
  629. struct cpsw_priv *priv = netdev_priv(ndev);
  630. int ret = 0;
  631. cpsw_dual_emac_src_port_detect(status, priv, ndev, skb);
  632. if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
  633. bool ndev_status = false;
  634. struct cpsw_slave *slave = priv->slaves;
  635. int n;
  636. if (priv->data.dual_emac) {
  637. /* In dual emac mode check for all interfaces */
  638. for (n = priv->data.slaves; n; n--, slave++)
  639. if (netif_running(slave->ndev))
  640. ndev_status = true;
  641. }
  642. if (ndev_status && (status >= 0)) {
  643. /* The packet received is for the interface which
  644. * is already down and the other interface is up
  645. * and running, intead of freeing which results
  646. * in reducing of the number of rx descriptor in
  647. * DMA engine, requeue skb back to cpdma.
  648. */
  649. new_skb = skb;
  650. goto requeue;
  651. }
  652. /* the interface is going down, skbs are purged */
  653. dev_kfree_skb_any(skb);
  654. return;
  655. }
  656. new_skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
  657. if (new_skb) {
  658. skb_put(skb, len);
  659. cpts_rx_timestamp(priv->cpts, skb);
  660. skb->protocol = eth_type_trans(skb, ndev);
  661. netif_receive_skb(skb);
  662. ndev->stats.rx_bytes += len;
  663. ndev->stats.rx_packets++;
  664. } else {
  665. ndev->stats.rx_dropped++;
  666. new_skb = skb;
  667. }
  668. requeue:
  669. ret = cpdma_chan_submit(priv->rxch, new_skb, new_skb->data,
  670. skb_tailroom(new_skb), 0);
  671. if (WARN_ON(ret < 0))
  672. dev_kfree_skb_any(new_skb);
  673. }
  674. static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
  675. {
  676. struct cpsw_priv *priv = dev_id;
  677. int value = irq - priv->irqs_table[0];
  678. /* NOTICE: Ending IRQ here. The trick with the 'value' variable above
  679. * is to make sure we will always write the correct value to the EOI
  680. * register. Namely 0 for RX_THRESH Interrupt, 1 for RX Interrupt, 2
  681. * for TX Interrupt and 3 for MISC Interrupt.
  682. */
  683. cpdma_ctlr_eoi(priv->dma, value);
  684. cpsw_intr_disable(priv);
  685. if (priv->irq_enabled == true) {
  686. cpsw_disable_irq(priv);
  687. priv->irq_enabled = false;
  688. }
  689. if (netif_running(priv->ndev)) {
  690. napi_schedule(&priv->napi);
  691. return IRQ_HANDLED;
  692. }
  693. priv = cpsw_get_slave_priv(priv, 1);
  694. if (!priv)
  695. return IRQ_NONE;
  696. if (netif_running(priv->ndev)) {
  697. napi_schedule(&priv->napi);
  698. return IRQ_HANDLED;
  699. }
  700. return IRQ_NONE;
  701. }
  702. static int cpsw_poll(struct napi_struct *napi, int budget)
  703. {
  704. struct cpsw_priv *priv = napi_to_priv(napi);
  705. int num_tx, num_rx;
  706. num_tx = cpdma_chan_process(priv->txch, 128);
  707. num_rx = cpdma_chan_process(priv->rxch, budget);
  708. if (num_rx < budget) {
  709. struct cpsw_priv *prim_cpsw;
  710. napi_complete(napi);
  711. cpsw_intr_enable(priv);
  712. prim_cpsw = cpsw_get_slave_priv(priv, 0);
  713. if (prim_cpsw->irq_enabled == false) {
  714. prim_cpsw->irq_enabled = true;
  715. cpsw_enable_irq(priv);
  716. }
  717. }
  718. if (num_rx || num_tx)
  719. cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n",
  720. num_rx, num_tx);
  721. return num_rx;
  722. }
  723. static inline void soft_reset(const char *module, void __iomem *reg)
  724. {
  725. unsigned long timeout = jiffies + HZ;
  726. __raw_writel(1, reg);
  727. do {
  728. cpu_relax();
  729. } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
  730. WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
  731. }
  732. #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
  733. ((mac)[2] << 16) | ((mac)[3] << 24))
  734. #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
  735. static void cpsw_set_slave_mac(struct cpsw_slave *slave,
  736. struct cpsw_priv *priv)
  737. {
  738. slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
  739. slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
  740. }
  741. static void _cpsw_adjust_link(struct cpsw_slave *slave,
  742. struct cpsw_priv *priv, bool *link)
  743. {
  744. struct phy_device *phy = slave->phy;
  745. u32 mac_control = 0;
  746. u32 slave_port;
  747. if (!phy)
  748. return;
  749. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  750. if (phy->link) {
  751. mac_control = priv->data.mac_control;
  752. /* enable forwarding */
  753. cpsw_ale_control_set(priv->ale, slave_port,
  754. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  755. if (phy->speed == 1000)
  756. mac_control |= BIT(7); /* GIGABITEN */
  757. if (phy->duplex)
  758. mac_control |= BIT(0); /* FULLDUPLEXEN */
  759. /* set speed_in input in case RMII mode is used in 100Mbps */
  760. if (phy->speed == 100)
  761. mac_control |= BIT(15);
  762. else if (phy->speed == 10)
  763. mac_control |= BIT(18); /* In Band mode */
  764. if (priv->rx_pause)
  765. mac_control |= BIT(3);
  766. if (priv->tx_pause)
  767. mac_control |= BIT(4);
  768. *link = true;
  769. } else {
  770. mac_control = 0;
  771. /* disable forwarding */
  772. cpsw_ale_control_set(priv->ale, slave_port,
  773. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  774. }
  775. if (mac_control != slave->mac_control) {
  776. phy_print_status(phy);
  777. __raw_writel(mac_control, &slave->sliver->mac_control);
  778. }
  779. slave->mac_control = mac_control;
  780. }
  781. static void cpsw_adjust_link(struct net_device *ndev)
  782. {
  783. struct cpsw_priv *priv = netdev_priv(ndev);
  784. bool link = false;
  785. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  786. if (link) {
  787. netif_carrier_on(ndev);
  788. if (netif_running(ndev))
  789. netif_wake_queue(ndev);
  790. } else {
  791. netif_carrier_off(ndev);
  792. netif_stop_queue(ndev);
  793. }
  794. }
  795. static int cpsw_get_coalesce(struct net_device *ndev,
  796. struct ethtool_coalesce *coal)
  797. {
  798. struct cpsw_priv *priv = netdev_priv(ndev);
  799. coal->rx_coalesce_usecs = priv->coal_intvl;
  800. return 0;
  801. }
  802. static int cpsw_set_coalesce(struct net_device *ndev,
  803. struct ethtool_coalesce *coal)
  804. {
  805. struct cpsw_priv *priv = netdev_priv(ndev);
  806. u32 int_ctrl;
  807. u32 num_interrupts = 0;
  808. u32 prescale = 0;
  809. u32 addnl_dvdr = 1;
  810. u32 coal_intvl = 0;
  811. coal_intvl = coal->rx_coalesce_usecs;
  812. int_ctrl = readl(&priv->wr_regs->int_control);
  813. prescale = priv->bus_freq_mhz * 4;
  814. if (!coal->rx_coalesce_usecs) {
  815. int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
  816. goto update_return;
  817. }
  818. if (coal_intvl < CPSW_CMINTMIN_INTVL)
  819. coal_intvl = CPSW_CMINTMIN_INTVL;
  820. if (coal_intvl > CPSW_CMINTMAX_INTVL) {
  821. /* Interrupt pacer works with 4us Pulse, we can
  822. * throttle further by dilating the 4us pulse.
  823. */
  824. addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
  825. if (addnl_dvdr > 1) {
  826. prescale *= addnl_dvdr;
  827. if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
  828. coal_intvl = (CPSW_CMINTMAX_INTVL
  829. * addnl_dvdr);
  830. } else {
  831. addnl_dvdr = 1;
  832. coal_intvl = CPSW_CMINTMAX_INTVL;
  833. }
  834. }
  835. num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
  836. writel(num_interrupts, &priv->wr_regs->rx_imax);
  837. writel(num_interrupts, &priv->wr_regs->tx_imax);
  838. int_ctrl |= CPSW_INTPACEEN;
  839. int_ctrl &= (~CPSW_INTPRESCALE_MASK);
  840. int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
  841. update_return:
  842. writel(int_ctrl, &priv->wr_regs->int_control);
  843. cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
  844. if (priv->data.dual_emac) {
  845. int i;
  846. for (i = 0; i < priv->data.slaves; i++) {
  847. priv = netdev_priv(priv->slaves[i].ndev);
  848. priv->coal_intvl = coal_intvl;
  849. }
  850. } else {
  851. priv->coal_intvl = coal_intvl;
  852. }
  853. return 0;
  854. }
  855. static int cpsw_get_sset_count(struct net_device *ndev, int sset)
  856. {
  857. switch (sset) {
  858. case ETH_SS_STATS:
  859. return CPSW_STATS_LEN;
  860. default:
  861. return -EOPNOTSUPP;
  862. }
  863. }
  864. static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  865. {
  866. u8 *p = data;
  867. int i;
  868. switch (stringset) {
  869. case ETH_SS_STATS:
  870. for (i = 0; i < CPSW_STATS_LEN; i++) {
  871. memcpy(p, cpsw_gstrings_stats[i].stat_string,
  872. ETH_GSTRING_LEN);
  873. p += ETH_GSTRING_LEN;
  874. }
  875. break;
  876. }
  877. }
  878. static void cpsw_get_ethtool_stats(struct net_device *ndev,
  879. struct ethtool_stats *stats, u64 *data)
  880. {
  881. struct cpsw_priv *priv = netdev_priv(ndev);
  882. struct cpdma_chan_stats rx_stats;
  883. struct cpdma_chan_stats tx_stats;
  884. u32 val;
  885. u8 *p;
  886. int i;
  887. /* Collect Davinci CPDMA stats for Rx and Tx Channel */
  888. cpdma_chan_get_stats(priv->rxch, &rx_stats);
  889. cpdma_chan_get_stats(priv->txch, &tx_stats);
  890. for (i = 0; i < CPSW_STATS_LEN; i++) {
  891. switch (cpsw_gstrings_stats[i].type) {
  892. case CPSW_STATS:
  893. val = readl(priv->hw_stats +
  894. cpsw_gstrings_stats[i].stat_offset);
  895. data[i] = val;
  896. break;
  897. case CPDMA_RX_STATS:
  898. p = (u8 *)&rx_stats +
  899. cpsw_gstrings_stats[i].stat_offset;
  900. data[i] = *(u32 *)p;
  901. break;
  902. case CPDMA_TX_STATS:
  903. p = (u8 *)&tx_stats +
  904. cpsw_gstrings_stats[i].stat_offset;
  905. data[i] = *(u32 *)p;
  906. break;
  907. }
  908. }
  909. }
  910. static int cpsw_common_res_usage_state(struct cpsw_priv *priv)
  911. {
  912. u32 i;
  913. u32 usage_count = 0;
  914. if (!priv->data.dual_emac)
  915. return 0;
  916. for (i = 0; i < priv->data.slaves; i++)
  917. if (priv->slaves[i].open_stat)
  918. usage_count++;
  919. return usage_count;
  920. }
  921. static inline int cpsw_tx_packet_submit(struct net_device *ndev,
  922. struct cpsw_priv *priv, struct sk_buff *skb)
  923. {
  924. if (!priv->data.dual_emac)
  925. return cpdma_chan_submit(priv->txch, skb, skb->data,
  926. skb->len, 0);
  927. if (ndev == cpsw_get_slave_ndev(priv, 0))
  928. return cpdma_chan_submit(priv->txch, skb, skb->data,
  929. skb->len, 1);
  930. else
  931. return cpdma_chan_submit(priv->txch, skb, skb->data,
  932. skb->len, 2);
  933. }
  934. static inline void cpsw_add_dual_emac_def_ale_entries(
  935. struct cpsw_priv *priv, struct cpsw_slave *slave,
  936. u32 slave_port)
  937. {
  938. u32 port_mask = 1 << slave_port | 1 << priv->host_port;
  939. if (priv->version == CPSW_VERSION_1)
  940. slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
  941. else
  942. slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
  943. cpsw_ale_add_vlan(priv->ale, slave->port_vlan, port_mask,
  944. port_mask, port_mask, 0);
  945. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  946. port_mask, ALE_VLAN, slave->port_vlan, 0);
  947. cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
  948. priv->host_port, ALE_VLAN, slave->port_vlan);
  949. }
  950. static void soft_reset_slave(struct cpsw_slave *slave)
  951. {
  952. char name[32];
  953. snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
  954. soft_reset(name, &slave->sliver->soft_reset);
  955. }
  956. static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
  957. {
  958. u32 slave_port;
  959. soft_reset_slave(slave);
  960. /* setup priority mapping */
  961. __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
  962. switch (priv->version) {
  963. case CPSW_VERSION_1:
  964. slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
  965. break;
  966. case CPSW_VERSION_2:
  967. case CPSW_VERSION_3:
  968. case CPSW_VERSION_4:
  969. slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
  970. break;
  971. }
  972. /* setup max packet size, and mac address */
  973. __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
  974. cpsw_set_slave_mac(slave, priv);
  975. slave->mac_control = 0; /* no link yet */
  976. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  977. if (priv->data.dual_emac)
  978. cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
  979. else
  980. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  981. 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
  982. slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
  983. &cpsw_adjust_link, slave->data->phy_if);
  984. if (IS_ERR(slave->phy)) {
  985. dev_err(priv->dev, "phy %s not found on slave %d\n",
  986. slave->data->phy_id, slave->slave_num);
  987. slave->phy = NULL;
  988. } else {
  989. dev_info(priv->dev, "phy found : id is : 0x%x\n",
  990. slave->phy->phy_id);
  991. phy_start(slave->phy);
  992. /* Configure GMII_SEL register */
  993. cpsw_phy_sel(&priv->pdev->dev, slave->phy->interface,
  994. slave->slave_num);
  995. }
  996. }
  997. static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
  998. {
  999. const int vlan = priv->data.default_vlan;
  1000. const int port = priv->host_port;
  1001. u32 reg;
  1002. int i;
  1003. int unreg_mcast_mask;
  1004. reg = (priv->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
  1005. CPSW2_PORT_VLAN;
  1006. writel(vlan, &priv->host_port_regs->port_vlan);
  1007. for (i = 0; i < priv->data.slaves; i++)
  1008. slave_write(priv->slaves + i, vlan, reg);
  1009. if (priv->ndev->flags & IFF_ALLMULTI)
  1010. unreg_mcast_mask = ALE_ALL_PORTS;
  1011. else
  1012. unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
  1013. cpsw_ale_add_vlan(priv->ale, vlan, ALE_ALL_PORTS << port,
  1014. ALE_ALL_PORTS << port, ALE_ALL_PORTS << port,
  1015. unreg_mcast_mask << port);
  1016. }
  1017. static void cpsw_init_host_port(struct cpsw_priv *priv)
  1018. {
  1019. u32 control_reg;
  1020. u32 fifo_mode;
  1021. /* soft reset the controller and initialize ale */
  1022. soft_reset("cpsw", &priv->regs->soft_reset);
  1023. cpsw_ale_start(priv->ale);
  1024. /* switch to vlan unaware mode */
  1025. cpsw_ale_control_set(priv->ale, priv->host_port, ALE_VLAN_AWARE,
  1026. CPSW_ALE_VLAN_AWARE);
  1027. control_reg = readl(&priv->regs->control);
  1028. control_reg |= CPSW_VLAN_AWARE;
  1029. writel(control_reg, &priv->regs->control);
  1030. fifo_mode = (priv->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
  1031. CPSW_FIFO_NORMAL_MODE;
  1032. writel(fifo_mode, &priv->host_port_regs->tx_in_ctl);
  1033. /* setup host port priority mapping */
  1034. __raw_writel(CPDMA_TX_PRIORITY_MAP,
  1035. &priv->host_port_regs->cpdma_tx_pri_map);
  1036. __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
  1037. cpsw_ale_control_set(priv->ale, priv->host_port,
  1038. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  1039. if (!priv->data.dual_emac) {
  1040. cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port,
  1041. 0, 0);
  1042. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  1043. 1 << priv->host_port, 0, 0, ALE_MCAST_FWD_2);
  1044. }
  1045. }
  1046. static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
  1047. {
  1048. u32 slave_port;
  1049. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  1050. if (!slave->phy)
  1051. return;
  1052. phy_stop(slave->phy);
  1053. phy_disconnect(slave->phy);
  1054. slave->phy = NULL;
  1055. cpsw_ale_control_set(priv->ale, slave_port,
  1056. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  1057. }
  1058. static int cpsw_ndo_open(struct net_device *ndev)
  1059. {
  1060. struct cpsw_priv *priv = netdev_priv(ndev);
  1061. struct cpsw_priv *prim_cpsw;
  1062. int i, ret;
  1063. u32 reg;
  1064. if (!cpsw_common_res_usage_state(priv))
  1065. cpsw_intr_disable(priv);
  1066. netif_carrier_off(ndev);
  1067. pm_runtime_get_sync(&priv->pdev->dev);
  1068. reg = priv->version;
  1069. dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
  1070. CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
  1071. CPSW_RTL_VERSION(reg));
  1072. /* initialize host and slave ports */
  1073. if (!cpsw_common_res_usage_state(priv))
  1074. cpsw_init_host_port(priv);
  1075. for_each_slave(priv, cpsw_slave_open, priv);
  1076. /* Add default VLAN */
  1077. if (!priv->data.dual_emac)
  1078. cpsw_add_default_vlan(priv);
  1079. else
  1080. cpsw_ale_add_vlan(priv->ale, priv->data.default_vlan,
  1081. ALE_ALL_PORTS << priv->host_port,
  1082. ALE_ALL_PORTS << priv->host_port, 0, 0);
  1083. if (!cpsw_common_res_usage_state(priv)) {
  1084. /* setup tx dma to fixed prio and zero offset */
  1085. cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
  1086. cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
  1087. /* disable priority elevation */
  1088. __raw_writel(0, &priv->regs->ptype);
  1089. /* enable statistics collection only on all ports */
  1090. __raw_writel(0x7, &priv->regs->stat_port_en);
  1091. /* Enable internal fifo flow control */
  1092. writel(0x7, &priv->regs->flow_control);
  1093. if (WARN_ON(!priv->data.rx_descs))
  1094. priv->data.rx_descs = 128;
  1095. for (i = 0; i < priv->data.rx_descs; i++) {
  1096. struct sk_buff *skb;
  1097. ret = -ENOMEM;
  1098. skb = __netdev_alloc_skb_ip_align(priv->ndev,
  1099. priv->rx_packet_max, GFP_KERNEL);
  1100. if (!skb)
  1101. goto err_cleanup;
  1102. ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
  1103. skb_tailroom(skb), 0);
  1104. if (ret < 0) {
  1105. kfree_skb(skb);
  1106. goto err_cleanup;
  1107. }
  1108. }
  1109. /* continue even if we didn't manage to submit all
  1110. * receive descs
  1111. */
  1112. cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
  1113. if (cpts_register(&priv->pdev->dev, priv->cpts,
  1114. priv->data.cpts_clock_mult,
  1115. priv->data.cpts_clock_shift))
  1116. dev_err(priv->dev, "error registering cpts device\n");
  1117. }
  1118. /* Enable Interrupt pacing if configured */
  1119. if (priv->coal_intvl != 0) {
  1120. struct ethtool_coalesce coal;
  1121. coal.rx_coalesce_usecs = (priv->coal_intvl << 4);
  1122. cpsw_set_coalesce(ndev, &coal);
  1123. }
  1124. napi_enable(&priv->napi);
  1125. cpdma_ctlr_start(priv->dma);
  1126. cpsw_intr_enable(priv);
  1127. prim_cpsw = cpsw_get_slave_priv(priv, 0);
  1128. if (prim_cpsw->irq_enabled == false) {
  1129. if ((priv == prim_cpsw) || !netif_running(prim_cpsw->ndev)) {
  1130. prim_cpsw->irq_enabled = true;
  1131. cpsw_enable_irq(prim_cpsw);
  1132. }
  1133. }
  1134. if (priv->data.dual_emac)
  1135. priv->slaves[priv->emac_port].open_stat = true;
  1136. return 0;
  1137. err_cleanup:
  1138. cpdma_ctlr_stop(priv->dma);
  1139. for_each_slave(priv, cpsw_slave_stop, priv);
  1140. pm_runtime_put_sync(&priv->pdev->dev);
  1141. netif_carrier_off(priv->ndev);
  1142. return ret;
  1143. }
  1144. static int cpsw_ndo_stop(struct net_device *ndev)
  1145. {
  1146. struct cpsw_priv *priv = netdev_priv(ndev);
  1147. cpsw_info(priv, ifdown, "shutting down cpsw device\n");
  1148. netif_stop_queue(priv->ndev);
  1149. napi_disable(&priv->napi);
  1150. netif_carrier_off(priv->ndev);
  1151. if (cpsw_common_res_usage_state(priv) <= 1) {
  1152. cpts_unregister(priv->cpts);
  1153. cpsw_intr_disable(priv);
  1154. cpdma_ctlr_int_ctrl(priv->dma, false);
  1155. cpdma_ctlr_stop(priv->dma);
  1156. cpsw_ale_stop(priv->ale);
  1157. }
  1158. for_each_slave(priv, cpsw_slave_stop, priv);
  1159. pm_runtime_put_sync(&priv->pdev->dev);
  1160. if (priv->data.dual_emac)
  1161. priv->slaves[priv->emac_port].open_stat = false;
  1162. return 0;
  1163. }
  1164. static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
  1165. struct net_device *ndev)
  1166. {
  1167. struct cpsw_priv *priv = netdev_priv(ndev);
  1168. int ret;
  1169. ndev->trans_start = jiffies;
  1170. if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
  1171. cpsw_err(priv, tx_err, "packet pad failed\n");
  1172. ndev->stats.tx_dropped++;
  1173. return NETDEV_TX_OK;
  1174. }
  1175. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
  1176. priv->cpts->tx_enable)
  1177. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1178. skb_tx_timestamp(skb);
  1179. ret = cpsw_tx_packet_submit(ndev, priv, skb);
  1180. if (unlikely(ret != 0)) {
  1181. cpsw_err(priv, tx_err, "desc submit failed\n");
  1182. goto fail;
  1183. }
  1184. /* If there is no more tx desc left free then we need to
  1185. * tell the kernel to stop sending us tx frames.
  1186. */
  1187. if (unlikely(!cpdma_check_free_tx_desc(priv->txch)))
  1188. netif_stop_queue(ndev);
  1189. return NETDEV_TX_OK;
  1190. fail:
  1191. ndev->stats.tx_dropped++;
  1192. netif_stop_queue(ndev);
  1193. return NETDEV_TX_BUSY;
  1194. }
  1195. #ifdef CONFIG_TI_CPTS
  1196. static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
  1197. {
  1198. struct cpsw_slave *slave = &priv->slaves[priv->data.active_slave];
  1199. u32 ts_en, seq_id;
  1200. if (!priv->cpts->tx_enable && !priv->cpts->rx_enable) {
  1201. slave_write(slave, 0, CPSW1_TS_CTL);
  1202. return;
  1203. }
  1204. seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
  1205. ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
  1206. if (priv->cpts->tx_enable)
  1207. ts_en |= CPSW_V1_TS_TX_EN;
  1208. if (priv->cpts->rx_enable)
  1209. ts_en |= CPSW_V1_TS_RX_EN;
  1210. slave_write(slave, ts_en, CPSW1_TS_CTL);
  1211. slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
  1212. }
  1213. static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
  1214. {
  1215. struct cpsw_slave *slave;
  1216. u32 ctrl, mtype;
  1217. if (priv->data.dual_emac)
  1218. slave = &priv->slaves[priv->emac_port];
  1219. else
  1220. slave = &priv->slaves[priv->data.active_slave];
  1221. ctrl = slave_read(slave, CPSW2_CONTROL);
  1222. switch (priv->version) {
  1223. case CPSW_VERSION_2:
  1224. ctrl &= ~CTRL_V2_ALL_TS_MASK;
  1225. if (priv->cpts->tx_enable)
  1226. ctrl |= CTRL_V2_TX_TS_BITS;
  1227. if (priv->cpts->rx_enable)
  1228. ctrl |= CTRL_V2_RX_TS_BITS;
  1229. break;
  1230. case CPSW_VERSION_3:
  1231. default:
  1232. ctrl &= ~CTRL_V3_ALL_TS_MASK;
  1233. if (priv->cpts->tx_enable)
  1234. ctrl |= CTRL_V3_TX_TS_BITS;
  1235. if (priv->cpts->rx_enable)
  1236. ctrl |= CTRL_V3_RX_TS_BITS;
  1237. break;
  1238. }
  1239. mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
  1240. slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
  1241. slave_write(slave, ctrl, CPSW2_CONTROL);
  1242. __raw_writel(ETH_P_1588, &priv->regs->ts_ltype);
  1243. }
  1244. static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  1245. {
  1246. struct cpsw_priv *priv = netdev_priv(dev);
  1247. struct cpts *cpts = priv->cpts;
  1248. struct hwtstamp_config cfg;
  1249. if (priv->version != CPSW_VERSION_1 &&
  1250. priv->version != CPSW_VERSION_2 &&
  1251. priv->version != CPSW_VERSION_3)
  1252. return -EOPNOTSUPP;
  1253. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  1254. return -EFAULT;
  1255. /* reserved for future extensions */
  1256. if (cfg.flags)
  1257. return -EINVAL;
  1258. if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
  1259. return -ERANGE;
  1260. switch (cfg.rx_filter) {
  1261. case HWTSTAMP_FILTER_NONE:
  1262. cpts->rx_enable = 0;
  1263. break;
  1264. case HWTSTAMP_FILTER_ALL:
  1265. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1266. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1267. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1268. return -ERANGE;
  1269. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1270. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1271. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1272. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1273. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1274. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1275. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1276. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1277. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1278. cpts->rx_enable = 1;
  1279. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  1280. break;
  1281. default:
  1282. return -ERANGE;
  1283. }
  1284. cpts->tx_enable = cfg.tx_type == HWTSTAMP_TX_ON;
  1285. switch (priv->version) {
  1286. case CPSW_VERSION_1:
  1287. cpsw_hwtstamp_v1(priv);
  1288. break;
  1289. case CPSW_VERSION_2:
  1290. case CPSW_VERSION_3:
  1291. cpsw_hwtstamp_v2(priv);
  1292. break;
  1293. default:
  1294. WARN_ON(1);
  1295. }
  1296. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  1297. }
  1298. static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  1299. {
  1300. struct cpsw_priv *priv = netdev_priv(dev);
  1301. struct cpts *cpts = priv->cpts;
  1302. struct hwtstamp_config cfg;
  1303. if (priv->version != CPSW_VERSION_1 &&
  1304. priv->version != CPSW_VERSION_2 &&
  1305. priv->version != CPSW_VERSION_3)
  1306. return -EOPNOTSUPP;
  1307. cfg.flags = 0;
  1308. cfg.tx_type = cpts->tx_enable ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
  1309. cfg.rx_filter = (cpts->rx_enable ?
  1310. HWTSTAMP_FILTER_PTP_V2_EVENT : HWTSTAMP_FILTER_NONE);
  1311. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  1312. }
  1313. #endif /*CONFIG_TI_CPTS*/
  1314. static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  1315. {
  1316. struct cpsw_priv *priv = netdev_priv(dev);
  1317. int slave_no = cpsw_slave_index(priv);
  1318. if (!netif_running(dev))
  1319. return -EINVAL;
  1320. switch (cmd) {
  1321. #ifdef CONFIG_TI_CPTS
  1322. case SIOCSHWTSTAMP:
  1323. return cpsw_hwtstamp_set(dev, req);
  1324. case SIOCGHWTSTAMP:
  1325. return cpsw_hwtstamp_get(dev, req);
  1326. #endif
  1327. }
  1328. if (!priv->slaves[slave_no].phy)
  1329. return -EOPNOTSUPP;
  1330. return phy_mii_ioctl(priv->slaves[slave_no].phy, req, cmd);
  1331. }
  1332. static void cpsw_ndo_tx_timeout(struct net_device *ndev)
  1333. {
  1334. struct cpsw_priv *priv = netdev_priv(ndev);
  1335. cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
  1336. ndev->stats.tx_errors++;
  1337. cpsw_intr_disable(priv);
  1338. cpdma_ctlr_int_ctrl(priv->dma, false);
  1339. cpdma_chan_stop(priv->txch);
  1340. cpdma_chan_start(priv->txch);
  1341. cpdma_ctlr_int_ctrl(priv->dma, true);
  1342. cpsw_intr_enable(priv);
  1343. }
  1344. static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
  1345. {
  1346. struct cpsw_priv *priv = netdev_priv(ndev);
  1347. struct sockaddr *addr = (struct sockaddr *)p;
  1348. int flags = 0;
  1349. u16 vid = 0;
  1350. if (!is_valid_ether_addr(addr->sa_data))
  1351. return -EADDRNOTAVAIL;
  1352. if (priv->data.dual_emac) {
  1353. vid = priv->slaves[priv->emac_port].port_vlan;
  1354. flags = ALE_VLAN;
  1355. }
  1356. cpsw_ale_del_ucast(priv->ale, priv->mac_addr, priv->host_port,
  1357. flags, vid);
  1358. cpsw_ale_add_ucast(priv->ale, addr->sa_data, priv->host_port,
  1359. flags, vid);
  1360. memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
  1361. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  1362. for_each_slave(priv, cpsw_set_slave_mac, priv);
  1363. return 0;
  1364. }
  1365. #ifdef CONFIG_NET_POLL_CONTROLLER
  1366. static void cpsw_ndo_poll_controller(struct net_device *ndev)
  1367. {
  1368. struct cpsw_priv *priv = netdev_priv(ndev);
  1369. cpsw_intr_disable(priv);
  1370. cpdma_ctlr_int_ctrl(priv->dma, false);
  1371. cpsw_interrupt(ndev->irq, priv);
  1372. cpdma_ctlr_int_ctrl(priv->dma, true);
  1373. cpsw_intr_enable(priv);
  1374. }
  1375. #endif
  1376. static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
  1377. unsigned short vid)
  1378. {
  1379. int ret;
  1380. int unreg_mcast_mask = 0;
  1381. u32 port_mask;
  1382. if (priv->data.dual_emac) {
  1383. port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
  1384. if (priv->ndev->flags & IFF_ALLMULTI)
  1385. unreg_mcast_mask = port_mask;
  1386. } else {
  1387. port_mask = ALE_ALL_PORTS;
  1388. if (priv->ndev->flags & IFF_ALLMULTI)
  1389. unreg_mcast_mask = ALE_ALL_PORTS;
  1390. else
  1391. unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
  1392. }
  1393. ret = cpsw_ale_add_vlan(priv->ale, vid, port_mask, 0, port_mask,
  1394. unreg_mcast_mask << priv->host_port);
  1395. if (ret != 0)
  1396. return ret;
  1397. ret = cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
  1398. priv->host_port, ALE_VLAN, vid);
  1399. if (ret != 0)
  1400. goto clean_vid;
  1401. ret = cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  1402. port_mask, ALE_VLAN, vid, 0);
  1403. if (ret != 0)
  1404. goto clean_vlan_ucast;
  1405. return 0;
  1406. clean_vlan_ucast:
  1407. cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
  1408. priv->host_port, ALE_VLAN, vid);
  1409. clean_vid:
  1410. cpsw_ale_del_vlan(priv->ale, vid, 0);
  1411. return ret;
  1412. }
  1413. static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
  1414. __be16 proto, u16 vid)
  1415. {
  1416. struct cpsw_priv *priv = netdev_priv(ndev);
  1417. if (vid == priv->data.default_vlan)
  1418. return 0;
  1419. if (priv->data.dual_emac) {
  1420. /* In dual EMAC, reserved VLAN id should not be used for
  1421. * creating VLAN interfaces as this can break the dual
  1422. * EMAC port separation
  1423. */
  1424. int i;
  1425. for (i = 0; i < priv->data.slaves; i++) {
  1426. if (vid == priv->slaves[i].port_vlan)
  1427. return -EINVAL;
  1428. }
  1429. }
  1430. dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
  1431. return cpsw_add_vlan_ale_entry(priv, vid);
  1432. }
  1433. static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
  1434. __be16 proto, u16 vid)
  1435. {
  1436. struct cpsw_priv *priv = netdev_priv(ndev);
  1437. int ret;
  1438. if (vid == priv->data.default_vlan)
  1439. return 0;
  1440. if (priv->data.dual_emac) {
  1441. int i;
  1442. for (i = 0; i < priv->data.slaves; i++) {
  1443. if (vid == priv->slaves[i].port_vlan)
  1444. return -EINVAL;
  1445. }
  1446. }
  1447. dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
  1448. ret = cpsw_ale_del_vlan(priv->ale, vid, 0);
  1449. if (ret != 0)
  1450. return ret;
  1451. ret = cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
  1452. priv->host_port, ALE_VLAN, vid);
  1453. if (ret != 0)
  1454. return ret;
  1455. return cpsw_ale_del_mcast(priv->ale, priv->ndev->broadcast,
  1456. 0, ALE_VLAN, vid);
  1457. }
  1458. static const struct net_device_ops cpsw_netdev_ops = {
  1459. .ndo_open = cpsw_ndo_open,
  1460. .ndo_stop = cpsw_ndo_stop,
  1461. .ndo_start_xmit = cpsw_ndo_start_xmit,
  1462. .ndo_set_mac_address = cpsw_ndo_set_mac_address,
  1463. .ndo_do_ioctl = cpsw_ndo_ioctl,
  1464. .ndo_validate_addr = eth_validate_addr,
  1465. .ndo_change_mtu = eth_change_mtu,
  1466. .ndo_tx_timeout = cpsw_ndo_tx_timeout,
  1467. .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
  1468. #ifdef CONFIG_NET_POLL_CONTROLLER
  1469. .ndo_poll_controller = cpsw_ndo_poll_controller,
  1470. #endif
  1471. .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
  1472. .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
  1473. };
  1474. static int cpsw_get_regs_len(struct net_device *ndev)
  1475. {
  1476. struct cpsw_priv *priv = netdev_priv(ndev);
  1477. return priv->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
  1478. }
  1479. static void cpsw_get_regs(struct net_device *ndev,
  1480. struct ethtool_regs *regs, void *p)
  1481. {
  1482. struct cpsw_priv *priv = netdev_priv(ndev);
  1483. u32 *reg = p;
  1484. /* update CPSW IP version */
  1485. regs->version = priv->version;
  1486. cpsw_ale_dump(priv->ale, reg);
  1487. }
  1488. static void cpsw_get_drvinfo(struct net_device *ndev,
  1489. struct ethtool_drvinfo *info)
  1490. {
  1491. struct cpsw_priv *priv = netdev_priv(ndev);
  1492. strlcpy(info->driver, "cpsw", sizeof(info->driver));
  1493. strlcpy(info->version, "1.0", sizeof(info->version));
  1494. strlcpy(info->bus_info, priv->pdev->name, sizeof(info->bus_info));
  1495. info->regdump_len = cpsw_get_regs_len(ndev);
  1496. }
  1497. static u32 cpsw_get_msglevel(struct net_device *ndev)
  1498. {
  1499. struct cpsw_priv *priv = netdev_priv(ndev);
  1500. return priv->msg_enable;
  1501. }
  1502. static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
  1503. {
  1504. struct cpsw_priv *priv = netdev_priv(ndev);
  1505. priv->msg_enable = value;
  1506. }
  1507. static int cpsw_get_ts_info(struct net_device *ndev,
  1508. struct ethtool_ts_info *info)
  1509. {
  1510. #ifdef CONFIG_TI_CPTS
  1511. struct cpsw_priv *priv = netdev_priv(ndev);
  1512. info->so_timestamping =
  1513. SOF_TIMESTAMPING_TX_HARDWARE |
  1514. SOF_TIMESTAMPING_TX_SOFTWARE |
  1515. SOF_TIMESTAMPING_RX_HARDWARE |
  1516. SOF_TIMESTAMPING_RX_SOFTWARE |
  1517. SOF_TIMESTAMPING_SOFTWARE |
  1518. SOF_TIMESTAMPING_RAW_HARDWARE;
  1519. info->phc_index = priv->cpts->phc_index;
  1520. info->tx_types =
  1521. (1 << HWTSTAMP_TX_OFF) |
  1522. (1 << HWTSTAMP_TX_ON);
  1523. info->rx_filters =
  1524. (1 << HWTSTAMP_FILTER_NONE) |
  1525. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
  1526. #else
  1527. info->so_timestamping =
  1528. SOF_TIMESTAMPING_TX_SOFTWARE |
  1529. SOF_TIMESTAMPING_RX_SOFTWARE |
  1530. SOF_TIMESTAMPING_SOFTWARE;
  1531. info->phc_index = -1;
  1532. info->tx_types = 0;
  1533. info->rx_filters = 0;
  1534. #endif
  1535. return 0;
  1536. }
  1537. static int cpsw_get_settings(struct net_device *ndev,
  1538. struct ethtool_cmd *ecmd)
  1539. {
  1540. struct cpsw_priv *priv = netdev_priv(ndev);
  1541. int slave_no = cpsw_slave_index(priv);
  1542. if (priv->slaves[slave_no].phy)
  1543. return phy_ethtool_gset(priv->slaves[slave_no].phy, ecmd);
  1544. else
  1545. return -EOPNOTSUPP;
  1546. }
  1547. static int cpsw_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
  1548. {
  1549. struct cpsw_priv *priv = netdev_priv(ndev);
  1550. int slave_no = cpsw_slave_index(priv);
  1551. if (priv->slaves[slave_no].phy)
  1552. return phy_ethtool_sset(priv->slaves[slave_no].phy, ecmd);
  1553. else
  1554. return -EOPNOTSUPP;
  1555. }
  1556. static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  1557. {
  1558. struct cpsw_priv *priv = netdev_priv(ndev);
  1559. int slave_no = cpsw_slave_index(priv);
  1560. wol->supported = 0;
  1561. wol->wolopts = 0;
  1562. if (priv->slaves[slave_no].phy)
  1563. phy_ethtool_get_wol(priv->slaves[slave_no].phy, wol);
  1564. }
  1565. static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  1566. {
  1567. struct cpsw_priv *priv = netdev_priv(ndev);
  1568. int slave_no = cpsw_slave_index(priv);
  1569. if (priv->slaves[slave_no].phy)
  1570. return phy_ethtool_set_wol(priv->slaves[slave_no].phy, wol);
  1571. else
  1572. return -EOPNOTSUPP;
  1573. }
  1574. static void cpsw_get_pauseparam(struct net_device *ndev,
  1575. struct ethtool_pauseparam *pause)
  1576. {
  1577. struct cpsw_priv *priv = netdev_priv(ndev);
  1578. pause->autoneg = AUTONEG_DISABLE;
  1579. pause->rx_pause = priv->rx_pause ? true : false;
  1580. pause->tx_pause = priv->tx_pause ? true : false;
  1581. }
  1582. static int cpsw_set_pauseparam(struct net_device *ndev,
  1583. struct ethtool_pauseparam *pause)
  1584. {
  1585. struct cpsw_priv *priv = netdev_priv(ndev);
  1586. bool link;
  1587. priv->rx_pause = pause->rx_pause ? true : false;
  1588. priv->tx_pause = pause->tx_pause ? true : false;
  1589. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  1590. return 0;
  1591. }
  1592. static const struct ethtool_ops cpsw_ethtool_ops = {
  1593. .get_drvinfo = cpsw_get_drvinfo,
  1594. .get_msglevel = cpsw_get_msglevel,
  1595. .set_msglevel = cpsw_set_msglevel,
  1596. .get_link = ethtool_op_get_link,
  1597. .get_ts_info = cpsw_get_ts_info,
  1598. .get_settings = cpsw_get_settings,
  1599. .set_settings = cpsw_set_settings,
  1600. .get_coalesce = cpsw_get_coalesce,
  1601. .set_coalesce = cpsw_set_coalesce,
  1602. .get_sset_count = cpsw_get_sset_count,
  1603. .get_strings = cpsw_get_strings,
  1604. .get_ethtool_stats = cpsw_get_ethtool_stats,
  1605. .get_pauseparam = cpsw_get_pauseparam,
  1606. .set_pauseparam = cpsw_set_pauseparam,
  1607. .get_wol = cpsw_get_wol,
  1608. .set_wol = cpsw_set_wol,
  1609. .get_regs_len = cpsw_get_regs_len,
  1610. .get_regs = cpsw_get_regs,
  1611. };
  1612. static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv,
  1613. u32 slave_reg_ofs, u32 sliver_reg_ofs)
  1614. {
  1615. void __iomem *regs = priv->regs;
  1616. int slave_num = slave->slave_num;
  1617. struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
  1618. slave->data = data;
  1619. slave->regs = regs + slave_reg_ofs;
  1620. slave->sliver = regs + sliver_reg_ofs;
  1621. slave->port_vlan = data->dual_emac_res_vlan;
  1622. }
  1623. #define AM33XX_CTRL_MAC_LO_REG(id) (0x630 + 0x8 * id)
  1624. #define AM33XX_CTRL_MAC_HI_REG(id) (0x630 + 0x8 * id + 0x4)
  1625. static int cpsw_am33xx_cm_get_macid(struct device *dev, int slave,
  1626. u8 *mac_addr)
  1627. {
  1628. u32 macid_lo;
  1629. u32 macid_hi;
  1630. struct regmap *syscon;
  1631. syscon = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
  1632. if (IS_ERR(syscon)) {
  1633. if (PTR_ERR(syscon) == -ENODEV)
  1634. return 0;
  1635. return PTR_ERR(syscon);
  1636. }
  1637. regmap_read(syscon, AM33XX_CTRL_MAC_LO_REG(slave), &macid_lo);
  1638. regmap_read(syscon, AM33XX_CTRL_MAC_HI_REG(slave), &macid_hi);
  1639. mac_addr[5] = (macid_lo >> 8) & 0xff;
  1640. mac_addr[4] = macid_lo & 0xff;
  1641. mac_addr[3] = (macid_hi >> 24) & 0xff;
  1642. mac_addr[2] = (macid_hi >> 16) & 0xff;
  1643. mac_addr[1] = (macid_hi >> 8) & 0xff;
  1644. mac_addr[0] = macid_hi & 0xff;
  1645. return 0;
  1646. }
  1647. static int cpsw_probe_dt(struct cpsw_platform_data *data,
  1648. struct platform_device *pdev)
  1649. {
  1650. struct device_node *node = pdev->dev.of_node;
  1651. struct device_node *slave_node;
  1652. int i = 0, ret;
  1653. u32 prop;
  1654. if (!node)
  1655. return -EINVAL;
  1656. if (of_property_read_u32(node, "slaves", &prop)) {
  1657. dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
  1658. return -EINVAL;
  1659. }
  1660. data->slaves = prop;
  1661. if (of_property_read_u32(node, "active_slave", &prop)) {
  1662. dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
  1663. return -EINVAL;
  1664. }
  1665. data->active_slave = prop;
  1666. if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
  1667. dev_err(&pdev->dev, "Missing cpts_clock_mult property in the DT.\n");
  1668. return -EINVAL;
  1669. }
  1670. data->cpts_clock_mult = prop;
  1671. if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
  1672. dev_err(&pdev->dev, "Missing cpts_clock_shift property in the DT.\n");
  1673. return -EINVAL;
  1674. }
  1675. data->cpts_clock_shift = prop;
  1676. data->slave_data = devm_kzalloc(&pdev->dev, data->slaves
  1677. * sizeof(struct cpsw_slave_data),
  1678. GFP_KERNEL);
  1679. if (!data->slave_data)
  1680. return -ENOMEM;
  1681. if (of_property_read_u32(node, "cpdma_channels", &prop)) {
  1682. dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
  1683. return -EINVAL;
  1684. }
  1685. data->channels = prop;
  1686. if (of_property_read_u32(node, "ale_entries", &prop)) {
  1687. dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
  1688. return -EINVAL;
  1689. }
  1690. data->ale_entries = prop;
  1691. if (of_property_read_u32(node, "bd_ram_size", &prop)) {
  1692. dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
  1693. return -EINVAL;
  1694. }
  1695. data->bd_ram_size = prop;
  1696. if (of_property_read_u32(node, "rx_descs", &prop)) {
  1697. dev_err(&pdev->dev, "Missing rx_descs property in the DT.\n");
  1698. return -EINVAL;
  1699. }
  1700. data->rx_descs = prop;
  1701. if (of_property_read_u32(node, "mac_control", &prop)) {
  1702. dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
  1703. return -EINVAL;
  1704. }
  1705. data->mac_control = prop;
  1706. if (of_property_read_bool(node, "dual_emac"))
  1707. data->dual_emac = 1;
  1708. /*
  1709. * Populate all the child nodes here...
  1710. */
  1711. ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
  1712. /* We do not want to force this, as in some cases may not have child */
  1713. if (ret)
  1714. dev_warn(&pdev->dev, "Doesn't have any child node\n");
  1715. for_each_child_of_node(node, slave_node) {
  1716. struct cpsw_slave_data *slave_data = data->slave_data + i;
  1717. const void *mac_addr = NULL;
  1718. u32 phyid;
  1719. int lenp;
  1720. const __be32 *parp;
  1721. struct device_node *mdio_node;
  1722. struct platform_device *mdio;
  1723. /* This is no slave child node, continue */
  1724. if (strcmp(slave_node->name, "slave"))
  1725. continue;
  1726. parp = of_get_property(slave_node, "phy_id", &lenp);
  1727. if ((parp == NULL) || (lenp != (sizeof(void *) * 2))) {
  1728. dev_err(&pdev->dev, "Missing slave[%d] phy_id property\n", i);
  1729. goto no_phy_slave;
  1730. }
  1731. mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
  1732. phyid = be32_to_cpup(parp+1);
  1733. mdio = of_find_device_by_node(mdio_node);
  1734. of_node_put(mdio_node);
  1735. if (!mdio) {
  1736. dev_err(&pdev->dev, "Missing mdio platform device\n");
  1737. return -EINVAL;
  1738. }
  1739. snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
  1740. PHY_ID_FMT, mdio->name, phyid);
  1741. slave_data->phy_if = of_get_phy_mode(slave_node);
  1742. if (slave_data->phy_if < 0) {
  1743. dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
  1744. i);
  1745. return slave_data->phy_if;
  1746. }
  1747. no_phy_slave:
  1748. mac_addr = of_get_mac_address(slave_node);
  1749. if (mac_addr) {
  1750. memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
  1751. } else {
  1752. if (of_machine_is_compatible("ti,am33xx")) {
  1753. ret = cpsw_am33xx_cm_get_macid(&pdev->dev, i,
  1754. slave_data->mac_addr);
  1755. if (ret)
  1756. return ret;
  1757. }
  1758. }
  1759. if (data->dual_emac) {
  1760. if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
  1761. &prop)) {
  1762. dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
  1763. slave_data->dual_emac_res_vlan = i+1;
  1764. dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
  1765. slave_data->dual_emac_res_vlan, i);
  1766. } else {
  1767. slave_data->dual_emac_res_vlan = prop;
  1768. }
  1769. }
  1770. i++;
  1771. if (i == data->slaves)
  1772. break;
  1773. }
  1774. return 0;
  1775. }
  1776. static int cpsw_probe_dual_emac(struct platform_device *pdev,
  1777. struct cpsw_priv *priv)
  1778. {
  1779. struct cpsw_platform_data *data = &priv->data;
  1780. struct net_device *ndev;
  1781. struct cpsw_priv *priv_sl2;
  1782. int ret = 0, i;
  1783. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  1784. if (!ndev) {
  1785. dev_err(&pdev->dev, "cpsw: error allocating net_device\n");
  1786. return -ENOMEM;
  1787. }
  1788. priv_sl2 = netdev_priv(ndev);
  1789. spin_lock_init(&priv_sl2->lock);
  1790. priv_sl2->data = *data;
  1791. priv_sl2->pdev = pdev;
  1792. priv_sl2->ndev = ndev;
  1793. priv_sl2->dev = &ndev->dev;
  1794. priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  1795. priv_sl2->rx_packet_max = max(rx_packet_max, 128);
  1796. if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
  1797. memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
  1798. ETH_ALEN);
  1799. dev_info(&pdev->dev, "cpsw: Detected MACID = %pM\n", priv_sl2->mac_addr);
  1800. } else {
  1801. random_ether_addr(priv_sl2->mac_addr);
  1802. dev_info(&pdev->dev, "cpsw: Random MACID = %pM\n", priv_sl2->mac_addr);
  1803. }
  1804. memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
  1805. priv_sl2->slaves = priv->slaves;
  1806. priv_sl2->clk = priv->clk;
  1807. priv_sl2->coal_intvl = 0;
  1808. priv_sl2->bus_freq_mhz = priv->bus_freq_mhz;
  1809. priv_sl2->regs = priv->regs;
  1810. priv_sl2->host_port = priv->host_port;
  1811. priv_sl2->host_port_regs = priv->host_port_regs;
  1812. priv_sl2->wr_regs = priv->wr_regs;
  1813. priv_sl2->hw_stats = priv->hw_stats;
  1814. priv_sl2->dma = priv->dma;
  1815. priv_sl2->txch = priv->txch;
  1816. priv_sl2->rxch = priv->rxch;
  1817. priv_sl2->ale = priv->ale;
  1818. priv_sl2->emac_port = 1;
  1819. priv->slaves[1].ndev = ndev;
  1820. priv_sl2->cpts = priv->cpts;
  1821. priv_sl2->version = priv->version;
  1822. for (i = 0; i < priv->num_irqs; i++) {
  1823. priv_sl2->irqs_table[i] = priv->irqs_table[i];
  1824. priv_sl2->num_irqs = priv->num_irqs;
  1825. }
  1826. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1827. ndev->netdev_ops = &cpsw_netdev_ops;
  1828. ndev->ethtool_ops = &cpsw_ethtool_ops;
  1829. netif_napi_add(ndev, &priv_sl2->napi, cpsw_poll, CPSW_POLL_WEIGHT);
  1830. /* register the network device */
  1831. SET_NETDEV_DEV(ndev, &pdev->dev);
  1832. ret = register_netdev(ndev);
  1833. if (ret) {
  1834. dev_err(&pdev->dev, "cpsw: error registering net device\n");
  1835. free_netdev(ndev);
  1836. ret = -ENODEV;
  1837. }
  1838. return ret;
  1839. }
  1840. static int cpsw_probe(struct platform_device *pdev)
  1841. {
  1842. struct cpsw_platform_data *data;
  1843. struct net_device *ndev;
  1844. struct cpsw_priv *priv;
  1845. struct cpdma_params dma_params;
  1846. struct cpsw_ale_params ale_params;
  1847. void __iomem *ss_regs;
  1848. struct resource *res, *ss_res;
  1849. u32 slave_offset, sliver_offset, slave_size;
  1850. int ret = 0, i, k = 0;
  1851. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  1852. if (!ndev) {
  1853. dev_err(&pdev->dev, "error allocating net_device\n");
  1854. return -ENOMEM;
  1855. }
  1856. platform_set_drvdata(pdev, ndev);
  1857. priv = netdev_priv(ndev);
  1858. spin_lock_init(&priv->lock);
  1859. priv->pdev = pdev;
  1860. priv->ndev = ndev;
  1861. priv->dev = &ndev->dev;
  1862. priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  1863. priv->rx_packet_max = max(rx_packet_max, 128);
  1864. priv->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL);
  1865. priv->irq_enabled = true;
  1866. if (!priv->cpts) {
  1867. dev_err(&pdev->dev, "error allocating cpts\n");
  1868. ret = -ENOMEM;
  1869. goto clean_ndev_ret;
  1870. }
  1871. /*
  1872. * This may be required here for child devices.
  1873. */
  1874. pm_runtime_enable(&pdev->dev);
  1875. /* Select default pin state */
  1876. pinctrl_pm_select_default_state(&pdev->dev);
  1877. if (cpsw_probe_dt(&priv->data, pdev)) {
  1878. dev_err(&pdev->dev, "cpsw: platform data missing\n");
  1879. ret = -ENODEV;
  1880. goto clean_runtime_disable_ret;
  1881. }
  1882. data = &priv->data;
  1883. if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
  1884. memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
  1885. dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr);
  1886. } else {
  1887. eth_random_addr(priv->mac_addr);
  1888. dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr);
  1889. }
  1890. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  1891. priv->slaves = devm_kzalloc(&pdev->dev,
  1892. sizeof(struct cpsw_slave) * data->slaves,
  1893. GFP_KERNEL);
  1894. if (!priv->slaves) {
  1895. ret = -ENOMEM;
  1896. goto clean_runtime_disable_ret;
  1897. }
  1898. for (i = 0; i < data->slaves; i++)
  1899. priv->slaves[i].slave_num = i;
  1900. priv->slaves[0].ndev = ndev;
  1901. priv->emac_port = 0;
  1902. priv->clk = devm_clk_get(&pdev->dev, "fck");
  1903. if (IS_ERR(priv->clk)) {
  1904. dev_err(priv->dev, "fck is not found\n");
  1905. ret = -ENODEV;
  1906. goto clean_runtime_disable_ret;
  1907. }
  1908. priv->coal_intvl = 0;
  1909. priv->bus_freq_mhz = clk_get_rate(priv->clk) / 1000000;
  1910. ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1911. ss_regs = devm_ioremap_resource(&pdev->dev, ss_res);
  1912. if (IS_ERR(ss_regs)) {
  1913. ret = PTR_ERR(ss_regs);
  1914. goto clean_runtime_disable_ret;
  1915. }
  1916. priv->regs = ss_regs;
  1917. priv->host_port = HOST_PORT_NUM;
  1918. /* Need to enable clocks with runtime PM api to access module
  1919. * registers
  1920. */
  1921. pm_runtime_get_sync(&pdev->dev);
  1922. priv->version = readl(&priv->regs->id_ver);
  1923. pm_runtime_put_sync(&pdev->dev);
  1924. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1925. priv->wr_regs = devm_ioremap_resource(&pdev->dev, res);
  1926. if (IS_ERR(priv->wr_regs)) {
  1927. ret = PTR_ERR(priv->wr_regs);
  1928. goto clean_runtime_disable_ret;
  1929. }
  1930. memset(&dma_params, 0, sizeof(dma_params));
  1931. memset(&ale_params, 0, sizeof(ale_params));
  1932. switch (priv->version) {
  1933. case CPSW_VERSION_1:
  1934. priv->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
  1935. priv->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET;
  1936. priv->hw_stats = ss_regs + CPSW1_HW_STATS;
  1937. dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
  1938. dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
  1939. ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
  1940. slave_offset = CPSW1_SLAVE_OFFSET;
  1941. slave_size = CPSW1_SLAVE_SIZE;
  1942. sliver_offset = CPSW1_SLIVER_OFFSET;
  1943. dma_params.desc_mem_phys = 0;
  1944. break;
  1945. case CPSW_VERSION_2:
  1946. case CPSW_VERSION_3:
  1947. case CPSW_VERSION_4:
  1948. priv->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
  1949. priv->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET;
  1950. priv->hw_stats = ss_regs + CPSW2_HW_STATS;
  1951. dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
  1952. dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
  1953. ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
  1954. slave_offset = CPSW2_SLAVE_OFFSET;
  1955. slave_size = CPSW2_SLAVE_SIZE;
  1956. sliver_offset = CPSW2_SLIVER_OFFSET;
  1957. dma_params.desc_mem_phys =
  1958. (u32 __force) ss_res->start + CPSW2_BD_OFFSET;
  1959. break;
  1960. default:
  1961. dev_err(priv->dev, "unknown version 0x%08x\n", priv->version);
  1962. ret = -ENODEV;
  1963. goto clean_runtime_disable_ret;
  1964. }
  1965. for (i = 0; i < priv->data.slaves; i++) {
  1966. struct cpsw_slave *slave = &priv->slaves[i];
  1967. cpsw_slave_init(slave, priv, slave_offset, sliver_offset);
  1968. slave_offset += slave_size;
  1969. sliver_offset += SLIVER_SIZE;
  1970. }
  1971. dma_params.dev = &pdev->dev;
  1972. dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
  1973. dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
  1974. dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
  1975. dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
  1976. dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
  1977. dma_params.num_chan = data->channels;
  1978. dma_params.has_soft_reset = true;
  1979. dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
  1980. dma_params.desc_mem_size = data->bd_ram_size;
  1981. dma_params.desc_align = 16;
  1982. dma_params.has_ext_regs = true;
  1983. dma_params.desc_hw_addr = dma_params.desc_mem_phys;
  1984. priv->dma = cpdma_ctlr_create(&dma_params);
  1985. if (!priv->dma) {
  1986. dev_err(priv->dev, "error initializing dma\n");
  1987. ret = -ENOMEM;
  1988. goto clean_runtime_disable_ret;
  1989. }
  1990. priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
  1991. cpsw_tx_handler);
  1992. priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
  1993. cpsw_rx_handler);
  1994. if (WARN_ON(!priv->txch || !priv->rxch)) {
  1995. dev_err(priv->dev, "error initializing dma channels\n");
  1996. ret = -ENOMEM;
  1997. goto clean_dma_ret;
  1998. }
  1999. ale_params.dev = &ndev->dev;
  2000. ale_params.ale_ageout = ale_ageout;
  2001. ale_params.ale_entries = data->ale_entries;
  2002. ale_params.ale_ports = data->slaves;
  2003. priv->ale = cpsw_ale_create(&ale_params);
  2004. if (!priv->ale) {
  2005. dev_err(priv->dev, "error initializing ale engine\n");
  2006. ret = -ENODEV;
  2007. goto clean_dma_ret;
  2008. }
  2009. ndev->irq = platform_get_irq(pdev, 0);
  2010. if (ndev->irq < 0) {
  2011. dev_err(priv->dev, "error getting irq resource\n");
  2012. ret = -ENOENT;
  2013. goto clean_ale_ret;
  2014. }
  2015. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
  2016. if (k >= ARRAY_SIZE(priv->irqs_table)) {
  2017. ret = -EINVAL;
  2018. goto clean_ale_ret;
  2019. }
  2020. ret = devm_request_irq(&pdev->dev, res->start, cpsw_interrupt,
  2021. 0, dev_name(&pdev->dev), priv);
  2022. if (ret < 0) {
  2023. dev_err(priv->dev, "error attaching irq (%d)\n", ret);
  2024. goto clean_ale_ret;
  2025. }
  2026. priv->irqs_table[k] = res->start;
  2027. k++;
  2028. }
  2029. priv->num_irqs = k;
  2030. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  2031. ndev->netdev_ops = &cpsw_netdev_ops;
  2032. ndev->ethtool_ops = &cpsw_ethtool_ops;
  2033. netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
  2034. /* register the network device */
  2035. SET_NETDEV_DEV(ndev, &pdev->dev);
  2036. ret = register_netdev(ndev);
  2037. if (ret) {
  2038. dev_err(priv->dev, "error registering net device\n");
  2039. ret = -ENODEV;
  2040. goto clean_ale_ret;
  2041. }
  2042. cpsw_notice(priv, probe, "initialized device (regs %pa, irq %d)\n",
  2043. &ss_res->start, ndev->irq);
  2044. if (priv->data.dual_emac) {
  2045. ret = cpsw_probe_dual_emac(pdev, priv);
  2046. if (ret) {
  2047. cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
  2048. goto clean_ale_ret;
  2049. }
  2050. }
  2051. return 0;
  2052. clean_ale_ret:
  2053. cpsw_ale_destroy(priv->ale);
  2054. clean_dma_ret:
  2055. cpdma_chan_destroy(priv->txch);
  2056. cpdma_chan_destroy(priv->rxch);
  2057. cpdma_ctlr_destroy(priv->dma);
  2058. clean_runtime_disable_ret:
  2059. pm_runtime_disable(&pdev->dev);
  2060. clean_ndev_ret:
  2061. free_netdev(priv->ndev);
  2062. return ret;
  2063. }
  2064. static int cpsw_remove_child_device(struct device *dev, void *c)
  2065. {
  2066. struct platform_device *pdev = to_platform_device(dev);
  2067. of_device_unregister(pdev);
  2068. return 0;
  2069. }
  2070. static int cpsw_remove(struct platform_device *pdev)
  2071. {
  2072. struct net_device *ndev = platform_get_drvdata(pdev);
  2073. struct cpsw_priv *priv = netdev_priv(ndev);
  2074. if (priv->data.dual_emac)
  2075. unregister_netdev(cpsw_get_slave_ndev(priv, 1));
  2076. unregister_netdev(ndev);
  2077. cpsw_ale_destroy(priv->ale);
  2078. cpdma_chan_destroy(priv->txch);
  2079. cpdma_chan_destroy(priv->rxch);
  2080. cpdma_ctlr_destroy(priv->dma);
  2081. pm_runtime_disable(&pdev->dev);
  2082. device_for_each_child(&pdev->dev, NULL, cpsw_remove_child_device);
  2083. if (priv->data.dual_emac)
  2084. free_netdev(cpsw_get_slave_ndev(priv, 1));
  2085. free_netdev(ndev);
  2086. return 0;
  2087. }
  2088. static int cpsw_suspend(struct device *dev)
  2089. {
  2090. struct platform_device *pdev = to_platform_device(dev);
  2091. struct net_device *ndev = platform_get_drvdata(pdev);
  2092. struct cpsw_priv *priv = netdev_priv(ndev);
  2093. if (priv->data.dual_emac) {
  2094. int i;
  2095. for (i = 0; i < priv->data.slaves; i++) {
  2096. if (netif_running(priv->slaves[i].ndev))
  2097. cpsw_ndo_stop(priv->slaves[i].ndev);
  2098. soft_reset_slave(priv->slaves + i);
  2099. }
  2100. } else {
  2101. if (netif_running(ndev))
  2102. cpsw_ndo_stop(ndev);
  2103. for_each_slave(priv, soft_reset_slave);
  2104. }
  2105. pm_runtime_put_sync(&pdev->dev);
  2106. /* Select sleep pin state */
  2107. pinctrl_pm_select_sleep_state(&pdev->dev);
  2108. return 0;
  2109. }
  2110. static int cpsw_resume(struct device *dev)
  2111. {
  2112. struct platform_device *pdev = to_platform_device(dev);
  2113. struct net_device *ndev = platform_get_drvdata(pdev);
  2114. struct cpsw_priv *priv = netdev_priv(ndev);
  2115. pm_runtime_get_sync(&pdev->dev);
  2116. /* Select default pin state */
  2117. pinctrl_pm_select_default_state(&pdev->dev);
  2118. if (priv->data.dual_emac) {
  2119. int i;
  2120. for (i = 0; i < priv->data.slaves; i++) {
  2121. if (netif_running(priv->slaves[i].ndev))
  2122. cpsw_ndo_open(priv->slaves[i].ndev);
  2123. }
  2124. } else {
  2125. if (netif_running(ndev))
  2126. cpsw_ndo_open(ndev);
  2127. }
  2128. return 0;
  2129. }
  2130. static const struct dev_pm_ops cpsw_pm_ops = {
  2131. .suspend = cpsw_suspend,
  2132. .resume = cpsw_resume,
  2133. };
  2134. static const struct of_device_id cpsw_of_mtable[] = {
  2135. { .compatible = "ti,cpsw", },
  2136. { /* sentinel */ },
  2137. };
  2138. MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
  2139. static struct platform_driver cpsw_driver = {
  2140. .driver = {
  2141. .name = "cpsw",
  2142. .pm = &cpsw_pm_ops,
  2143. .of_match_table = cpsw_of_mtable,
  2144. },
  2145. .probe = cpsw_probe,
  2146. .remove = cpsw_remove,
  2147. };
  2148. static int __init cpsw_init(void)
  2149. {
  2150. return platform_driver_register(&cpsw_driver);
  2151. }
  2152. late_initcall(cpsw_init);
  2153. static void __exit cpsw_exit(void)
  2154. {
  2155. platform_driver_unregister(&cpsw_driver);
  2156. }
  2157. module_exit(cpsw_exit);
  2158. MODULE_LICENSE("GPL");
  2159. MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
  2160. MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
  2161. MODULE_DESCRIPTION("TI CPSW Ethernet driver");