cpsw.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  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_enable_irq(priv) \
  107. do { \
  108. u32 i; \
  109. for (i = 0; i < priv->num_irqs; i++) \
  110. enable_irq(priv->irqs_table[i]); \
  111. } while (0);
  112. #define cpsw_disable_irq(priv) \
  113. do { \
  114. u32 i; \
  115. for (i = 0; i < priv->num_irqs; i++) \
  116. disable_irq_nosync(priv->irqs_table[i]); \
  117. } while (0);
  118. static int debug_level;
  119. module_param(debug_level, int, 0);
  120. MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
  121. static int ale_ageout = 10;
  122. module_param(ale_ageout, int, 0);
  123. MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
  124. static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
  125. module_param(rx_packet_max, int, 0);
  126. MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
  127. struct cpsw_wr_regs {
  128. u32 id_ver;
  129. u32 soft_reset;
  130. u32 control;
  131. u32 int_control;
  132. u32 rx_thresh_en;
  133. u32 rx_en;
  134. u32 tx_en;
  135. u32 misc_en;
  136. };
  137. struct cpsw_ss_regs {
  138. u32 id_ver;
  139. u32 control;
  140. u32 soft_reset;
  141. u32 stat_port_en;
  142. u32 ptype;
  143. u32 soft_idle;
  144. u32 thru_rate;
  145. u32 gap_thresh;
  146. u32 tx_start_wds;
  147. u32 flow_control;
  148. u32 vlan_ltype;
  149. u32 ts_ltype;
  150. u32 dlr_ltype;
  151. };
  152. /* CPSW_PORT_V1 */
  153. #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
  154. #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
  155. #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
  156. #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
  157. #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
  158. #define CPSW1_TS_CTL 0x14 /* Time Sync Control */
  159. #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
  160. #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
  161. /* CPSW_PORT_V2 */
  162. #define CPSW2_CONTROL 0x00 /* Control Register */
  163. #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
  164. #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
  165. #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
  166. #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
  167. #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
  168. #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
  169. /* CPSW_PORT_V1 and V2 */
  170. #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
  171. #define SA_HI 0x24 /* CPGMAC_SL Source Address High */
  172. #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
  173. /* CPSW_PORT_V2 only */
  174. #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
  175. #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
  176. #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
  177. #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
  178. #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
  179. #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
  180. #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
  181. #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
  182. /* Bit definitions for the CPSW2_CONTROL register */
  183. #define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
  184. #define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
  185. #define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
  186. #define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
  187. #define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
  188. #define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
  189. #define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
  190. #define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
  191. #define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
  192. #define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
  193. #define TS_BIT8 (1<<8) /* ts_ttl_nonzero? */
  194. #define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
  195. #define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
  196. #define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
  197. #define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
  198. #define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
  199. #define CTRL_TS_BITS \
  200. (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 | TS_BIT8 | \
  201. TS_ANNEX_D_EN | TS_LTYPE1_EN)
  202. #define CTRL_ALL_TS_MASK (CTRL_TS_BITS | TS_TX_EN | TS_RX_EN)
  203. #define CTRL_TX_TS_BITS (CTRL_TS_BITS | TS_TX_EN)
  204. #define CTRL_RX_TS_BITS (CTRL_TS_BITS | TS_RX_EN)
  205. /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
  206. #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
  207. #define TS_SEQ_ID_OFFSET_MASK (0x3f)
  208. #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
  209. #define TS_MSG_TYPE_EN_MASK (0xffff)
  210. /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
  211. #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
  212. /* Bit definitions for the CPSW1_TS_CTL register */
  213. #define CPSW_V1_TS_RX_EN BIT(0)
  214. #define CPSW_V1_TS_TX_EN BIT(4)
  215. #define CPSW_V1_MSG_TYPE_OFS 16
  216. /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
  217. #define CPSW_V1_SEQ_ID_OFS_SHIFT 16
  218. struct cpsw_host_regs {
  219. u32 max_blks;
  220. u32 blk_cnt;
  221. u32 flow_thresh;
  222. u32 port_vlan;
  223. u32 tx_pri_map;
  224. u32 cpdma_tx_pri_map;
  225. u32 cpdma_rx_chan_map;
  226. };
  227. struct cpsw_sliver_regs {
  228. u32 id_ver;
  229. u32 mac_control;
  230. u32 mac_status;
  231. u32 soft_reset;
  232. u32 rx_maxlen;
  233. u32 __reserved_0;
  234. u32 rx_pause;
  235. u32 tx_pause;
  236. u32 __reserved_1;
  237. u32 rx_pri_map;
  238. };
  239. struct cpsw_slave {
  240. void __iomem *regs;
  241. struct cpsw_sliver_regs __iomem *sliver;
  242. int slave_num;
  243. u32 mac_control;
  244. struct cpsw_slave_data *data;
  245. struct phy_device *phy;
  246. };
  247. static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
  248. {
  249. return __raw_readl(slave->regs + offset);
  250. }
  251. static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
  252. {
  253. __raw_writel(val, slave->regs + offset);
  254. }
  255. struct cpsw_priv {
  256. spinlock_t lock;
  257. struct platform_device *pdev;
  258. struct net_device *ndev;
  259. struct resource *cpsw_res;
  260. struct resource *cpsw_wr_res;
  261. struct napi_struct napi;
  262. struct device *dev;
  263. struct cpsw_platform_data data;
  264. struct cpsw_ss_regs __iomem *regs;
  265. struct cpsw_wr_regs __iomem *wr_regs;
  266. struct cpsw_host_regs __iomem *host_port_regs;
  267. u32 msg_enable;
  268. u32 version;
  269. struct net_device_stats stats;
  270. int rx_packet_max;
  271. int host_port;
  272. struct clk *clk;
  273. u8 mac_addr[ETH_ALEN];
  274. struct cpsw_slave *slaves;
  275. struct cpdma_ctlr *dma;
  276. struct cpdma_chan *txch, *rxch;
  277. struct cpsw_ale *ale;
  278. /* snapshot of IRQ numbers */
  279. u32 irqs_table[4];
  280. u32 num_irqs;
  281. struct cpts cpts;
  282. };
  283. #define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
  284. #define for_each_slave(priv, func, arg...) \
  285. do { \
  286. int idx; \
  287. for (idx = 0; idx < (priv)->data.slaves; idx++) \
  288. (func)((priv)->slaves + idx, ##arg); \
  289. } while (0)
  290. static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
  291. {
  292. struct cpsw_priv *priv = netdev_priv(ndev);
  293. if (ndev->flags & IFF_PROMISC) {
  294. /* Enable promiscuous mode */
  295. dev_err(priv->dev, "Ignoring Promiscuous mode\n");
  296. return;
  297. }
  298. /* Clear all mcast from ALE */
  299. cpsw_ale_flush_multicast(priv->ale, ALE_ALL_PORTS << priv->host_port);
  300. if (!netdev_mc_empty(ndev)) {
  301. struct netdev_hw_addr *ha;
  302. /* program multicast address list into ALE register */
  303. netdev_for_each_mc_addr(ha, ndev) {
  304. cpsw_ale_add_mcast(priv->ale, (u8 *)ha->addr,
  305. ALE_ALL_PORTS << priv->host_port, 0, 0, 0);
  306. }
  307. }
  308. }
  309. static void cpsw_intr_enable(struct cpsw_priv *priv)
  310. {
  311. __raw_writel(0xFF, &priv->wr_regs->tx_en);
  312. __raw_writel(0xFF, &priv->wr_regs->rx_en);
  313. cpdma_ctlr_int_ctrl(priv->dma, true);
  314. return;
  315. }
  316. static void cpsw_intr_disable(struct cpsw_priv *priv)
  317. {
  318. __raw_writel(0, &priv->wr_regs->tx_en);
  319. __raw_writel(0, &priv->wr_regs->rx_en);
  320. cpdma_ctlr_int_ctrl(priv->dma, false);
  321. return;
  322. }
  323. void cpsw_tx_handler(void *token, int len, int status)
  324. {
  325. struct sk_buff *skb = token;
  326. struct net_device *ndev = skb->dev;
  327. struct cpsw_priv *priv = netdev_priv(ndev);
  328. /* Check whether the queue is stopped due to stalled tx dma, if the
  329. * queue is stopped then start the queue as we have free desc for tx
  330. */
  331. if (unlikely(netif_queue_stopped(ndev)))
  332. netif_start_queue(ndev);
  333. cpts_tx_timestamp(&priv->cpts, skb);
  334. priv->stats.tx_packets++;
  335. priv->stats.tx_bytes += len;
  336. dev_kfree_skb_any(skb);
  337. }
  338. void cpsw_rx_handler(void *token, int len, int status)
  339. {
  340. struct sk_buff *skb = token;
  341. struct net_device *ndev = skb->dev;
  342. struct cpsw_priv *priv = netdev_priv(ndev);
  343. int ret = 0;
  344. /* free and bail if we are shutting down */
  345. if (unlikely(!netif_running(ndev)) ||
  346. unlikely(!netif_carrier_ok(ndev))) {
  347. dev_kfree_skb_any(skb);
  348. return;
  349. }
  350. if (likely(status >= 0)) {
  351. skb_put(skb, len);
  352. cpts_rx_timestamp(&priv->cpts, skb);
  353. skb->protocol = eth_type_trans(skb, ndev);
  354. netif_receive_skb(skb);
  355. priv->stats.rx_bytes += len;
  356. priv->stats.rx_packets++;
  357. skb = NULL;
  358. }
  359. if (unlikely(!netif_running(ndev))) {
  360. if (skb)
  361. dev_kfree_skb_any(skb);
  362. return;
  363. }
  364. if (likely(!skb)) {
  365. skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
  366. if (WARN_ON(!skb))
  367. return;
  368. ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
  369. skb_tailroom(skb), 0, GFP_KERNEL);
  370. }
  371. WARN_ON(ret < 0);
  372. }
  373. static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
  374. {
  375. struct cpsw_priv *priv = dev_id;
  376. if (likely(netif_running(priv->ndev))) {
  377. cpsw_intr_disable(priv);
  378. cpsw_disable_irq(priv);
  379. napi_schedule(&priv->napi);
  380. }
  381. return IRQ_HANDLED;
  382. }
  383. static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
  384. {
  385. if (priv->host_port == 0)
  386. return slave_num + 1;
  387. else
  388. return slave_num;
  389. }
  390. static int cpsw_poll(struct napi_struct *napi, int budget)
  391. {
  392. struct cpsw_priv *priv = napi_to_priv(napi);
  393. int num_tx, num_rx;
  394. num_tx = cpdma_chan_process(priv->txch, 128);
  395. num_rx = cpdma_chan_process(priv->rxch, budget);
  396. if (num_rx || num_tx)
  397. cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n",
  398. num_rx, num_tx);
  399. if (num_rx < budget) {
  400. napi_complete(napi);
  401. cpsw_intr_enable(priv);
  402. cpdma_ctlr_eoi(priv->dma);
  403. cpsw_enable_irq(priv);
  404. }
  405. return num_rx;
  406. }
  407. static inline void soft_reset(const char *module, void __iomem *reg)
  408. {
  409. unsigned long timeout = jiffies + HZ;
  410. __raw_writel(1, reg);
  411. do {
  412. cpu_relax();
  413. } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
  414. WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
  415. }
  416. #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
  417. ((mac)[2] << 16) | ((mac)[3] << 24))
  418. #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
  419. static void cpsw_set_slave_mac(struct cpsw_slave *slave,
  420. struct cpsw_priv *priv)
  421. {
  422. slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
  423. slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
  424. }
  425. static void _cpsw_adjust_link(struct cpsw_slave *slave,
  426. struct cpsw_priv *priv, bool *link)
  427. {
  428. struct phy_device *phy = slave->phy;
  429. u32 mac_control = 0;
  430. u32 slave_port;
  431. if (!phy)
  432. return;
  433. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  434. if (phy->link) {
  435. mac_control = priv->data.mac_control;
  436. /* enable forwarding */
  437. cpsw_ale_control_set(priv->ale, slave_port,
  438. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  439. if (phy->speed == 1000)
  440. mac_control |= BIT(7); /* GIGABITEN */
  441. if (phy->duplex)
  442. mac_control |= BIT(0); /* FULLDUPLEXEN */
  443. /* set speed_in input in case RMII mode is used in 100Mbps */
  444. if (phy->speed == 100)
  445. mac_control |= BIT(15);
  446. *link = true;
  447. } else {
  448. mac_control = 0;
  449. /* disable forwarding */
  450. cpsw_ale_control_set(priv->ale, slave_port,
  451. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  452. }
  453. if (mac_control != slave->mac_control) {
  454. phy_print_status(phy);
  455. __raw_writel(mac_control, &slave->sliver->mac_control);
  456. }
  457. slave->mac_control = mac_control;
  458. }
  459. static void cpsw_adjust_link(struct net_device *ndev)
  460. {
  461. struct cpsw_priv *priv = netdev_priv(ndev);
  462. bool link = false;
  463. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  464. if (link) {
  465. netif_carrier_on(ndev);
  466. if (netif_running(ndev))
  467. netif_wake_queue(ndev);
  468. } else {
  469. netif_carrier_off(ndev);
  470. netif_stop_queue(ndev);
  471. }
  472. }
  473. static inline int __show_stat(char *buf, int maxlen, const char *name, u32 val)
  474. {
  475. static char *leader = "........................................";
  476. if (!val)
  477. return 0;
  478. else
  479. return snprintf(buf, maxlen, "%s %s %10d\n", name,
  480. leader + strlen(name), val);
  481. }
  482. static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
  483. {
  484. char name[32];
  485. u32 slave_port;
  486. sprintf(name, "slave-%d", slave->slave_num);
  487. soft_reset(name, &slave->sliver->soft_reset);
  488. /* setup priority mapping */
  489. __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
  490. switch (priv->version) {
  491. case CPSW_VERSION_1:
  492. slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
  493. break;
  494. case CPSW_VERSION_2:
  495. slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
  496. break;
  497. }
  498. /* setup max packet size, and mac address */
  499. __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
  500. cpsw_set_slave_mac(slave, priv);
  501. slave->mac_control = 0; /* no link yet */
  502. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  503. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  504. 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
  505. slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
  506. &cpsw_adjust_link, slave->data->phy_if);
  507. if (IS_ERR(slave->phy)) {
  508. dev_err(priv->dev, "phy %s not found on slave %d\n",
  509. slave->data->phy_id, slave->slave_num);
  510. slave->phy = NULL;
  511. } else {
  512. dev_info(priv->dev, "phy found : id is : 0x%x\n",
  513. slave->phy->phy_id);
  514. phy_start(slave->phy);
  515. }
  516. }
  517. static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
  518. {
  519. const int vlan = priv->data.default_vlan;
  520. const int port = priv->host_port;
  521. u32 reg;
  522. int i;
  523. reg = (priv->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
  524. CPSW2_PORT_VLAN;
  525. writel(vlan, &priv->host_port_regs->port_vlan);
  526. for (i = 0; i < 2; i++)
  527. slave_write(priv->slaves + i, vlan, reg);
  528. cpsw_ale_add_vlan(priv->ale, vlan, ALE_ALL_PORTS << port,
  529. ALE_ALL_PORTS << port, ALE_ALL_PORTS << port,
  530. (ALE_PORT_1 | ALE_PORT_2) << port);
  531. }
  532. static void cpsw_init_host_port(struct cpsw_priv *priv)
  533. {
  534. u32 control_reg;
  535. /* soft reset the controller and initialize ale */
  536. soft_reset("cpsw", &priv->regs->soft_reset);
  537. cpsw_ale_start(priv->ale);
  538. /* switch to vlan unaware mode */
  539. cpsw_ale_control_set(priv->ale, priv->host_port, ALE_VLAN_AWARE,
  540. CPSW_ALE_VLAN_AWARE);
  541. control_reg = readl(&priv->regs->control);
  542. control_reg |= CPSW_VLAN_AWARE;
  543. writel(control_reg, &priv->regs->control);
  544. /* setup host port priority mapping */
  545. __raw_writel(CPDMA_TX_PRIORITY_MAP,
  546. &priv->host_port_regs->cpdma_tx_pri_map);
  547. __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
  548. cpsw_ale_control_set(priv->ale, priv->host_port,
  549. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  550. cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port, 0, 0);
  551. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  552. 1 << priv->host_port, 0, 0, ALE_MCAST_FWD_2);
  553. }
  554. static int cpsw_ndo_open(struct net_device *ndev)
  555. {
  556. struct cpsw_priv *priv = netdev_priv(ndev);
  557. int i, ret;
  558. u32 reg;
  559. cpsw_intr_disable(priv);
  560. netif_carrier_off(ndev);
  561. pm_runtime_get_sync(&priv->pdev->dev);
  562. reg = priv->version;
  563. dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
  564. CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
  565. CPSW_RTL_VERSION(reg));
  566. /* initialize host and slave ports */
  567. cpsw_init_host_port(priv);
  568. for_each_slave(priv, cpsw_slave_open, priv);
  569. /* Add default VLAN */
  570. cpsw_add_default_vlan(priv);
  571. /* setup tx dma to fixed prio and zero offset */
  572. cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
  573. cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
  574. /* disable priority elevation and enable statistics on all ports */
  575. __raw_writel(0, &priv->regs->ptype);
  576. /* enable statistics collection only on the host port */
  577. __raw_writel(0x7, &priv->regs->stat_port_en);
  578. if (WARN_ON(!priv->data.rx_descs))
  579. priv->data.rx_descs = 128;
  580. for (i = 0; i < priv->data.rx_descs; i++) {
  581. struct sk_buff *skb;
  582. ret = -ENOMEM;
  583. skb = netdev_alloc_skb_ip_align(priv->ndev,
  584. priv->rx_packet_max);
  585. if (!skb)
  586. break;
  587. ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
  588. skb_tailroom(skb), 0, GFP_KERNEL);
  589. if (WARN_ON(ret < 0))
  590. break;
  591. }
  592. /* continue even if we didn't manage to submit all receive descs */
  593. cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
  594. cpdma_ctlr_start(priv->dma);
  595. cpsw_intr_enable(priv);
  596. napi_enable(&priv->napi);
  597. cpdma_ctlr_eoi(priv->dma);
  598. return 0;
  599. }
  600. static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
  601. {
  602. if (!slave->phy)
  603. return;
  604. phy_stop(slave->phy);
  605. phy_disconnect(slave->phy);
  606. slave->phy = NULL;
  607. }
  608. static int cpsw_ndo_stop(struct net_device *ndev)
  609. {
  610. struct cpsw_priv *priv = netdev_priv(ndev);
  611. cpsw_info(priv, ifdown, "shutting down cpsw device\n");
  612. netif_stop_queue(priv->ndev);
  613. napi_disable(&priv->napi);
  614. netif_carrier_off(priv->ndev);
  615. cpsw_intr_disable(priv);
  616. cpdma_ctlr_int_ctrl(priv->dma, false);
  617. cpdma_ctlr_stop(priv->dma);
  618. cpsw_ale_stop(priv->ale);
  619. for_each_slave(priv, cpsw_slave_stop, priv);
  620. pm_runtime_put_sync(&priv->pdev->dev);
  621. return 0;
  622. }
  623. static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
  624. struct net_device *ndev)
  625. {
  626. struct cpsw_priv *priv = netdev_priv(ndev);
  627. int ret;
  628. ndev->trans_start = jiffies;
  629. if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
  630. cpsw_err(priv, tx_err, "packet pad failed\n");
  631. priv->stats.tx_dropped++;
  632. return NETDEV_TX_OK;
  633. }
  634. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && priv->cpts.tx_enable)
  635. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  636. skb_tx_timestamp(skb);
  637. ret = cpdma_chan_submit(priv->txch, skb, skb->data,
  638. skb->len, 0, GFP_KERNEL);
  639. if (unlikely(ret != 0)) {
  640. cpsw_err(priv, tx_err, "desc submit failed\n");
  641. goto fail;
  642. }
  643. /* If there is no more tx desc left free then we need to
  644. * tell the kernel to stop sending us tx frames.
  645. */
  646. if (unlikely(cpdma_check_free_tx_desc(priv->txch)))
  647. netif_stop_queue(ndev);
  648. return NETDEV_TX_OK;
  649. fail:
  650. priv->stats.tx_dropped++;
  651. netif_stop_queue(ndev);
  652. return NETDEV_TX_BUSY;
  653. }
  654. static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
  655. {
  656. /*
  657. * The switch cannot operate in promiscuous mode without substantial
  658. * headache. For promiscuous mode to work, we would need to put the
  659. * ALE in bypass mode and route all traffic to the host port.
  660. * Subsequently, the host will need to operate as a "bridge", learn,
  661. * and flood as needed. For now, we simply complain here and
  662. * do nothing about it :-)
  663. */
  664. if ((flags & IFF_PROMISC) && (ndev->flags & IFF_PROMISC))
  665. dev_err(&ndev->dev, "promiscuity ignored!\n");
  666. /*
  667. * The switch cannot filter multicast traffic unless it is configured
  668. * in "VLAN Aware" mode. Unfortunately, VLAN awareness requires a
  669. * whole bunch of additional logic that this driver does not implement
  670. * at present.
  671. */
  672. if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
  673. dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
  674. }
  675. #ifdef CONFIG_TI_CPTS
  676. static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
  677. {
  678. struct cpsw_slave *slave = &priv->slaves[priv->data.cpts_active_slave];
  679. u32 ts_en, seq_id;
  680. if (!priv->cpts.tx_enable && !priv->cpts.rx_enable) {
  681. slave_write(slave, 0, CPSW1_TS_CTL);
  682. return;
  683. }
  684. seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
  685. ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
  686. if (priv->cpts.tx_enable)
  687. ts_en |= CPSW_V1_TS_TX_EN;
  688. if (priv->cpts.rx_enable)
  689. ts_en |= CPSW_V1_TS_RX_EN;
  690. slave_write(slave, ts_en, CPSW1_TS_CTL);
  691. slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
  692. }
  693. static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
  694. {
  695. struct cpsw_slave *slave = &priv->slaves[priv->data.cpts_active_slave];
  696. u32 ctrl, mtype;
  697. ctrl = slave_read(slave, CPSW2_CONTROL);
  698. ctrl &= ~CTRL_ALL_TS_MASK;
  699. if (priv->cpts.tx_enable)
  700. ctrl |= CTRL_TX_TS_BITS;
  701. if (priv->cpts.rx_enable)
  702. ctrl |= CTRL_RX_TS_BITS;
  703. mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
  704. slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
  705. slave_write(slave, ctrl, CPSW2_CONTROL);
  706. __raw_writel(ETH_P_1588, &priv->regs->ts_ltype);
  707. }
  708. static int cpsw_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
  709. {
  710. struct cpsw_priv *priv = netdev_priv(dev);
  711. struct cpts *cpts = &priv->cpts;
  712. struct hwtstamp_config cfg;
  713. if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
  714. return -EFAULT;
  715. /* reserved for future extensions */
  716. if (cfg.flags)
  717. return -EINVAL;
  718. switch (cfg.tx_type) {
  719. case HWTSTAMP_TX_OFF:
  720. cpts->tx_enable = 0;
  721. break;
  722. case HWTSTAMP_TX_ON:
  723. cpts->tx_enable = 1;
  724. break;
  725. default:
  726. return -ERANGE;
  727. }
  728. switch (cfg.rx_filter) {
  729. case HWTSTAMP_FILTER_NONE:
  730. cpts->rx_enable = 0;
  731. break;
  732. case HWTSTAMP_FILTER_ALL:
  733. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  734. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  735. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  736. return -ERANGE;
  737. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  738. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  739. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  740. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  741. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  742. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  743. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  744. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  745. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  746. cpts->rx_enable = 1;
  747. cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  748. break;
  749. default:
  750. return -ERANGE;
  751. }
  752. switch (priv->version) {
  753. case CPSW_VERSION_1:
  754. cpsw_hwtstamp_v1(priv);
  755. break;
  756. case CPSW_VERSION_2:
  757. cpsw_hwtstamp_v2(priv);
  758. break;
  759. default:
  760. return -ENOTSUPP;
  761. }
  762. return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
  763. }
  764. #endif /*CONFIG_TI_CPTS*/
  765. static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  766. {
  767. if (!netif_running(dev))
  768. return -EINVAL;
  769. #ifdef CONFIG_TI_CPTS
  770. if (cmd == SIOCSHWTSTAMP)
  771. return cpsw_hwtstamp_ioctl(dev, req);
  772. #endif
  773. return -ENOTSUPP;
  774. }
  775. static void cpsw_ndo_tx_timeout(struct net_device *ndev)
  776. {
  777. struct cpsw_priv *priv = netdev_priv(ndev);
  778. cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
  779. priv->stats.tx_errors++;
  780. cpsw_intr_disable(priv);
  781. cpdma_ctlr_int_ctrl(priv->dma, false);
  782. cpdma_chan_stop(priv->txch);
  783. cpdma_chan_start(priv->txch);
  784. cpdma_ctlr_int_ctrl(priv->dma, true);
  785. cpsw_intr_enable(priv);
  786. cpdma_ctlr_eoi(priv->dma);
  787. }
  788. static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev)
  789. {
  790. struct cpsw_priv *priv = netdev_priv(ndev);
  791. return &priv->stats;
  792. }
  793. #ifdef CONFIG_NET_POLL_CONTROLLER
  794. static void cpsw_ndo_poll_controller(struct net_device *ndev)
  795. {
  796. struct cpsw_priv *priv = netdev_priv(ndev);
  797. cpsw_intr_disable(priv);
  798. cpdma_ctlr_int_ctrl(priv->dma, false);
  799. cpsw_interrupt(ndev->irq, priv);
  800. cpdma_ctlr_int_ctrl(priv->dma, true);
  801. cpsw_intr_enable(priv);
  802. cpdma_ctlr_eoi(priv->dma);
  803. }
  804. #endif
  805. static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
  806. unsigned short vid)
  807. {
  808. int ret;
  809. ret = cpsw_ale_add_vlan(priv->ale, vid,
  810. ALE_ALL_PORTS << priv->host_port,
  811. 0, ALE_ALL_PORTS << priv->host_port,
  812. (ALE_PORT_1 | ALE_PORT_2) << priv->host_port);
  813. if (ret != 0)
  814. return ret;
  815. ret = cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
  816. priv->host_port, ALE_VLAN, vid);
  817. if (ret != 0)
  818. goto clean_vid;
  819. ret = cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  820. ALE_ALL_PORTS << priv->host_port,
  821. ALE_VLAN, vid, 0);
  822. if (ret != 0)
  823. goto clean_vlan_ucast;
  824. return 0;
  825. clean_vlan_ucast:
  826. cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
  827. priv->host_port, ALE_VLAN, vid);
  828. clean_vid:
  829. cpsw_ale_del_vlan(priv->ale, vid, 0);
  830. return ret;
  831. }
  832. static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
  833. unsigned short vid)
  834. {
  835. struct cpsw_priv *priv = netdev_priv(ndev);
  836. if (vid == priv->data.default_vlan)
  837. return 0;
  838. dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
  839. return cpsw_add_vlan_ale_entry(priv, vid);
  840. }
  841. static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
  842. unsigned short vid)
  843. {
  844. struct cpsw_priv *priv = netdev_priv(ndev);
  845. int ret;
  846. if (vid == priv->data.default_vlan)
  847. return 0;
  848. dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
  849. ret = cpsw_ale_del_vlan(priv->ale, vid, 0);
  850. if (ret != 0)
  851. return ret;
  852. ret = cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
  853. priv->host_port, ALE_VLAN, vid);
  854. if (ret != 0)
  855. return ret;
  856. return cpsw_ale_del_mcast(priv->ale, priv->ndev->broadcast,
  857. 0, ALE_VLAN, vid);
  858. }
  859. static const struct net_device_ops cpsw_netdev_ops = {
  860. .ndo_open = cpsw_ndo_open,
  861. .ndo_stop = cpsw_ndo_stop,
  862. .ndo_start_xmit = cpsw_ndo_start_xmit,
  863. .ndo_change_rx_flags = cpsw_ndo_change_rx_flags,
  864. .ndo_do_ioctl = cpsw_ndo_ioctl,
  865. .ndo_validate_addr = eth_validate_addr,
  866. .ndo_change_mtu = eth_change_mtu,
  867. .ndo_tx_timeout = cpsw_ndo_tx_timeout,
  868. .ndo_get_stats = cpsw_ndo_get_stats,
  869. .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
  870. #ifdef CONFIG_NET_POLL_CONTROLLER
  871. .ndo_poll_controller = cpsw_ndo_poll_controller,
  872. #endif
  873. .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
  874. .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
  875. };
  876. static void cpsw_get_drvinfo(struct net_device *ndev,
  877. struct ethtool_drvinfo *info)
  878. {
  879. struct cpsw_priv *priv = netdev_priv(ndev);
  880. strlcpy(info->driver, "TI CPSW Driver v1.0", sizeof(info->driver));
  881. strlcpy(info->version, "1.0", sizeof(info->version));
  882. strlcpy(info->bus_info, priv->pdev->name, sizeof(info->bus_info));
  883. }
  884. static u32 cpsw_get_msglevel(struct net_device *ndev)
  885. {
  886. struct cpsw_priv *priv = netdev_priv(ndev);
  887. return priv->msg_enable;
  888. }
  889. static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
  890. {
  891. struct cpsw_priv *priv = netdev_priv(ndev);
  892. priv->msg_enable = value;
  893. }
  894. static int cpsw_get_ts_info(struct net_device *ndev,
  895. struct ethtool_ts_info *info)
  896. {
  897. #ifdef CONFIG_TI_CPTS
  898. struct cpsw_priv *priv = netdev_priv(ndev);
  899. info->so_timestamping =
  900. SOF_TIMESTAMPING_TX_HARDWARE |
  901. SOF_TIMESTAMPING_TX_SOFTWARE |
  902. SOF_TIMESTAMPING_RX_HARDWARE |
  903. SOF_TIMESTAMPING_RX_SOFTWARE |
  904. SOF_TIMESTAMPING_SOFTWARE |
  905. SOF_TIMESTAMPING_RAW_HARDWARE;
  906. info->phc_index = priv->cpts.phc_index;
  907. info->tx_types =
  908. (1 << HWTSTAMP_TX_OFF) |
  909. (1 << HWTSTAMP_TX_ON);
  910. info->rx_filters =
  911. (1 << HWTSTAMP_FILTER_NONE) |
  912. (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
  913. #else
  914. info->so_timestamping =
  915. SOF_TIMESTAMPING_TX_SOFTWARE |
  916. SOF_TIMESTAMPING_RX_SOFTWARE |
  917. SOF_TIMESTAMPING_SOFTWARE;
  918. info->phc_index = -1;
  919. info->tx_types = 0;
  920. info->rx_filters = 0;
  921. #endif
  922. return 0;
  923. }
  924. static const struct ethtool_ops cpsw_ethtool_ops = {
  925. .get_drvinfo = cpsw_get_drvinfo,
  926. .get_msglevel = cpsw_get_msglevel,
  927. .set_msglevel = cpsw_set_msglevel,
  928. .get_link = ethtool_op_get_link,
  929. .get_ts_info = cpsw_get_ts_info,
  930. };
  931. static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv,
  932. u32 slave_reg_ofs, u32 sliver_reg_ofs)
  933. {
  934. void __iomem *regs = priv->regs;
  935. int slave_num = slave->slave_num;
  936. struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
  937. slave->data = data;
  938. slave->regs = regs + slave_reg_ofs;
  939. slave->sliver = regs + sliver_reg_ofs;
  940. }
  941. static int cpsw_probe_dt(struct cpsw_platform_data *data,
  942. struct platform_device *pdev)
  943. {
  944. struct device_node *node = pdev->dev.of_node;
  945. struct device_node *slave_node;
  946. int i = 0, ret;
  947. u32 prop;
  948. if (!node)
  949. return -EINVAL;
  950. if (of_property_read_u32(node, "slaves", &prop)) {
  951. pr_err("Missing slaves property in the DT.\n");
  952. return -EINVAL;
  953. }
  954. data->slaves = prop;
  955. if (of_property_read_u32(node, "cpts_active_slave", &prop)) {
  956. pr_err("Missing cpts_active_slave property in the DT.\n");
  957. ret = -EINVAL;
  958. goto error_ret;
  959. }
  960. data->cpts_active_slave = prop;
  961. if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
  962. pr_err("Missing cpts_clock_mult property in the DT.\n");
  963. ret = -EINVAL;
  964. goto error_ret;
  965. }
  966. data->cpts_clock_mult = prop;
  967. if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
  968. pr_err("Missing cpts_clock_shift property in the DT.\n");
  969. ret = -EINVAL;
  970. goto error_ret;
  971. }
  972. data->cpts_clock_shift = prop;
  973. data->slave_data = kcalloc(data->slaves, sizeof(struct cpsw_slave_data),
  974. GFP_KERNEL);
  975. if (!data->slave_data)
  976. return -EINVAL;
  977. if (of_property_read_u32(node, "cpdma_channels", &prop)) {
  978. pr_err("Missing cpdma_channels property in the DT.\n");
  979. ret = -EINVAL;
  980. goto error_ret;
  981. }
  982. data->channels = prop;
  983. if (of_property_read_u32(node, "ale_entries", &prop)) {
  984. pr_err("Missing ale_entries property in the DT.\n");
  985. ret = -EINVAL;
  986. goto error_ret;
  987. }
  988. data->ale_entries = prop;
  989. if (of_property_read_u32(node, "bd_ram_size", &prop)) {
  990. pr_err("Missing bd_ram_size property in the DT.\n");
  991. ret = -EINVAL;
  992. goto error_ret;
  993. }
  994. data->bd_ram_size = prop;
  995. if (of_property_read_u32(node, "rx_descs", &prop)) {
  996. pr_err("Missing rx_descs property in the DT.\n");
  997. ret = -EINVAL;
  998. goto error_ret;
  999. }
  1000. data->rx_descs = prop;
  1001. if (of_property_read_u32(node, "mac_control", &prop)) {
  1002. pr_err("Missing mac_control property in the DT.\n");
  1003. ret = -EINVAL;
  1004. goto error_ret;
  1005. }
  1006. data->mac_control = prop;
  1007. /*
  1008. * Populate all the child nodes here...
  1009. */
  1010. ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
  1011. /* We do not want to force this, as in some cases may not have child */
  1012. if (ret)
  1013. pr_warn("Doesn't have any child node\n");
  1014. for_each_node_by_name(slave_node, "slave") {
  1015. struct cpsw_slave_data *slave_data = data->slave_data + i;
  1016. const void *mac_addr = NULL;
  1017. u32 phyid;
  1018. int lenp;
  1019. const __be32 *parp;
  1020. struct device_node *mdio_node;
  1021. struct platform_device *mdio;
  1022. parp = of_get_property(slave_node, "phy_id", &lenp);
  1023. if ((parp == NULL) && (lenp != (sizeof(void *) * 2))) {
  1024. pr_err("Missing slave[%d] phy_id property\n", i);
  1025. ret = -EINVAL;
  1026. goto error_ret;
  1027. }
  1028. mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
  1029. phyid = be32_to_cpup(parp+1);
  1030. mdio = of_find_device_by_node(mdio_node);
  1031. snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
  1032. PHY_ID_FMT, mdio->name, phyid);
  1033. mac_addr = of_get_mac_address(slave_node);
  1034. if (mac_addr)
  1035. memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
  1036. i++;
  1037. }
  1038. return 0;
  1039. error_ret:
  1040. kfree(data->slave_data);
  1041. return ret;
  1042. }
  1043. static int cpsw_probe(struct platform_device *pdev)
  1044. {
  1045. struct cpsw_platform_data *data = pdev->dev.platform_data;
  1046. struct net_device *ndev;
  1047. struct cpsw_priv *priv;
  1048. struct cpdma_params dma_params;
  1049. struct cpsw_ale_params ale_params;
  1050. void __iomem *ss_regs, *wr_regs;
  1051. struct resource *res;
  1052. u32 slave_offset, sliver_offset, slave_size;
  1053. int ret = 0, i, k = 0;
  1054. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  1055. if (!ndev) {
  1056. pr_err("error allocating net_device\n");
  1057. return -ENOMEM;
  1058. }
  1059. platform_set_drvdata(pdev, ndev);
  1060. priv = netdev_priv(ndev);
  1061. spin_lock_init(&priv->lock);
  1062. priv->pdev = pdev;
  1063. priv->ndev = ndev;
  1064. priv->dev = &ndev->dev;
  1065. priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  1066. priv->rx_packet_max = max(rx_packet_max, 128);
  1067. /*
  1068. * This may be required here for child devices.
  1069. */
  1070. pm_runtime_enable(&pdev->dev);
  1071. if (cpsw_probe_dt(&priv->data, pdev)) {
  1072. pr_err("cpsw: platform data missing\n");
  1073. ret = -ENODEV;
  1074. goto clean_ndev_ret;
  1075. }
  1076. data = &priv->data;
  1077. if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
  1078. memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
  1079. pr_info("Detected MACID = %pM", priv->mac_addr);
  1080. } else {
  1081. eth_random_addr(priv->mac_addr);
  1082. pr_info("Random MACID = %pM", priv->mac_addr);
  1083. }
  1084. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  1085. priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves,
  1086. GFP_KERNEL);
  1087. if (!priv->slaves) {
  1088. ret = -EBUSY;
  1089. goto clean_ndev_ret;
  1090. }
  1091. for (i = 0; i < data->slaves; i++)
  1092. priv->slaves[i].slave_num = i;
  1093. priv->clk = clk_get(&pdev->dev, "fck");
  1094. if (IS_ERR(priv->clk)) {
  1095. dev_err(&pdev->dev, "fck is not found\n");
  1096. ret = -ENODEV;
  1097. goto clean_slave_ret;
  1098. }
  1099. priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1100. if (!priv->cpsw_res) {
  1101. dev_err(priv->dev, "error getting i/o resource\n");
  1102. ret = -ENOENT;
  1103. goto clean_clk_ret;
  1104. }
  1105. if (!request_mem_region(priv->cpsw_res->start,
  1106. resource_size(priv->cpsw_res), ndev->name)) {
  1107. dev_err(priv->dev, "failed request i/o region\n");
  1108. ret = -ENXIO;
  1109. goto clean_clk_ret;
  1110. }
  1111. ss_regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res));
  1112. if (!ss_regs) {
  1113. dev_err(priv->dev, "unable to map i/o region\n");
  1114. goto clean_cpsw_iores_ret;
  1115. }
  1116. priv->regs = ss_regs;
  1117. priv->version = __raw_readl(&priv->regs->id_ver);
  1118. priv->host_port = HOST_PORT_NUM;
  1119. priv->cpsw_wr_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1120. if (!priv->cpsw_wr_res) {
  1121. dev_err(priv->dev, "error getting i/o resource\n");
  1122. ret = -ENOENT;
  1123. goto clean_iomap_ret;
  1124. }
  1125. if (!request_mem_region(priv->cpsw_wr_res->start,
  1126. resource_size(priv->cpsw_wr_res), ndev->name)) {
  1127. dev_err(priv->dev, "failed request i/o region\n");
  1128. ret = -ENXIO;
  1129. goto clean_iomap_ret;
  1130. }
  1131. wr_regs = ioremap(priv->cpsw_wr_res->start,
  1132. resource_size(priv->cpsw_wr_res));
  1133. if (!wr_regs) {
  1134. dev_err(priv->dev, "unable to map i/o region\n");
  1135. goto clean_cpsw_wr_iores_ret;
  1136. }
  1137. priv->wr_regs = wr_regs;
  1138. memset(&dma_params, 0, sizeof(dma_params));
  1139. memset(&ale_params, 0, sizeof(ale_params));
  1140. switch (priv->version) {
  1141. case CPSW_VERSION_1:
  1142. priv->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
  1143. priv->cpts.reg = ss_regs + CPSW1_CPTS_OFFSET;
  1144. dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
  1145. dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
  1146. ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
  1147. slave_offset = CPSW1_SLAVE_OFFSET;
  1148. slave_size = CPSW1_SLAVE_SIZE;
  1149. sliver_offset = CPSW1_SLIVER_OFFSET;
  1150. dma_params.desc_mem_phys = 0;
  1151. break;
  1152. case CPSW_VERSION_2:
  1153. priv->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
  1154. priv->cpts.reg = ss_regs + CPSW2_CPTS_OFFSET;
  1155. dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
  1156. dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
  1157. ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
  1158. slave_offset = CPSW2_SLAVE_OFFSET;
  1159. slave_size = CPSW2_SLAVE_SIZE;
  1160. sliver_offset = CPSW2_SLIVER_OFFSET;
  1161. dma_params.desc_mem_phys =
  1162. (u32 __force) priv->cpsw_res->start + CPSW2_BD_OFFSET;
  1163. break;
  1164. default:
  1165. dev_err(priv->dev, "unknown version 0x%08x\n", priv->version);
  1166. ret = -ENODEV;
  1167. goto clean_cpsw_wr_iores_ret;
  1168. }
  1169. for (i = 0; i < priv->data.slaves; i++) {
  1170. struct cpsw_slave *slave = &priv->slaves[i];
  1171. cpsw_slave_init(slave, priv, slave_offset, sliver_offset);
  1172. slave_offset += slave_size;
  1173. sliver_offset += SLIVER_SIZE;
  1174. }
  1175. dma_params.dev = &pdev->dev;
  1176. dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
  1177. dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
  1178. dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
  1179. dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
  1180. dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
  1181. dma_params.num_chan = data->channels;
  1182. dma_params.has_soft_reset = true;
  1183. dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
  1184. dma_params.desc_mem_size = data->bd_ram_size;
  1185. dma_params.desc_align = 16;
  1186. dma_params.has_ext_regs = true;
  1187. dma_params.desc_hw_addr = dma_params.desc_mem_phys;
  1188. priv->dma = cpdma_ctlr_create(&dma_params);
  1189. if (!priv->dma) {
  1190. dev_err(priv->dev, "error initializing dma\n");
  1191. ret = -ENOMEM;
  1192. goto clean_wr_iomap_ret;
  1193. }
  1194. priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
  1195. cpsw_tx_handler);
  1196. priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
  1197. cpsw_rx_handler);
  1198. if (WARN_ON(!priv->txch || !priv->rxch)) {
  1199. dev_err(priv->dev, "error initializing dma channels\n");
  1200. ret = -ENOMEM;
  1201. goto clean_dma_ret;
  1202. }
  1203. ale_params.dev = &ndev->dev;
  1204. ale_params.ale_ageout = ale_ageout;
  1205. ale_params.ale_entries = data->ale_entries;
  1206. ale_params.ale_ports = data->slaves;
  1207. priv->ale = cpsw_ale_create(&ale_params);
  1208. if (!priv->ale) {
  1209. dev_err(priv->dev, "error initializing ale engine\n");
  1210. ret = -ENODEV;
  1211. goto clean_dma_ret;
  1212. }
  1213. ndev->irq = platform_get_irq(pdev, 0);
  1214. if (ndev->irq < 0) {
  1215. dev_err(priv->dev, "error getting irq resource\n");
  1216. ret = -ENOENT;
  1217. goto clean_ale_ret;
  1218. }
  1219. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
  1220. for (i = res->start; i <= res->end; i++) {
  1221. if (request_irq(i, cpsw_interrupt, IRQF_DISABLED,
  1222. dev_name(&pdev->dev), priv)) {
  1223. dev_err(priv->dev, "error attaching irq\n");
  1224. goto clean_ale_ret;
  1225. }
  1226. priv->irqs_table[k] = i;
  1227. priv->num_irqs = k;
  1228. }
  1229. k++;
  1230. }
  1231. ndev->features |= NETIF_F_HW_VLAN_FILTER;
  1232. ndev->netdev_ops = &cpsw_netdev_ops;
  1233. SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
  1234. netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
  1235. /* register the network device */
  1236. SET_NETDEV_DEV(ndev, &pdev->dev);
  1237. ret = register_netdev(ndev);
  1238. if (ret) {
  1239. dev_err(priv->dev, "error registering net device\n");
  1240. ret = -ENODEV;
  1241. goto clean_irq_ret;
  1242. }
  1243. if (cpts_register(&pdev->dev, &priv->cpts,
  1244. data->cpts_clock_mult, data->cpts_clock_shift))
  1245. dev_err(priv->dev, "error registering cpts device\n");
  1246. cpsw_notice(priv, probe, "initialized device (regs %x, irq %d)\n",
  1247. priv->cpsw_res->start, ndev->irq);
  1248. return 0;
  1249. clean_irq_ret:
  1250. free_irq(ndev->irq, priv);
  1251. clean_ale_ret:
  1252. cpsw_ale_destroy(priv->ale);
  1253. clean_dma_ret:
  1254. cpdma_chan_destroy(priv->txch);
  1255. cpdma_chan_destroy(priv->rxch);
  1256. cpdma_ctlr_destroy(priv->dma);
  1257. clean_wr_iomap_ret:
  1258. iounmap(priv->wr_regs);
  1259. clean_cpsw_wr_iores_ret:
  1260. release_mem_region(priv->cpsw_wr_res->start,
  1261. resource_size(priv->cpsw_wr_res));
  1262. clean_iomap_ret:
  1263. iounmap(priv->regs);
  1264. clean_cpsw_iores_ret:
  1265. release_mem_region(priv->cpsw_res->start,
  1266. resource_size(priv->cpsw_res));
  1267. clean_clk_ret:
  1268. clk_put(priv->clk);
  1269. clean_slave_ret:
  1270. pm_runtime_disable(&pdev->dev);
  1271. kfree(priv->slaves);
  1272. clean_ndev_ret:
  1273. free_netdev(ndev);
  1274. return ret;
  1275. }
  1276. static int cpsw_remove(struct platform_device *pdev)
  1277. {
  1278. struct net_device *ndev = platform_get_drvdata(pdev);
  1279. struct cpsw_priv *priv = netdev_priv(ndev);
  1280. pr_info("removing device");
  1281. platform_set_drvdata(pdev, NULL);
  1282. cpts_unregister(&priv->cpts);
  1283. free_irq(ndev->irq, priv);
  1284. cpsw_ale_destroy(priv->ale);
  1285. cpdma_chan_destroy(priv->txch);
  1286. cpdma_chan_destroy(priv->rxch);
  1287. cpdma_ctlr_destroy(priv->dma);
  1288. iounmap(priv->regs);
  1289. release_mem_region(priv->cpsw_res->start,
  1290. resource_size(priv->cpsw_res));
  1291. iounmap(priv->wr_regs);
  1292. release_mem_region(priv->cpsw_wr_res->start,
  1293. resource_size(priv->cpsw_wr_res));
  1294. pm_runtime_disable(&pdev->dev);
  1295. clk_put(priv->clk);
  1296. kfree(priv->slaves);
  1297. free_netdev(ndev);
  1298. return 0;
  1299. }
  1300. static int cpsw_suspend(struct device *dev)
  1301. {
  1302. struct platform_device *pdev = to_platform_device(dev);
  1303. struct net_device *ndev = platform_get_drvdata(pdev);
  1304. if (netif_running(ndev))
  1305. cpsw_ndo_stop(ndev);
  1306. pm_runtime_put_sync(&pdev->dev);
  1307. return 0;
  1308. }
  1309. static int cpsw_resume(struct device *dev)
  1310. {
  1311. struct platform_device *pdev = to_platform_device(dev);
  1312. struct net_device *ndev = platform_get_drvdata(pdev);
  1313. pm_runtime_get_sync(&pdev->dev);
  1314. if (netif_running(ndev))
  1315. cpsw_ndo_open(ndev);
  1316. return 0;
  1317. }
  1318. static const struct dev_pm_ops cpsw_pm_ops = {
  1319. .suspend = cpsw_suspend,
  1320. .resume = cpsw_resume,
  1321. };
  1322. static const struct of_device_id cpsw_of_mtable[] = {
  1323. { .compatible = "ti,cpsw", },
  1324. { /* sentinel */ },
  1325. };
  1326. static struct platform_driver cpsw_driver = {
  1327. .driver = {
  1328. .name = "cpsw",
  1329. .owner = THIS_MODULE,
  1330. .pm = &cpsw_pm_ops,
  1331. .of_match_table = of_match_ptr(cpsw_of_mtable),
  1332. },
  1333. .probe = cpsw_probe,
  1334. .remove = cpsw_remove,
  1335. };
  1336. static int __init cpsw_init(void)
  1337. {
  1338. return platform_driver_register(&cpsw_driver);
  1339. }
  1340. late_initcall(cpsw_init);
  1341. static void __exit cpsw_exit(void)
  1342. {
  1343. platform_driver_unregister(&cpsw_driver);
  1344. }
  1345. module_exit(cpsw_exit);
  1346. MODULE_LICENSE("GPL");
  1347. MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
  1348. MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
  1349. MODULE_DESCRIPTION("TI CPSW Ethernet driver");