cpsw.c 54 KB

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