cpsw.c 40 KB

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