cpsw.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  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 << 15)
  112. #define CPSW_FIFO_DUAL_MAC_MODE (1 << 15)
  113. #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 15)
  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_BIT8 (1<<8) /* ts_ttl_nonzero? */
  219. #define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
  220. #define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
  221. #define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
  222. #define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
  223. #define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
  224. #define CTRL_TS_BITS \
  225. (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 | TS_BIT8 | \
  226. TS_ANNEX_D_EN | TS_LTYPE1_EN)
  227. #define CTRL_ALL_TS_MASK (CTRL_TS_BITS | TS_TX_EN | TS_RX_EN)
  228. #define CTRL_TX_TS_BITS (CTRL_TS_BITS | TS_TX_EN)
  229. #define CTRL_RX_TS_BITS (CTRL_TS_BITS | TS_RX_EN)
  230. /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
  231. #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
  232. #define TS_SEQ_ID_OFFSET_MASK (0x3f)
  233. #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
  234. #define TS_MSG_TYPE_EN_MASK (0xffff)
  235. /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
  236. #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
  237. /* Bit definitions for the CPSW1_TS_CTL register */
  238. #define CPSW_V1_TS_RX_EN BIT(0)
  239. #define CPSW_V1_TS_TX_EN BIT(4)
  240. #define CPSW_V1_MSG_TYPE_OFS 16
  241. /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
  242. #define CPSW_V1_SEQ_ID_OFS_SHIFT 16
  243. struct cpsw_host_regs {
  244. u32 max_blks;
  245. u32 blk_cnt;
  246. u32 tx_in_ctl;
  247. u32 port_vlan;
  248. u32 tx_pri_map;
  249. u32 cpdma_tx_pri_map;
  250. u32 cpdma_rx_chan_map;
  251. };
  252. struct cpsw_sliver_regs {
  253. u32 id_ver;
  254. u32 mac_control;
  255. u32 mac_status;
  256. u32 soft_reset;
  257. u32 rx_maxlen;
  258. u32 __reserved_0;
  259. u32 rx_pause;
  260. u32 tx_pause;
  261. u32 __reserved_1;
  262. u32 rx_pri_map;
  263. };
  264. struct cpsw_hw_stats {
  265. u32 rxgoodframes;
  266. u32 rxbroadcastframes;
  267. u32 rxmulticastframes;
  268. u32 rxpauseframes;
  269. u32 rxcrcerrors;
  270. u32 rxaligncodeerrors;
  271. u32 rxoversizedframes;
  272. u32 rxjabberframes;
  273. u32 rxundersizedframes;
  274. u32 rxfragments;
  275. u32 __pad_0[2];
  276. u32 rxoctets;
  277. u32 txgoodframes;
  278. u32 txbroadcastframes;
  279. u32 txmulticastframes;
  280. u32 txpauseframes;
  281. u32 txdeferredframes;
  282. u32 txcollisionframes;
  283. u32 txsinglecollframes;
  284. u32 txmultcollframes;
  285. u32 txexcessivecollisions;
  286. u32 txlatecollisions;
  287. u32 txunderrun;
  288. u32 txcarriersenseerrors;
  289. u32 txoctets;
  290. u32 octetframes64;
  291. u32 octetframes65t127;
  292. u32 octetframes128t255;
  293. u32 octetframes256t511;
  294. u32 octetframes512t1023;
  295. u32 octetframes1024tup;
  296. u32 netoctets;
  297. u32 rxsofoverruns;
  298. u32 rxmofoverruns;
  299. u32 rxdmaoverruns;
  300. };
  301. struct cpsw_slave {
  302. void __iomem *regs;
  303. struct cpsw_sliver_regs __iomem *sliver;
  304. int slave_num;
  305. u32 mac_control;
  306. struct cpsw_slave_data *data;
  307. struct phy_device *phy;
  308. struct net_device *ndev;
  309. u32 port_vlan;
  310. u32 open_stat;
  311. };
  312. static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
  313. {
  314. return __raw_readl(slave->regs + offset);
  315. }
  316. static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
  317. {
  318. __raw_writel(val, slave->regs + offset);
  319. }
  320. struct cpsw_priv {
  321. spinlock_t lock;
  322. struct platform_device *pdev;
  323. struct net_device *ndev;
  324. struct resource *cpsw_res;
  325. struct resource *cpsw_wr_res;
  326. struct napi_struct napi;
  327. struct device *dev;
  328. struct cpsw_platform_data data;
  329. struct cpsw_ss_regs __iomem *regs;
  330. struct cpsw_wr_regs __iomem *wr_regs;
  331. u8 __iomem *hw_stats;
  332. struct cpsw_host_regs __iomem *host_port_regs;
  333. u32 msg_enable;
  334. u32 version;
  335. u32 coal_intvl;
  336. u32 bus_freq_mhz;
  337. struct net_device_stats stats;
  338. int rx_packet_max;
  339. int host_port;
  340. struct clk *clk;
  341. u8 mac_addr[ETH_ALEN];
  342. struct cpsw_slave *slaves;
  343. struct cpdma_ctlr *dma;
  344. struct cpdma_chan *txch, *rxch;
  345. struct cpsw_ale *ale;
  346. /* snapshot of IRQ numbers */
  347. u32 irqs_table[4];
  348. u32 num_irqs;
  349. bool irq_enabled;
  350. struct cpts *cpts;
  351. u32 emac_port;
  352. };
  353. struct cpsw_stats {
  354. char stat_string[ETH_GSTRING_LEN];
  355. int type;
  356. int sizeof_stat;
  357. int stat_offset;
  358. };
  359. enum {
  360. CPSW_STATS,
  361. CPDMA_RX_STATS,
  362. CPDMA_TX_STATS,
  363. };
  364. #define CPSW_STAT(m) CPSW_STATS, \
  365. sizeof(((struct cpsw_hw_stats *)0)->m), \
  366. offsetof(struct cpsw_hw_stats, m)
  367. #define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
  368. sizeof(((struct cpdma_chan_stats *)0)->m), \
  369. offsetof(struct cpdma_chan_stats, m)
  370. #define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
  371. sizeof(((struct cpdma_chan_stats *)0)->m), \
  372. offsetof(struct cpdma_chan_stats, m)
  373. static const struct cpsw_stats cpsw_gstrings_stats[] = {
  374. { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
  375. { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
  376. { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
  377. { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
  378. { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
  379. { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
  380. { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
  381. { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
  382. { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
  383. { "Rx Fragments", CPSW_STAT(rxfragments) },
  384. { "Rx Octets", CPSW_STAT(rxoctets) },
  385. { "Good Tx Frames", CPSW_STAT(txgoodframes) },
  386. { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
  387. { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
  388. { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
  389. { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
  390. { "Collisions", CPSW_STAT(txcollisionframes) },
  391. { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
  392. { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
  393. { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
  394. { "Late Collisions", CPSW_STAT(txlatecollisions) },
  395. { "Tx Underrun", CPSW_STAT(txunderrun) },
  396. { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
  397. { "Tx Octets", CPSW_STAT(txoctets) },
  398. { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
  399. { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
  400. { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
  401. { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
  402. { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
  403. { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
  404. { "Net Octets", CPSW_STAT(netoctets) },
  405. { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
  406. { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
  407. { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
  408. { "Rx DMA chan: head_enqueue", CPDMA_RX_STAT(head_enqueue) },
  409. { "Rx DMA chan: tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
  410. { "Rx DMA chan: pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
  411. { "Rx DMA chan: misqueued", CPDMA_RX_STAT(misqueued) },
  412. { "Rx DMA chan: desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
  413. { "Rx DMA chan: pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
  414. { "Rx DMA chan: runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
  415. { "Rx DMA chan: runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
  416. { "Rx DMA chan: empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
  417. { "Rx DMA chan: busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
  418. { "Rx DMA chan: good_dequeue", CPDMA_RX_STAT(good_dequeue) },
  419. { "Rx DMA chan: requeue", CPDMA_RX_STAT(requeue) },
  420. { "Rx DMA chan: teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
  421. { "Tx DMA chan: head_enqueue", CPDMA_TX_STAT(head_enqueue) },
  422. { "Tx DMA chan: tail_enqueue", CPDMA_TX_STAT(tail_enqueue) },
  423. { "Tx DMA chan: pad_enqueue", CPDMA_TX_STAT(pad_enqueue) },
  424. { "Tx DMA chan: misqueued", CPDMA_TX_STAT(misqueued) },
  425. { "Tx DMA chan: desc_alloc_fail", CPDMA_TX_STAT(desc_alloc_fail) },
  426. { "Tx DMA chan: pad_alloc_fail", CPDMA_TX_STAT(pad_alloc_fail) },
  427. { "Tx DMA chan: runt_receive_buf", CPDMA_TX_STAT(runt_receive_buff) },
  428. { "Tx DMA chan: runt_transmit_buf", CPDMA_TX_STAT(runt_transmit_buff) },
  429. { "Tx DMA chan: empty_dequeue", CPDMA_TX_STAT(empty_dequeue) },
  430. { "Tx DMA chan: busy_dequeue", CPDMA_TX_STAT(busy_dequeue) },
  431. { "Tx DMA chan: good_dequeue", CPDMA_TX_STAT(good_dequeue) },
  432. { "Tx DMA chan: requeue", CPDMA_TX_STAT(requeue) },
  433. { "Tx DMA chan: teardown_dequeue", CPDMA_TX_STAT(teardown_dequeue) },
  434. };
  435. #define CPSW_STATS_LEN ARRAY_SIZE(cpsw_gstrings_stats)
  436. #define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
  437. #define for_each_slave(priv, func, arg...) \
  438. do { \
  439. struct cpsw_slave *slave; \
  440. int n; \
  441. if (priv->data.dual_emac) \
  442. (func)((priv)->slaves + priv->emac_port, ##arg);\
  443. else \
  444. for (n = (priv)->data.slaves, \
  445. slave = (priv)->slaves; \
  446. n; n--) \
  447. (func)(slave++, ##arg); \
  448. } while (0)
  449. #define cpsw_get_slave_ndev(priv, __slave_no__) \
  450. (priv->slaves[__slave_no__].ndev)
  451. #define cpsw_get_slave_priv(priv, __slave_no__) \
  452. ((priv->slaves[__slave_no__].ndev) ? \
  453. netdev_priv(priv->slaves[__slave_no__].ndev) : NULL) \
  454. #define cpsw_dual_emac_src_port_detect(status, priv, ndev, skb) \
  455. do { \
  456. if (!priv->data.dual_emac) \
  457. break; \
  458. if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
  459. ndev = cpsw_get_slave_ndev(priv, 0); \
  460. priv = netdev_priv(ndev); \
  461. skb->dev = ndev; \
  462. } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
  463. ndev = cpsw_get_slave_ndev(priv, 1); \
  464. priv = netdev_priv(ndev); \
  465. skb->dev = ndev; \
  466. } \
  467. } while (0)
  468. #define cpsw_add_mcast(priv, addr) \
  469. do { \
  470. if (priv->data.dual_emac) { \
  471. struct cpsw_slave *slave = priv->slaves + \
  472. priv->emac_port; \
  473. int slave_port = cpsw_get_slave_port(priv, \
  474. slave->slave_num); \
  475. cpsw_ale_add_mcast(priv->ale, addr, \
  476. 1 << slave_port | 1 << priv->host_port, \
  477. ALE_VLAN, slave->port_vlan, 0); \
  478. } else { \
  479. cpsw_ale_add_mcast(priv->ale, addr, \
  480. ALE_ALL_PORTS << priv->host_port, \
  481. 0, 0, 0); \
  482. } \
  483. } while (0)
  484. static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
  485. {
  486. if (priv->host_port == 0)
  487. return slave_num + 1;
  488. else
  489. return slave_num;
  490. }
  491. static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
  492. {
  493. struct cpsw_priv *priv = netdev_priv(ndev);
  494. if (ndev->flags & IFF_PROMISC) {
  495. /* Enable promiscuous mode */
  496. dev_err(priv->dev, "Ignoring Promiscuous mode\n");
  497. return;
  498. }
  499. /* Clear all mcast from ALE */
  500. cpsw_ale_flush_multicast(priv->ale, ALE_ALL_PORTS << priv->host_port);
  501. if (!netdev_mc_empty(ndev)) {
  502. struct netdev_hw_addr *ha;
  503. /* program multicast address list into ALE register */
  504. netdev_for_each_mc_addr(ha, ndev) {
  505. cpsw_add_mcast(priv, (u8 *)ha->addr);
  506. }
  507. }
  508. }
  509. static void cpsw_intr_enable(struct cpsw_priv *priv)
  510. {
  511. __raw_writel(0xFF, &priv->wr_regs->tx_en);
  512. __raw_writel(0xFF, &priv->wr_regs->rx_en);
  513. cpdma_ctlr_int_ctrl(priv->dma, true);
  514. return;
  515. }
  516. static void cpsw_intr_disable(struct cpsw_priv *priv)
  517. {
  518. __raw_writel(0, &priv->wr_regs->tx_en);
  519. __raw_writel(0, &priv->wr_regs->rx_en);
  520. cpdma_ctlr_int_ctrl(priv->dma, false);
  521. return;
  522. }
  523. void cpsw_tx_handler(void *token, int len, int status)
  524. {
  525. struct sk_buff *skb = token;
  526. struct net_device *ndev = skb->dev;
  527. struct cpsw_priv *priv = netdev_priv(ndev);
  528. /* Check whether the queue is stopped due to stalled tx dma, if the
  529. * queue is stopped then start the queue as we have free desc for tx
  530. */
  531. if (unlikely(netif_queue_stopped(ndev)))
  532. netif_wake_queue(ndev);
  533. cpts_tx_timestamp(priv->cpts, skb);
  534. priv->stats.tx_packets++;
  535. priv->stats.tx_bytes += len;
  536. dev_kfree_skb_any(skb);
  537. }
  538. void cpsw_rx_handler(void *token, int len, int status)
  539. {
  540. struct sk_buff *skb = token;
  541. struct sk_buff *new_skb;
  542. struct net_device *ndev = skb->dev;
  543. struct cpsw_priv *priv = netdev_priv(ndev);
  544. int ret = 0;
  545. cpsw_dual_emac_src_port_detect(status, priv, ndev, skb);
  546. if (unlikely(status < 0)) {
  547. /* the interface is going down, skbs are purged */
  548. dev_kfree_skb_any(skb);
  549. return;
  550. }
  551. new_skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
  552. if (new_skb) {
  553. skb_put(skb, len);
  554. cpts_rx_timestamp(priv->cpts, skb);
  555. skb->protocol = eth_type_trans(skb, ndev);
  556. netif_receive_skb(skb);
  557. priv->stats.rx_bytes += len;
  558. priv->stats.rx_packets++;
  559. } else {
  560. priv->stats.rx_dropped++;
  561. new_skb = skb;
  562. }
  563. ret = cpdma_chan_submit(priv->rxch, new_skb, new_skb->data,
  564. skb_tailroom(new_skb), 0);
  565. if (WARN_ON(ret < 0))
  566. dev_kfree_skb_any(new_skb);
  567. }
  568. static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
  569. {
  570. struct cpsw_priv *priv = dev_id;
  571. u32 rx, tx, rx_thresh;
  572. rx_thresh = __raw_readl(&priv->wr_regs->rx_thresh_stat);
  573. rx = __raw_readl(&priv->wr_regs->rx_stat);
  574. tx = __raw_readl(&priv->wr_regs->tx_stat);
  575. if (!rx_thresh && !rx && !tx)
  576. return IRQ_NONE;
  577. cpsw_intr_disable(priv);
  578. if (priv->irq_enabled == true) {
  579. cpsw_disable_irq(priv);
  580. priv->irq_enabled = false;
  581. }
  582. if (netif_running(priv->ndev)) {
  583. napi_schedule(&priv->napi);
  584. return IRQ_HANDLED;
  585. }
  586. priv = cpsw_get_slave_priv(priv, 1);
  587. if (!priv)
  588. return IRQ_NONE;
  589. if (netif_running(priv->ndev)) {
  590. napi_schedule(&priv->napi);
  591. return IRQ_HANDLED;
  592. }
  593. return IRQ_NONE;
  594. }
  595. static int cpsw_poll(struct napi_struct *napi, int budget)
  596. {
  597. struct cpsw_priv *priv = napi_to_priv(napi);
  598. int num_tx, num_rx;
  599. num_tx = cpdma_chan_process(priv->txch, 128);
  600. if (num_tx)
  601. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
  602. num_rx = cpdma_chan_process(priv->rxch, budget);
  603. if (num_rx < budget) {
  604. struct cpsw_priv *prim_cpsw;
  605. napi_complete(napi);
  606. cpsw_intr_enable(priv);
  607. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
  608. prim_cpsw = cpsw_get_slave_priv(priv, 0);
  609. if (prim_cpsw->irq_enabled == false) {
  610. prim_cpsw->irq_enabled = true;
  611. cpsw_enable_irq(priv);
  612. }
  613. }
  614. if (num_rx || num_tx)
  615. cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n",
  616. num_rx, num_tx);
  617. return num_rx;
  618. }
  619. static inline void soft_reset(const char *module, void __iomem *reg)
  620. {
  621. unsigned long timeout = jiffies + HZ;
  622. __raw_writel(1, reg);
  623. do {
  624. cpu_relax();
  625. } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
  626. WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
  627. }
  628. #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
  629. ((mac)[2] << 16) | ((mac)[3] << 24))
  630. #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
  631. static void cpsw_set_slave_mac(struct cpsw_slave *slave,
  632. struct cpsw_priv *priv)
  633. {
  634. slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
  635. slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
  636. }
  637. static void _cpsw_adjust_link(struct cpsw_slave *slave,
  638. struct cpsw_priv *priv, bool *link)
  639. {
  640. struct phy_device *phy = slave->phy;
  641. u32 mac_control = 0;
  642. u32 slave_port;
  643. if (!phy)
  644. return;
  645. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  646. if (phy->link) {
  647. mac_control = priv->data.mac_control;
  648. /* enable forwarding */
  649. cpsw_ale_control_set(priv->ale, slave_port,
  650. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  651. if (phy->speed == 1000)
  652. mac_control |= BIT(7); /* GIGABITEN */
  653. if (phy->duplex)
  654. mac_control |= BIT(0); /* FULLDUPLEXEN */
  655. /* set speed_in input in case RMII mode is used in 100Mbps */
  656. if (phy->speed == 100)
  657. mac_control |= BIT(15);
  658. *link = true;
  659. } else {
  660. mac_control = 0;
  661. /* disable forwarding */
  662. cpsw_ale_control_set(priv->ale, slave_port,
  663. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  664. }
  665. if (mac_control != slave->mac_control) {
  666. phy_print_status(phy);
  667. __raw_writel(mac_control, &slave->sliver->mac_control);
  668. }
  669. slave->mac_control = mac_control;
  670. }
  671. static void cpsw_adjust_link(struct net_device *ndev)
  672. {
  673. struct cpsw_priv *priv = netdev_priv(ndev);
  674. bool link = false;
  675. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  676. if (link) {
  677. netif_carrier_on(ndev);
  678. if (netif_running(ndev))
  679. netif_wake_queue(ndev);
  680. } else {
  681. netif_carrier_off(ndev);
  682. netif_stop_queue(ndev);
  683. }
  684. }
  685. static int cpsw_get_coalesce(struct net_device *ndev,
  686. struct ethtool_coalesce *coal)
  687. {
  688. struct cpsw_priv *priv = netdev_priv(ndev);
  689. coal->rx_coalesce_usecs = priv->coal_intvl;
  690. return 0;
  691. }
  692. static int cpsw_set_coalesce(struct net_device *ndev,
  693. struct ethtool_coalesce *coal)
  694. {
  695. struct cpsw_priv *priv = netdev_priv(ndev);
  696. u32 int_ctrl;
  697. u32 num_interrupts = 0;
  698. u32 prescale = 0;
  699. u32 addnl_dvdr = 1;
  700. u32 coal_intvl = 0;
  701. if (!coal->rx_coalesce_usecs)
  702. return -EINVAL;
  703. coal_intvl = coal->rx_coalesce_usecs;
  704. int_ctrl = readl(&priv->wr_regs->int_control);
  705. prescale = priv->bus_freq_mhz * 4;
  706. if (coal_intvl < CPSW_CMINTMIN_INTVL)
  707. coal_intvl = CPSW_CMINTMIN_INTVL;
  708. if (coal_intvl > CPSW_CMINTMAX_INTVL) {
  709. /* Interrupt pacer works with 4us Pulse, we can
  710. * throttle further by dilating the 4us pulse.
  711. */
  712. addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
  713. if (addnl_dvdr > 1) {
  714. prescale *= addnl_dvdr;
  715. if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
  716. coal_intvl = (CPSW_CMINTMAX_INTVL
  717. * addnl_dvdr);
  718. } else {
  719. addnl_dvdr = 1;
  720. coal_intvl = CPSW_CMINTMAX_INTVL;
  721. }
  722. }
  723. num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
  724. writel(num_interrupts, &priv->wr_regs->rx_imax);
  725. writel(num_interrupts, &priv->wr_regs->tx_imax);
  726. int_ctrl |= CPSW_INTPACEEN;
  727. int_ctrl &= (~CPSW_INTPRESCALE_MASK);
  728. int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
  729. writel(int_ctrl, &priv->wr_regs->int_control);
  730. cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
  731. if (priv->data.dual_emac) {
  732. int i;
  733. for (i = 0; i < priv->data.slaves; i++) {
  734. priv = netdev_priv(priv->slaves[i].ndev);
  735. priv->coal_intvl = coal_intvl;
  736. }
  737. } else {
  738. priv->coal_intvl = coal_intvl;
  739. }
  740. return 0;
  741. }
  742. static int cpsw_get_sset_count(struct net_device *ndev, int sset)
  743. {
  744. switch (sset) {
  745. case ETH_SS_STATS:
  746. return CPSW_STATS_LEN;
  747. default:
  748. return -EOPNOTSUPP;
  749. }
  750. }
  751. static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  752. {
  753. u8 *p = data;
  754. int i;
  755. switch (stringset) {
  756. case ETH_SS_STATS:
  757. for (i = 0; i < CPSW_STATS_LEN; i++) {
  758. memcpy(p, cpsw_gstrings_stats[i].stat_string,
  759. ETH_GSTRING_LEN);
  760. p += ETH_GSTRING_LEN;
  761. }
  762. break;
  763. }
  764. }
  765. static void cpsw_get_ethtool_stats(struct net_device *ndev,
  766. struct ethtool_stats *stats, u64 *data)
  767. {
  768. struct cpsw_priv *priv = netdev_priv(ndev);
  769. struct cpdma_chan_stats rx_stats;
  770. struct cpdma_chan_stats tx_stats;
  771. u32 val;
  772. u8 *p;
  773. int i;
  774. /* Collect Davinci CPDMA stats for Rx and Tx Channel */
  775. cpdma_chan_get_stats(priv->rxch, &rx_stats);
  776. cpdma_chan_get_stats(priv->txch, &tx_stats);
  777. for (i = 0; i < CPSW_STATS_LEN; i++) {
  778. switch (cpsw_gstrings_stats[i].type) {
  779. case CPSW_STATS:
  780. val = readl(priv->hw_stats +
  781. cpsw_gstrings_stats[i].stat_offset);
  782. data[i] = val;
  783. break;
  784. case CPDMA_RX_STATS:
  785. p = (u8 *)&rx_stats +
  786. cpsw_gstrings_stats[i].stat_offset;
  787. data[i] = *(u32 *)p;
  788. break;
  789. case CPDMA_TX_STATS:
  790. p = (u8 *)&tx_stats +
  791. cpsw_gstrings_stats[i].stat_offset;
  792. data[i] = *(u32 *)p;
  793. break;
  794. }
  795. }
  796. }
  797. static inline int __show_stat(char *buf, int maxlen, const char *name, u32 val)
  798. {
  799. static char *leader = "........................................";
  800. if (!val)
  801. return 0;
  802. else
  803. return snprintf(buf, maxlen, "%s %s %10d\n", name,
  804. leader + strlen(name), val);
  805. }
  806. static int cpsw_common_res_usage_state(struct cpsw_priv *priv)
  807. {
  808. u32 i;
  809. u32 usage_count = 0;
  810. if (!priv->data.dual_emac)
  811. return 0;
  812. for (i = 0; i < priv->data.slaves; i++)
  813. if (priv->slaves[i].open_stat)
  814. usage_count++;
  815. return usage_count;
  816. }
  817. static inline int cpsw_tx_packet_submit(struct net_device *ndev,
  818. struct cpsw_priv *priv, struct sk_buff *skb)
  819. {
  820. if (!priv->data.dual_emac)
  821. return cpdma_chan_submit(priv->txch, skb, skb->data,
  822. skb->len, 0);
  823. if (ndev == cpsw_get_slave_ndev(priv, 0))
  824. return cpdma_chan_submit(priv->txch, skb, skb->data,
  825. skb->len, 1);
  826. else
  827. return cpdma_chan_submit(priv->txch, skb, skb->data,
  828. skb->len, 2);
  829. }
  830. static inline void cpsw_add_dual_emac_def_ale_entries(
  831. struct cpsw_priv *priv, struct cpsw_slave *slave,
  832. u32 slave_port)
  833. {
  834. u32 port_mask = 1 << slave_port | 1 << priv->host_port;
  835. if (priv->version == CPSW_VERSION_1)
  836. slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
  837. else
  838. slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
  839. cpsw_ale_add_vlan(priv->ale, slave->port_vlan, port_mask,
  840. port_mask, port_mask, 0);
  841. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  842. port_mask, ALE_VLAN, slave->port_vlan, 0);
  843. cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
  844. priv->host_port, ALE_VLAN, slave->port_vlan);
  845. }
  846. static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
  847. {
  848. char name[32];
  849. u32 slave_port;
  850. sprintf(name, "slave-%d", slave->slave_num);
  851. soft_reset(name, &slave->sliver->soft_reset);
  852. /* setup priority mapping */
  853. __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
  854. switch (priv->version) {
  855. case CPSW_VERSION_1:
  856. slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
  857. break;
  858. case CPSW_VERSION_2:
  859. case CPSW_VERSION_3:
  860. case CPSW_VERSION_4:
  861. slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
  862. break;
  863. }
  864. /* setup max packet size, and mac address */
  865. __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
  866. cpsw_set_slave_mac(slave, priv);
  867. slave->mac_control = 0; /* no link yet */
  868. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  869. if (priv->data.dual_emac)
  870. cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
  871. else
  872. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  873. 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
  874. slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
  875. &cpsw_adjust_link, slave->data->phy_if);
  876. if (IS_ERR(slave->phy)) {
  877. dev_err(priv->dev, "phy %s not found on slave %d\n",
  878. slave->data->phy_id, slave->slave_num);
  879. slave->phy = NULL;
  880. } else {
  881. dev_info(priv->dev, "phy found : id is : 0x%x\n",
  882. slave->phy->phy_id);
  883. phy_start(slave->phy);
  884. }
  885. }
  886. static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
  887. {
  888. const int vlan = priv->data.default_vlan;
  889. const int port = priv->host_port;
  890. u32 reg;
  891. int i;
  892. reg = (priv->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
  893. CPSW2_PORT_VLAN;
  894. writel(vlan, &priv->host_port_regs->port_vlan);
  895. for (i = 0; i < priv->data.slaves; i++)
  896. slave_write(priv->slaves + i, vlan, reg);
  897. cpsw_ale_add_vlan(priv->ale, vlan, ALE_ALL_PORTS << port,
  898. ALE_ALL_PORTS << port, ALE_ALL_PORTS << port,
  899. (ALE_PORT_1 | ALE_PORT_2) << port);
  900. }
  901. static void cpsw_init_host_port(struct cpsw_priv *priv)
  902. {
  903. u32 control_reg;
  904. u32 fifo_mode;
  905. /* soft reset the controller and initialize ale */
  906. soft_reset("cpsw", &priv->regs->soft_reset);
  907. cpsw_ale_start(priv->ale);
  908. /* switch to vlan unaware mode */
  909. cpsw_ale_control_set(priv->ale, priv->host_port, ALE_VLAN_AWARE,
  910. CPSW_ALE_VLAN_AWARE);
  911. control_reg = readl(&priv->regs->control);
  912. control_reg |= CPSW_VLAN_AWARE;
  913. writel(control_reg, &priv->regs->control);
  914. fifo_mode = (priv->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
  915. CPSW_FIFO_NORMAL_MODE;
  916. writel(fifo_mode, &priv->host_port_regs->tx_in_ctl);
  917. /* setup host port priority mapping */
  918. __raw_writel(CPDMA_TX_PRIORITY_MAP,
  919. &priv->host_port_regs->cpdma_tx_pri_map);
  920. __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
  921. cpsw_ale_control_set(priv->ale, priv->host_port,
  922. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  923. if (!priv->data.dual_emac) {
  924. cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port,
  925. 0, 0);
  926. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  927. 1 << priv->host_port, 0, 0, ALE_MCAST_FWD_2);
  928. }
  929. }
  930. static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
  931. {
  932. if (!slave->phy)
  933. return;
  934. phy_stop(slave->phy);
  935. phy_disconnect(slave->phy);
  936. slave->phy = NULL;
  937. }
  938. static int cpsw_ndo_open(struct net_device *ndev)
  939. {
  940. struct cpsw_priv *priv = netdev_priv(ndev);
  941. struct cpsw_priv *prim_cpsw;
  942. int i, ret;
  943. u32 reg;
  944. if (!cpsw_common_res_usage_state(priv))
  945. cpsw_intr_disable(priv);
  946. netif_carrier_off(ndev);
  947. pm_runtime_get_sync(&priv->pdev->dev);
  948. reg = priv->version;
  949. dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
  950. CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
  951. CPSW_RTL_VERSION(reg));
  952. /* initialize host and slave ports */
  953. if (!cpsw_common_res_usage_state(priv))
  954. cpsw_init_host_port(priv);
  955. for_each_slave(priv, cpsw_slave_open, priv);
  956. /* Add default VLAN */
  957. if (!priv->data.dual_emac)
  958. cpsw_add_default_vlan(priv);
  959. if (!cpsw_common_res_usage_state(priv)) {
  960. /* setup tx dma to fixed prio and zero offset */
  961. cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
  962. cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
  963. /* disable priority elevation */
  964. __raw_writel(0, &priv->regs->ptype);
  965. /* enable statistics collection only on all ports */
  966. __raw_writel(0x7, &priv->regs->stat_port_en);
  967. if (WARN_ON(!priv->data.rx_descs))
  968. priv->data.rx_descs = 128;
  969. for (i = 0; i < priv->data.rx_descs; i++) {
  970. struct sk_buff *skb;
  971. ret = -ENOMEM;
  972. skb = __netdev_alloc_skb_ip_align(priv->ndev,
  973. priv->rx_packet_max, GFP_KERNEL);
  974. if (!skb)
  975. goto err_cleanup;
  976. ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
  977. skb_tailroom(skb), 0);
  978. if (ret < 0) {
  979. kfree_skb(skb);
  980. goto err_cleanup;
  981. }
  982. }
  983. /* continue even if we didn't manage to submit all
  984. * receive descs
  985. */
  986. cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
  987. }
  988. /* Enable Interrupt pacing if configured */
  989. if (priv->coal_intvl != 0) {
  990. struct ethtool_coalesce coal;
  991. coal.rx_coalesce_usecs = (priv->coal_intvl << 4);
  992. cpsw_set_coalesce(ndev, &coal);
  993. }
  994. prim_cpsw = cpsw_get_slave_priv(priv, 0);
  995. if (prim_cpsw->irq_enabled == false) {
  996. if ((priv == prim_cpsw) || !netif_running(prim_cpsw->ndev)) {
  997. prim_cpsw->irq_enabled = true;
  998. cpsw_enable_irq(prim_cpsw);
  999. }
  1000. }
  1001. napi_enable(&priv->napi);
  1002. cpdma_ctlr_start(priv->dma);
  1003. cpsw_intr_enable(priv);
  1004. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
  1005. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
  1006. if (priv->data.dual_emac)
  1007. priv->slaves[priv->emac_port].open_stat = true;
  1008. return 0;
  1009. err_cleanup:
  1010. cpdma_ctlr_stop(priv->dma);
  1011. for_each_slave(priv, cpsw_slave_stop, priv);
  1012. pm_runtime_put_sync(&priv->pdev->dev);
  1013. netif_carrier_off(priv->ndev);
  1014. return ret;
  1015. }
  1016. static int cpsw_ndo_stop(struct net_device *ndev)
  1017. {
  1018. struct cpsw_priv *priv = netdev_priv(ndev);
  1019. cpsw_info(priv, ifdown, "shutting down cpsw device\n");
  1020. netif_stop_queue(priv->ndev);
  1021. napi_disable(&priv->napi);
  1022. netif_carrier_off(priv->ndev);
  1023. if (cpsw_common_res_usage_state(priv) <= 1) {
  1024. cpsw_intr_disable(priv);
  1025. cpdma_ctlr_int_ctrl(priv->dma, false);
  1026. cpdma_ctlr_stop(priv->dma);
  1027. cpsw_ale_stop(priv->ale);
  1028. }
  1029. for_each_slave(priv, cpsw_slave_stop, priv);
  1030. pm_runtime_put_sync(&priv->pdev->dev);
  1031. if (priv->data.dual_emac)
  1032. priv->slaves[priv->emac_port].open_stat = false;
  1033. return 0;
  1034. }
  1035. static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
  1036. struct net_device *ndev)
  1037. {
  1038. struct cpsw_priv *priv = netdev_priv(ndev);
  1039. int ret;
  1040. ndev->trans_start = jiffies;
  1041. if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
  1042. cpsw_err(priv, tx_err, "packet pad failed\n");
  1043. priv->stats.tx_dropped++;
  1044. return NETDEV_TX_OK;
  1045. }
  1046. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
  1047. priv->cpts->tx_enable)
  1048. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1049. skb_tx_timestamp(skb);
  1050. ret = cpsw_tx_packet_submit(ndev, priv, skb);
  1051. if (unlikely(ret != 0)) {
  1052. cpsw_err(priv, tx_err, "desc submit failed\n");
  1053. goto fail;
  1054. }
  1055. /* If there is no more tx desc left free then we need to
  1056. * tell the kernel to stop sending us tx frames.
  1057. */
  1058. if (unlikely(!cpdma_check_free_tx_desc(priv->txch)))
  1059. netif_stop_queue(ndev);
  1060. return NETDEV_TX_OK;
  1061. fail:
  1062. priv->stats.tx_dropped++;
  1063. netif_stop_queue(ndev);
  1064. return NETDEV_TX_BUSY;
  1065. }
  1066. static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
  1067. {
  1068. /*
  1069. * The switch cannot operate in promiscuous mode without substantial
  1070. * headache. For promiscuous mode to work, we would need to put the
  1071. * ALE in bypass mode and route all traffic to the host port.
  1072. * Subsequently, the host will need to operate as a "bridge", learn,
  1073. * and flood as needed. For now, we simply complain here and
  1074. * do nothing about it :-)
  1075. */
  1076. if ((flags & IFF_PROMISC) && (ndev->flags & IFF_PROMISC))
  1077. dev_err(&ndev->dev, "promiscuity ignored!\n");
  1078. /*
  1079. * The switch cannot filter multicast traffic unless it is configured
  1080. * in "VLAN Aware" mode. Unfortunately, VLAN awareness requires a
  1081. * whole bunch of additional logic that this driver does not implement
  1082. * at present.
  1083. */
  1084. if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
  1085. dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
  1086. }
  1087. #ifdef CONFIG_TI_CPTS
  1088. static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
  1089. {
  1090. struct cpsw_slave *slave = &priv->slaves[priv->data.active_slave];
  1091. u32 ts_en, seq_id;
  1092. if (!priv->cpts->tx_enable && !priv->cpts->rx_enable) {
  1093. slave_write(slave, 0, CPSW1_TS_CTL);
  1094. return;
  1095. }
  1096. seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
  1097. ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
  1098. if (priv->cpts->tx_enable)
  1099. ts_en |= CPSW_V1_TS_TX_EN;
  1100. if (priv->cpts->rx_enable)
  1101. ts_en |= CPSW_V1_TS_RX_EN;
  1102. slave_write(slave, ts_en, CPSW1_TS_CTL);
  1103. slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
  1104. }
  1105. static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
  1106. {
  1107. struct cpsw_slave *slave;
  1108. u32 ctrl, mtype;
  1109. if (priv->data.dual_emac)
  1110. slave = &priv->slaves[priv->emac_port];
  1111. else
  1112. slave = &priv->slaves[priv->data.active_slave];
  1113. ctrl = slave_read(slave, CPSW2_CONTROL);
  1114. ctrl &= ~CTRL_ALL_TS_MASK;
  1115. if (priv->cpts->tx_enable)
  1116. ctrl |= CTRL_TX_TS_BITS;
  1117. if (priv->cpts->rx_enable)
  1118. ctrl |= CTRL_RX_TS_BITS;
  1119. mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
  1120. slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
  1121. slave_write(slave, ctrl, CPSW2_CONTROL);
  1122. __raw_writel(ETH_P_1588, &priv->regs->ts_ltype);
  1123. }
  1124. static int cpsw_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
  1125. {
  1126. struct cpsw_priv *priv = netdev_priv(dev);
  1127. struct cpts *cpts = priv->cpts;
  1128. struct hwtstamp_config cfg;
  1129. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  1130. return -EFAULT;
  1131. /* reserved for future extensions */
  1132. if (cfg.flags)
  1133. return -EINVAL;
  1134. switch (cfg.tx_type) {
  1135. case HWTSTAMP_TX_OFF:
  1136. cpts->tx_enable = 0;
  1137. break;
  1138. case HWTSTAMP_TX_ON:
  1139. cpts->tx_enable = 1;
  1140. break;
  1141. default:
  1142. return -ERANGE;
  1143. }
  1144. switch (cfg.rx_filter) {
  1145. case HWTSTAMP_FILTER_NONE:
  1146. cpts->rx_enable = 0;
  1147. break;
  1148. case HWTSTAMP_FILTER_ALL:
  1149. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1150. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1151. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1152. return -ERANGE;
  1153. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1154. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1155. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1156. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1157. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1158. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1159. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1160. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1161. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1162. cpts->rx_enable = 1;
  1163. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  1164. break;
  1165. default:
  1166. return -ERANGE;
  1167. }
  1168. switch (priv->version) {
  1169. case CPSW_VERSION_1:
  1170. cpsw_hwtstamp_v1(priv);
  1171. break;
  1172. case CPSW_VERSION_2:
  1173. cpsw_hwtstamp_v2(priv);
  1174. break;
  1175. default:
  1176. return -ENOTSUPP;
  1177. }
  1178. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  1179. }
  1180. #endif /*CONFIG_TI_CPTS*/
  1181. static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  1182. {
  1183. struct cpsw_priv *priv = netdev_priv(dev);
  1184. struct mii_ioctl_data *data = if_mii(req);
  1185. int slave_no = cpsw_slave_index(priv);
  1186. if (!netif_running(dev))
  1187. return -EINVAL;
  1188. switch (cmd) {
  1189. #ifdef CONFIG_TI_CPTS
  1190. case SIOCSHWTSTAMP:
  1191. return cpsw_hwtstamp_ioctl(dev, req);
  1192. #endif
  1193. case SIOCGMIIPHY:
  1194. data->phy_id = priv->slaves[slave_no].phy->addr;
  1195. break;
  1196. default:
  1197. return -ENOTSUPP;
  1198. }
  1199. return 0;
  1200. }
  1201. static void cpsw_ndo_tx_timeout(struct net_device *ndev)
  1202. {
  1203. struct cpsw_priv *priv = netdev_priv(ndev);
  1204. cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
  1205. priv->stats.tx_errors++;
  1206. cpsw_intr_disable(priv);
  1207. cpdma_ctlr_int_ctrl(priv->dma, false);
  1208. cpdma_chan_stop(priv->txch);
  1209. cpdma_chan_start(priv->txch);
  1210. cpdma_ctlr_int_ctrl(priv->dma, true);
  1211. cpsw_intr_enable(priv);
  1212. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
  1213. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
  1214. }
  1215. static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
  1216. {
  1217. struct cpsw_priv *priv = netdev_priv(ndev);
  1218. struct sockaddr *addr = (struct sockaddr *)p;
  1219. int flags = 0;
  1220. u16 vid = 0;
  1221. if (!is_valid_ether_addr(addr->sa_data))
  1222. return -EADDRNOTAVAIL;
  1223. if (priv->data.dual_emac) {
  1224. vid = priv->slaves[priv->emac_port].port_vlan;
  1225. flags = ALE_VLAN;
  1226. }
  1227. cpsw_ale_del_ucast(priv->ale, priv->mac_addr, priv->host_port,
  1228. flags, vid);
  1229. cpsw_ale_add_ucast(priv->ale, addr->sa_data, priv->host_port,
  1230. flags, vid);
  1231. memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
  1232. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  1233. for_each_slave(priv, cpsw_set_slave_mac, priv);
  1234. return 0;
  1235. }
  1236. static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev)
  1237. {
  1238. struct cpsw_priv *priv = netdev_priv(ndev);
  1239. return &priv->stats;
  1240. }
  1241. #ifdef CONFIG_NET_POLL_CONTROLLER
  1242. static void cpsw_ndo_poll_controller(struct net_device *ndev)
  1243. {
  1244. struct cpsw_priv *priv = netdev_priv(ndev);
  1245. cpsw_intr_disable(priv);
  1246. cpdma_ctlr_int_ctrl(priv->dma, false);
  1247. cpsw_interrupt(ndev->irq, priv);
  1248. cpdma_ctlr_int_ctrl(priv->dma, true);
  1249. cpsw_intr_enable(priv);
  1250. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX);
  1251. cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX);
  1252. }
  1253. #endif
  1254. static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
  1255. unsigned short vid)
  1256. {
  1257. int ret;
  1258. ret = cpsw_ale_add_vlan(priv->ale, vid,
  1259. ALE_ALL_PORTS << priv->host_port,
  1260. 0, ALE_ALL_PORTS << priv->host_port,
  1261. (ALE_PORT_1 | ALE_PORT_2) << priv->host_port);
  1262. if (ret != 0)
  1263. return ret;
  1264. ret = cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
  1265. priv->host_port, ALE_VLAN, vid);
  1266. if (ret != 0)
  1267. goto clean_vid;
  1268. ret = cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  1269. ALE_ALL_PORTS << priv->host_port,
  1270. ALE_VLAN, vid, 0);
  1271. if (ret != 0)
  1272. goto clean_vlan_ucast;
  1273. return 0;
  1274. clean_vlan_ucast:
  1275. cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
  1276. priv->host_port, ALE_VLAN, vid);
  1277. clean_vid:
  1278. cpsw_ale_del_vlan(priv->ale, vid, 0);
  1279. return ret;
  1280. }
  1281. static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
  1282. __be16 proto, u16 vid)
  1283. {
  1284. struct cpsw_priv *priv = netdev_priv(ndev);
  1285. if (vid == priv->data.default_vlan)
  1286. return 0;
  1287. dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
  1288. return cpsw_add_vlan_ale_entry(priv, vid);
  1289. }
  1290. static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
  1291. __be16 proto, u16 vid)
  1292. {
  1293. struct cpsw_priv *priv = netdev_priv(ndev);
  1294. int ret;
  1295. if (vid == priv->data.default_vlan)
  1296. return 0;
  1297. dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
  1298. ret = cpsw_ale_del_vlan(priv->ale, vid, 0);
  1299. if (ret != 0)
  1300. return ret;
  1301. ret = cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
  1302. priv->host_port, ALE_VLAN, vid);
  1303. if (ret != 0)
  1304. return ret;
  1305. return cpsw_ale_del_mcast(priv->ale, priv->ndev->broadcast,
  1306. 0, ALE_VLAN, vid);
  1307. }
  1308. static const struct net_device_ops cpsw_netdev_ops = {
  1309. .ndo_open = cpsw_ndo_open,
  1310. .ndo_stop = cpsw_ndo_stop,
  1311. .ndo_start_xmit = cpsw_ndo_start_xmit,
  1312. .ndo_change_rx_flags = cpsw_ndo_change_rx_flags,
  1313. .ndo_set_mac_address = cpsw_ndo_set_mac_address,
  1314. .ndo_do_ioctl = cpsw_ndo_ioctl,
  1315. .ndo_validate_addr = eth_validate_addr,
  1316. .ndo_change_mtu = eth_change_mtu,
  1317. .ndo_tx_timeout = cpsw_ndo_tx_timeout,
  1318. .ndo_get_stats = cpsw_ndo_get_stats,
  1319. .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
  1320. #ifdef CONFIG_NET_POLL_CONTROLLER
  1321. .ndo_poll_controller = cpsw_ndo_poll_controller,
  1322. #endif
  1323. .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
  1324. .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
  1325. };
  1326. static void cpsw_get_drvinfo(struct net_device *ndev,
  1327. struct ethtool_drvinfo *info)
  1328. {
  1329. struct cpsw_priv *priv = netdev_priv(ndev);
  1330. strlcpy(info->driver, "TI CPSW Driver v1.0", sizeof(info->driver));
  1331. strlcpy(info->version, "1.0", sizeof(info->version));
  1332. strlcpy(info->bus_info, priv->pdev->name, sizeof(info->bus_info));
  1333. }
  1334. static u32 cpsw_get_msglevel(struct net_device *ndev)
  1335. {
  1336. struct cpsw_priv *priv = netdev_priv(ndev);
  1337. return priv->msg_enable;
  1338. }
  1339. static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
  1340. {
  1341. struct cpsw_priv *priv = netdev_priv(ndev);
  1342. priv->msg_enable = value;
  1343. }
  1344. static int cpsw_get_ts_info(struct net_device *ndev,
  1345. struct ethtool_ts_info *info)
  1346. {
  1347. #ifdef CONFIG_TI_CPTS
  1348. struct cpsw_priv *priv = netdev_priv(ndev);
  1349. info->so_timestamping =
  1350. SOF_TIMESTAMPING_TX_HARDWARE |
  1351. SOF_TIMESTAMPING_TX_SOFTWARE |
  1352. SOF_TIMESTAMPING_RX_HARDWARE |
  1353. SOF_TIMESTAMPING_RX_SOFTWARE |
  1354. SOF_TIMESTAMPING_SOFTWARE |
  1355. SOF_TIMESTAMPING_RAW_HARDWARE;
  1356. info->phc_index = priv->cpts->phc_index;
  1357. info->tx_types =
  1358. (1 << HWTSTAMP_TX_OFF) |
  1359. (1 << HWTSTAMP_TX_ON);
  1360. info->rx_filters =
  1361. (1 << HWTSTAMP_FILTER_NONE) |
  1362. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
  1363. #else
  1364. info->so_timestamping =
  1365. SOF_TIMESTAMPING_TX_SOFTWARE |
  1366. SOF_TIMESTAMPING_RX_SOFTWARE |
  1367. SOF_TIMESTAMPING_SOFTWARE;
  1368. info->phc_index = -1;
  1369. info->tx_types = 0;
  1370. info->rx_filters = 0;
  1371. #endif
  1372. return 0;
  1373. }
  1374. static int cpsw_get_settings(struct net_device *ndev,
  1375. struct ethtool_cmd *ecmd)
  1376. {
  1377. struct cpsw_priv *priv = netdev_priv(ndev);
  1378. int slave_no = cpsw_slave_index(priv);
  1379. if (priv->slaves[slave_no].phy)
  1380. return phy_ethtool_gset(priv->slaves[slave_no].phy, ecmd);
  1381. else
  1382. return -EOPNOTSUPP;
  1383. }
  1384. static int cpsw_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
  1385. {
  1386. struct cpsw_priv *priv = netdev_priv(ndev);
  1387. int slave_no = cpsw_slave_index(priv);
  1388. if (priv->slaves[slave_no].phy)
  1389. return phy_ethtool_sset(priv->slaves[slave_no].phy, ecmd);
  1390. else
  1391. return -EOPNOTSUPP;
  1392. }
  1393. static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  1394. {
  1395. struct cpsw_priv *priv = netdev_priv(ndev);
  1396. int slave_no = cpsw_slave_index(priv);
  1397. wol->supported = 0;
  1398. wol->wolopts = 0;
  1399. if (priv->slaves[slave_no].phy)
  1400. phy_ethtool_get_wol(priv->slaves[slave_no].phy, wol);
  1401. }
  1402. static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  1403. {
  1404. struct cpsw_priv *priv = netdev_priv(ndev);
  1405. int slave_no = cpsw_slave_index(priv);
  1406. if (priv->slaves[slave_no].phy)
  1407. return phy_ethtool_set_wol(priv->slaves[slave_no].phy, wol);
  1408. else
  1409. return -EOPNOTSUPP;
  1410. }
  1411. static const struct ethtool_ops cpsw_ethtool_ops = {
  1412. .get_drvinfo = cpsw_get_drvinfo,
  1413. .get_msglevel = cpsw_get_msglevel,
  1414. .set_msglevel = cpsw_set_msglevel,
  1415. .get_link = ethtool_op_get_link,
  1416. .get_ts_info = cpsw_get_ts_info,
  1417. .get_settings = cpsw_get_settings,
  1418. .set_settings = cpsw_set_settings,
  1419. .get_coalesce = cpsw_get_coalesce,
  1420. .set_coalesce = cpsw_set_coalesce,
  1421. .get_sset_count = cpsw_get_sset_count,
  1422. .get_strings = cpsw_get_strings,
  1423. .get_ethtool_stats = cpsw_get_ethtool_stats,
  1424. .get_wol = cpsw_get_wol,
  1425. .set_wol = cpsw_set_wol,
  1426. };
  1427. static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv,
  1428. u32 slave_reg_ofs, u32 sliver_reg_ofs)
  1429. {
  1430. void __iomem *regs = priv->regs;
  1431. int slave_num = slave->slave_num;
  1432. struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
  1433. slave->data = data;
  1434. slave->regs = regs + slave_reg_ofs;
  1435. slave->sliver = regs + sliver_reg_ofs;
  1436. slave->port_vlan = data->dual_emac_res_vlan;
  1437. }
  1438. static int cpsw_probe_dt(struct cpsw_platform_data *data,
  1439. struct platform_device *pdev)
  1440. {
  1441. struct device_node *node = pdev->dev.of_node;
  1442. struct device_node *slave_node;
  1443. int i = 0, ret;
  1444. u32 prop;
  1445. if (!node)
  1446. return -EINVAL;
  1447. if (of_property_read_u32(node, "slaves", &prop)) {
  1448. pr_err("Missing slaves property in the DT.\n");
  1449. return -EINVAL;
  1450. }
  1451. data->slaves = prop;
  1452. if (of_property_read_u32(node, "active_slave", &prop)) {
  1453. pr_err("Missing active_slave property in the DT.\n");
  1454. ret = -EINVAL;
  1455. goto error_ret;
  1456. }
  1457. data->active_slave = prop;
  1458. if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
  1459. pr_err("Missing cpts_clock_mult property in the DT.\n");
  1460. ret = -EINVAL;
  1461. goto error_ret;
  1462. }
  1463. data->cpts_clock_mult = prop;
  1464. if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
  1465. pr_err("Missing cpts_clock_shift property in the DT.\n");
  1466. ret = -EINVAL;
  1467. goto error_ret;
  1468. }
  1469. data->cpts_clock_shift = prop;
  1470. data->slave_data = kcalloc(data->slaves, sizeof(struct cpsw_slave_data),
  1471. GFP_KERNEL);
  1472. if (!data->slave_data)
  1473. return -EINVAL;
  1474. if (of_property_read_u32(node, "cpdma_channels", &prop)) {
  1475. pr_err("Missing cpdma_channels property in the DT.\n");
  1476. ret = -EINVAL;
  1477. goto error_ret;
  1478. }
  1479. data->channels = prop;
  1480. if (of_property_read_u32(node, "ale_entries", &prop)) {
  1481. pr_err("Missing ale_entries property in the DT.\n");
  1482. ret = -EINVAL;
  1483. goto error_ret;
  1484. }
  1485. data->ale_entries = prop;
  1486. if (of_property_read_u32(node, "bd_ram_size", &prop)) {
  1487. pr_err("Missing bd_ram_size property in the DT.\n");
  1488. ret = -EINVAL;
  1489. goto error_ret;
  1490. }
  1491. data->bd_ram_size = prop;
  1492. if (of_property_read_u32(node, "rx_descs", &prop)) {
  1493. pr_err("Missing rx_descs property in the DT.\n");
  1494. ret = -EINVAL;
  1495. goto error_ret;
  1496. }
  1497. data->rx_descs = prop;
  1498. if (of_property_read_u32(node, "mac_control", &prop)) {
  1499. pr_err("Missing mac_control property in the DT.\n");
  1500. ret = -EINVAL;
  1501. goto error_ret;
  1502. }
  1503. data->mac_control = prop;
  1504. if (of_property_read_bool(node, "dual_emac"))
  1505. data->dual_emac = 1;
  1506. /*
  1507. * Populate all the child nodes here...
  1508. */
  1509. ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
  1510. /* We do not want to force this, as in some cases may not have child */
  1511. if (ret)
  1512. pr_warn("Doesn't have any child node\n");
  1513. for_each_child_of_node(node, slave_node) {
  1514. struct cpsw_slave_data *slave_data = data->slave_data + i;
  1515. const void *mac_addr = NULL;
  1516. u32 phyid;
  1517. int lenp;
  1518. const __be32 *parp;
  1519. struct device_node *mdio_node;
  1520. struct platform_device *mdio;
  1521. /* This is no slave child node, continue */
  1522. if (strcmp(slave_node->name, "slave"))
  1523. continue;
  1524. parp = of_get_property(slave_node, "phy_id", &lenp);
  1525. if ((parp == NULL) || (lenp != (sizeof(void *) * 2))) {
  1526. pr_err("Missing slave[%d] phy_id property\n", i);
  1527. ret = -EINVAL;
  1528. goto error_ret;
  1529. }
  1530. mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
  1531. phyid = be32_to_cpup(parp+1);
  1532. mdio = of_find_device_by_node(mdio_node);
  1533. snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
  1534. PHY_ID_FMT, mdio->name, phyid);
  1535. mac_addr = of_get_mac_address(slave_node);
  1536. if (mac_addr)
  1537. memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
  1538. slave_data->phy_if = of_get_phy_mode(slave_node);
  1539. if (data->dual_emac) {
  1540. if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
  1541. &prop)) {
  1542. pr_err("Missing dual_emac_res_vlan in DT.\n");
  1543. slave_data->dual_emac_res_vlan = i+1;
  1544. pr_err("Using %d as Reserved VLAN for %d slave\n",
  1545. slave_data->dual_emac_res_vlan, i);
  1546. } else {
  1547. slave_data->dual_emac_res_vlan = prop;
  1548. }
  1549. }
  1550. i++;
  1551. }
  1552. return 0;
  1553. error_ret:
  1554. kfree(data->slave_data);
  1555. return ret;
  1556. }
  1557. static int cpsw_probe_dual_emac(struct platform_device *pdev,
  1558. struct cpsw_priv *priv)
  1559. {
  1560. struct cpsw_platform_data *data = &priv->data;
  1561. struct net_device *ndev;
  1562. struct cpsw_priv *priv_sl2;
  1563. int ret = 0, i;
  1564. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  1565. if (!ndev) {
  1566. pr_err("cpsw: error allocating net_device\n");
  1567. return -ENOMEM;
  1568. }
  1569. priv_sl2 = netdev_priv(ndev);
  1570. spin_lock_init(&priv_sl2->lock);
  1571. priv_sl2->data = *data;
  1572. priv_sl2->pdev = pdev;
  1573. priv_sl2->ndev = ndev;
  1574. priv_sl2->dev = &ndev->dev;
  1575. priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  1576. priv_sl2->rx_packet_max = max(rx_packet_max, 128);
  1577. if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
  1578. memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
  1579. ETH_ALEN);
  1580. pr_info("cpsw: Detected MACID = %pM\n", priv_sl2->mac_addr);
  1581. } else {
  1582. random_ether_addr(priv_sl2->mac_addr);
  1583. pr_info("cpsw: Random MACID = %pM\n", priv_sl2->mac_addr);
  1584. }
  1585. memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
  1586. priv_sl2->slaves = priv->slaves;
  1587. priv_sl2->clk = priv->clk;
  1588. priv_sl2->coal_intvl = 0;
  1589. priv_sl2->bus_freq_mhz = priv->bus_freq_mhz;
  1590. priv_sl2->cpsw_res = priv->cpsw_res;
  1591. priv_sl2->regs = priv->regs;
  1592. priv_sl2->host_port = priv->host_port;
  1593. priv_sl2->host_port_regs = priv->host_port_regs;
  1594. priv_sl2->wr_regs = priv->wr_regs;
  1595. priv_sl2->hw_stats = priv->hw_stats;
  1596. priv_sl2->dma = priv->dma;
  1597. priv_sl2->txch = priv->txch;
  1598. priv_sl2->rxch = priv->rxch;
  1599. priv_sl2->ale = priv->ale;
  1600. priv_sl2->emac_port = 1;
  1601. priv->slaves[1].ndev = ndev;
  1602. priv_sl2->cpts = priv->cpts;
  1603. priv_sl2->version = priv->version;
  1604. for (i = 0; i < priv->num_irqs; i++) {
  1605. priv_sl2->irqs_table[i] = priv->irqs_table[i];
  1606. priv_sl2->num_irqs = priv->num_irqs;
  1607. }
  1608. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1609. ndev->netdev_ops = &cpsw_netdev_ops;
  1610. SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
  1611. netif_napi_add(ndev, &priv_sl2->napi, cpsw_poll, CPSW_POLL_WEIGHT);
  1612. /* register the network device */
  1613. SET_NETDEV_DEV(ndev, &pdev->dev);
  1614. ret = register_netdev(ndev);
  1615. if (ret) {
  1616. pr_err("cpsw: error registering net device\n");
  1617. free_netdev(ndev);
  1618. ret = -ENODEV;
  1619. }
  1620. return ret;
  1621. }
  1622. static int cpsw_probe(struct platform_device *pdev)
  1623. {
  1624. struct cpsw_platform_data *data;
  1625. struct net_device *ndev;
  1626. struct cpsw_priv *priv;
  1627. struct cpdma_params dma_params;
  1628. struct cpsw_ale_params ale_params;
  1629. void __iomem *ss_regs, *wr_regs;
  1630. struct resource *res;
  1631. u32 slave_offset, sliver_offset, slave_size;
  1632. int ret = 0, i, k = 0;
  1633. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  1634. if (!ndev) {
  1635. pr_err("error allocating net_device\n");
  1636. return -ENOMEM;
  1637. }
  1638. platform_set_drvdata(pdev, ndev);
  1639. priv = netdev_priv(ndev);
  1640. spin_lock_init(&priv->lock);
  1641. priv->pdev = pdev;
  1642. priv->ndev = ndev;
  1643. priv->dev = &ndev->dev;
  1644. priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  1645. priv->rx_packet_max = max(rx_packet_max, 128);
  1646. priv->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL);
  1647. priv->irq_enabled = true;
  1648. if (!priv->cpts) {
  1649. pr_err("error allocating cpts\n");
  1650. goto clean_ndev_ret;
  1651. }
  1652. /*
  1653. * This may be required here for child devices.
  1654. */
  1655. pm_runtime_enable(&pdev->dev);
  1656. /* Select default pin state */
  1657. pinctrl_pm_select_default_state(&pdev->dev);
  1658. if (cpsw_probe_dt(&priv->data, pdev)) {
  1659. pr_err("cpsw: platform data missing\n");
  1660. ret = -ENODEV;
  1661. goto clean_ndev_ret;
  1662. }
  1663. data = &priv->data;
  1664. if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
  1665. memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
  1666. pr_info("Detected MACID = %pM\n", priv->mac_addr);
  1667. } else {
  1668. eth_random_addr(priv->mac_addr);
  1669. pr_info("Random MACID = %pM\n", priv->mac_addr);
  1670. }
  1671. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  1672. priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves,
  1673. GFP_KERNEL);
  1674. if (!priv->slaves) {
  1675. ret = -EBUSY;
  1676. goto clean_ndev_ret;
  1677. }
  1678. for (i = 0; i < data->slaves; i++)
  1679. priv->slaves[i].slave_num = i;
  1680. priv->slaves[0].ndev = ndev;
  1681. priv->emac_port = 0;
  1682. priv->clk = clk_get(&pdev->dev, "fck");
  1683. if (IS_ERR(priv->clk)) {
  1684. dev_err(&pdev->dev, "fck is not found\n");
  1685. ret = -ENODEV;
  1686. goto clean_slave_ret;
  1687. }
  1688. priv->coal_intvl = 0;
  1689. priv->bus_freq_mhz = clk_get_rate(priv->clk) / 1000000;
  1690. priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1691. if (!priv->cpsw_res) {
  1692. dev_err(priv->dev, "error getting i/o resource\n");
  1693. ret = -ENOENT;
  1694. goto clean_clk_ret;
  1695. }
  1696. if (!request_mem_region(priv->cpsw_res->start,
  1697. resource_size(priv->cpsw_res), ndev->name)) {
  1698. dev_err(priv->dev, "failed request i/o region\n");
  1699. ret = -ENXIO;
  1700. goto clean_clk_ret;
  1701. }
  1702. ss_regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res));
  1703. if (!ss_regs) {
  1704. dev_err(priv->dev, "unable to map i/o region\n");
  1705. goto clean_cpsw_iores_ret;
  1706. }
  1707. priv->regs = ss_regs;
  1708. priv->version = __raw_readl(&priv->regs->id_ver);
  1709. priv->host_port = HOST_PORT_NUM;
  1710. priv->cpsw_wr_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1711. if (!priv->cpsw_wr_res) {
  1712. dev_err(priv->dev, "error getting i/o resource\n");
  1713. ret = -ENOENT;
  1714. goto clean_iomap_ret;
  1715. }
  1716. if (!request_mem_region(priv->cpsw_wr_res->start,
  1717. resource_size(priv->cpsw_wr_res), ndev->name)) {
  1718. dev_err(priv->dev, "failed request i/o region\n");
  1719. ret = -ENXIO;
  1720. goto clean_iomap_ret;
  1721. }
  1722. wr_regs = ioremap(priv->cpsw_wr_res->start,
  1723. resource_size(priv->cpsw_wr_res));
  1724. if (!wr_regs) {
  1725. dev_err(priv->dev, "unable to map i/o region\n");
  1726. goto clean_cpsw_wr_iores_ret;
  1727. }
  1728. priv->wr_regs = wr_regs;
  1729. memset(&dma_params, 0, sizeof(dma_params));
  1730. memset(&ale_params, 0, sizeof(ale_params));
  1731. switch (priv->version) {
  1732. case CPSW_VERSION_1:
  1733. priv->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
  1734. priv->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET;
  1735. priv->hw_stats = ss_regs + CPSW1_HW_STATS;
  1736. dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
  1737. dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
  1738. ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
  1739. slave_offset = CPSW1_SLAVE_OFFSET;
  1740. slave_size = CPSW1_SLAVE_SIZE;
  1741. sliver_offset = CPSW1_SLIVER_OFFSET;
  1742. dma_params.desc_mem_phys = 0;
  1743. break;
  1744. case CPSW_VERSION_2:
  1745. case CPSW_VERSION_3:
  1746. case CPSW_VERSION_4:
  1747. priv->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
  1748. priv->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET;
  1749. priv->hw_stats = ss_regs + CPSW2_HW_STATS;
  1750. dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
  1751. dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
  1752. ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
  1753. slave_offset = CPSW2_SLAVE_OFFSET;
  1754. slave_size = CPSW2_SLAVE_SIZE;
  1755. sliver_offset = CPSW2_SLIVER_OFFSET;
  1756. dma_params.desc_mem_phys =
  1757. (u32 __force) priv->cpsw_res->start + CPSW2_BD_OFFSET;
  1758. break;
  1759. default:
  1760. dev_err(priv->dev, "unknown version 0x%08x\n", priv->version);
  1761. ret = -ENODEV;
  1762. goto clean_cpsw_wr_iores_ret;
  1763. }
  1764. for (i = 0; i < priv->data.slaves; i++) {
  1765. struct cpsw_slave *slave = &priv->slaves[i];
  1766. cpsw_slave_init(slave, priv, slave_offset, sliver_offset);
  1767. slave_offset += slave_size;
  1768. sliver_offset += SLIVER_SIZE;
  1769. }
  1770. dma_params.dev = &pdev->dev;
  1771. dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
  1772. dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
  1773. dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
  1774. dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
  1775. dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
  1776. dma_params.num_chan = data->channels;
  1777. dma_params.has_soft_reset = true;
  1778. dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
  1779. dma_params.desc_mem_size = data->bd_ram_size;
  1780. dma_params.desc_align = 16;
  1781. dma_params.has_ext_regs = true;
  1782. dma_params.desc_hw_addr = dma_params.desc_mem_phys;
  1783. priv->dma = cpdma_ctlr_create(&dma_params);
  1784. if (!priv->dma) {
  1785. dev_err(priv->dev, "error initializing dma\n");
  1786. ret = -ENOMEM;
  1787. goto clean_wr_iomap_ret;
  1788. }
  1789. priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
  1790. cpsw_tx_handler);
  1791. priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
  1792. cpsw_rx_handler);
  1793. if (WARN_ON(!priv->txch || !priv->rxch)) {
  1794. dev_err(priv->dev, "error initializing dma channels\n");
  1795. ret = -ENOMEM;
  1796. goto clean_dma_ret;
  1797. }
  1798. ale_params.dev = &ndev->dev;
  1799. ale_params.ale_ageout = ale_ageout;
  1800. ale_params.ale_entries = data->ale_entries;
  1801. ale_params.ale_ports = data->slaves;
  1802. priv->ale = cpsw_ale_create(&ale_params);
  1803. if (!priv->ale) {
  1804. dev_err(priv->dev, "error initializing ale engine\n");
  1805. ret = -ENODEV;
  1806. goto clean_dma_ret;
  1807. }
  1808. ndev->irq = platform_get_irq(pdev, 0);
  1809. if (ndev->irq < 0) {
  1810. dev_err(priv->dev, "error getting irq resource\n");
  1811. ret = -ENOENT;
  1812. goto clean_ale_ret;
  1813. }
  1814. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
  1815. for (i = res->start; i <= res->end; i++) {
  1816. if (request_irq(i, cpsw_interrupt, 0,
  1817. dev_name(&pdev->dev), priv)) {
  1818. dev_err(priv->dev, "error attaching irq\n");
  1819. goto clean_ale_ret;
  1820. }
  1821. priv->irqs_table[k] = i;
  1822. priv->num_irqs = k + 1;
  1823. }
  1824. k++;
  1825. }
  1826. ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  1827. ndev->netdev_ops = &cpsw_netdev_ops;
  1828. SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
  1829. netif_napi_add(ndev, &priv->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(priv->dev, "error registering net device\n");
  1835. ret = -ENODEV;
  1836. goto clean_irq_ret;
  1837. }
  1838. if (cpts_register(&pdev->dev, priv->cpts,
  1839. data->cpts_clock_mult, data->cpts_clock_shift))
  1840. dev_err(priv->dev, "error registering cpts device\n");
  1841. cpsw_notice(priv, probe, "initialized device (regs %x, irq %d)\n",
  1842. priv->cpsw_res->start, ndev->irq);
  1843. if (priv->data.dual_emac) {
  1844. ret = cpsw_probe_dual_emac(pdev, priv);
  1845. if (ret) {
  1846. cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
  1847. goto clean_irq_ret;
  1848. }
  1849. }
  1850. return 0;
  1851. clean_irq_ret:
  1852. for (i = 0; i < priv->num_irqs; i++)
  1853. free_irq(priv->irqs_table[i], priv);
  1854. clean_ale_ret:
  1855. cpsw_ale_destroy(priv->ale);
  1856. clean_dma_ret:
  1857. cpdma_chan_destroy(priv->txch);
  1858. cpdma_chan_destroy(priv->rxch);
  1859. cpdma_ctlr_destroy(priv->dma);
  1860. clean_wr_iomap_ret:
  1861. iounmap(priv->wr_regs);
  1862. clean_cpsw_wr_iores_ret:
  1863. release_mem_region(priv->cpsw_wr_res->start,
  1864. resource_size(priv->cpsw_wr_res));
  1865. clean_iomap_ret:
  1866. iounmap(priv->regs);
  1867. clean_cpsw_iores_ret:
  1868. release_mem_region(priv->cpsw_res->start,
  1869. resource_size(priv->cpsw_res));
  1870. clean_clk_ret:
  1871. clk_put(priv->clk);
  1872. clean_slave_ret:
  1873. pm_runtime_disable(&pdev->dev);
  1874. kfree(priv->slaves);
  1875. clean_ndev_ret:
  1876. kfree(priv->data.slave_data);
  1877. free_netdev(priv->ndev);
  1878. return ret;
  1879. }
  1880. static int cpsw_remove(struct platform_device *pdev)
  1881. {
  1882. struct net_device *ndev = platform_get_drvdata(pdev);
  1883. struct cpsw_priv *priv = netdev_priv(ndev);
  1884. int i;
  1885. if (priv->data.dual_emac)
  1886. unregister_netdev(cpsw_get_slave_ndev(priv, 1));
  1887. unregister_netdev(ndev);
  1888. cpts_unregister(priv->cpts);
  1889. for (i = 0; i < priv->num_irqs; i++)
  1890. free_irq(priv->irqs_table[i], priv);
  1891. cpsw_ale_destroy(priv->ale);
  1892. cpdma_chan_destroy(priv->txch);
  1893. cpdma_chan_destroy(priv->rxch);
  1894. cpdma_ctlr_destroy(priv->dma);
  1895. iounmap(priv->regs);
  1896. release_mem_region(priv->cpsw_res->start,
  1897. resource_size(priv->cpsw_res));
  1898. iounmap(priv->wr_regs);
  1899. release_mem_region(priv->cpsw_wr_res->start,
  1900. resource_size(priv->cpsw_wr_res));
  1901. pm_runtime_disable(&pdev->dev);
  1902. clk_put(priv->clk);
  1903. kfree(priv->slaves);
  1904. kfree(priv->data.slave_data);
  1905. if (priv->data.dual_emac)
  1906. free_netdev(cpsw_get_slave_ndev(priv, 1));
  1907. free_netdev(ndev);
  1908. return 0;
  1909. }
  1910. static int cpsw_suspend(struct device *dev)
  1911. {
  1912. struct platform_device *pdev = to_platform_device(dev);
  1913. struct net_device *ndev = platform_get_drvdata(pdev);
  1914. struct cpsw_priv *priv = netdev_priv(ndev);
  1915. if (netif_running(ndev))
  1916. cpsw_ndo_stop(ndev);
  1917. soft_reset("sliver 0", &priv->slaves[0].sliver->soft_reset);
  1918. soft_reset("sliver 1", &priv->slaves[1].sliver->soft_reset);
  1919. pm_runtime_put_sync(&pdev->dev);
  1920. /* Select sleep pin state */
  1921. pinctrl_pm_select_sleep_state(&pdev->dev);
  1922. return 0;
  1923. }
  1924. static int cpsw_resume(struct device *dev)
  1925. {
  1926. struct platform_device *pdev = to_platform_device(dev);
  1927. struct net_device *ndev = platform_get_drvdata(pdev);
  1928. pm_runtime_get_sync(&pdev->dev);
  1929. /* Select default pin state */
  1930. pinctrl_pm_select_default_state(&pdev->dev);
  1931. if (netif_running(ndev))
  1932. cpsw_ndo_open(ndev);
  1933. return 0;
  1934. }
  1935. static const struct dev_pm_ops cpsw_pm_ops = {
  1936. .suspend = cpsw_suspend,
  1937. .resume = cpsw_resume,
  1938. };
  1939. static const struct of_device_id cpsw_of_mtable[] = {
  1940. { .compatible = "ti,cpsw", },
  1941. { /* sentinel */ },
  1942. };
  1943. MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
  1944. static struct platform_driver cpsw_driver = {
  1945. .driver = {
  1946. .name = "cpsw",
  1947. .owner = THIS_MODULE,
  1948. .pm = &cpsw_pm_ops,
  1949. .of_match_table = of_match_ptr(cpsw_of_mtable),
  1950. },
  1951. .probe = cpsw_probe,
  1952. .remove = cpsw_remove,
  1953. };
  1954. static int __init cpsw_init(void)
  1955. {
  1956. return platform_driver_register(&cpsw_driver);
  1957. }
  1958. late_initcall(cpsw_init);
  1959. static void __exit cpsw_exit(void)
  1960. {
  1961. platform_driver_unregister(&cpsw_driver);
  1962. }
  1963. module_exit(cpsw_exit);
  1964. MODULE_LICENSE("GPL");
  1965. MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
  1966. MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
  1967. MODULE_DESCRIPTION("TI CPSW Ethernet driver");