cpsw.c 69 KB

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