xilinx_emaclite.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Xilinx EmacLite Linux driver for the Xilinx Ethernet MAC Lite device.
  4. *
  5. * This is a new flat driver which is based on the original emac_lite
  6. * driver from John Williams <john.williams@xilinx.com>.
  7. *
  8. * 2007 - 2013 (c) Xilinx, Inc.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/ethtool.h>
  16. #include <linux/io.h>
  17. #include <linux/slab.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/of_mdio.h>
  22. #include <linux/of_net.h>
  23. #include <linux/phy.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/iopoll.h>
  26. #define DRIVER_NAME "xilinx_emaclite"
  27. /* Register offsets for the EmacLite Core */
  28. #define XEL_TXBUFF_OFFSET 0x0 /* Transmit Buffer */
  29. #define XEL_MDIOADDR_OFFSET 0x07E4 /* MDIO Address Register */
  30. #define XEL_MDIOWR_OFFSET 0x07E8 /* MDIO Write Data Register */
  31. #define XEL_MDIORD_OFFSET 0x07EC /* MDIO Read Data Register */
  32. #define XEL_MDIOCTRL_OFFSET 0x07F0 /* MDIO Control Register */
  33. #define XEL_GIER_OFFSET 0x07F8 /* GIE Register */
  34. #define XEL_TSR_OFFSET 0x07FC /* Tx status */
  35. #define XEL_TPLR_OFFSET 0x07F4 /* Tx packet length */
  36. #define XEL_RXBUFF_OFFSET 0x1000 /* Receive Buffer */
  37. #define XEL_RPLR_OFFSET 0x100C /* Rx packet length */
  38. #define XEL_RSR_OFFSET 0x17FC /* Rx status */
  39. #define XEL_BUFFER_OFFSET 0x0800 /* Next Tx/Rx buffer's offset */
  40. /* MDIO Address Register Bit Masks */
  41. #define XEL_MDIOADDR_REGADR_MASK 0x0000001F /* Register Address */
  42. #define XEL_MDIOADDR_PHYADR_MASK 0x000003E0 /* PHY Address */
  43. #define XEL_MDIOADDR_PHYADR_SHIFT 5
  44. #define XEL_MDIOADDR_OP_MASK 0x00000400 /* RD/WR Operation */
  45. /* MDIO Write Data Register Bit Masks */
  46. #define XEL_MDIOWR_WRDATA_MASK 0x0000FFFF /* Data to be Written */
  47. /* MDIO Read Data Register Bit Masks */
  48. #define XEL_MDIORD_RDDATA_MASK 0x0000FFFF /* Data to be Read */
  49. /* MDIO Control Register Bit Masks */
  50. #define XEL_MDIOCTRL_MDIOSTS_MASK 0x00000001 /* MDIO Status Mask */
  51. #define XEL_MDIOCTRL_MDIOEN_MASK 0x00000008 /* MDIO Enable */
  52. /* Global Interrupt Enable Register (GIER) Bit Masks */
  53. #define XEL_GIER_GIE_MASK 0x80000000 /* Global Enable */
  54. /* Transmit Status Register (TSR) Bit Masks */
  55. #define XEL_TSR_XMIT_BUSY_MASK 0x00000001 /* Tx complete */
  56. #define XEL_TSR_PROGRAM_MASK 0x00000002 /* Program the MAC address */
  57. #define XEL_TSR_XMIT_IE_MASK 0x00000008 /* Tx interrupt enable bit */
  58. #define XEL_TSR_XMIT_ACTIVE_MASK 0x80000000 /* Buffer is active, SW bit
  59. * only. This is not documented
  60. * in the HW spec
  61. */
  62. /* Define for programming the MAC address into the EmacLite */
  63. #define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
  64. /* Receive Status Register (RSR) */
  65. #define XEL_RSR_RECV_DONE_MASK 0x00000001 /* Rx complete */
  66. #define XEL_RSR_RECV_IE_MASK 0x00000008 /* Rx interrupt enable bit */
  67. /* Transmit Packet Length Register (TPLR) */
  68. #define XEL_TPLR_LENGTH_MASK 0x0000FFFF /* Tx packet length */
  69. /* Receive Packet Length Register (RPLR) */
  70. #define XEL_RPLR_LENGTH_MASK 0x0000FFFF /* Rx packet length */
  71. #define XEL_HEADER_OFFSET 12 /* Offset to length field */
  72. #define XEL_HEADER_SHIFT 16 /* Shift value for length */
  73. /* General Ethernet Definitions */
  74. #define XEL_ARP_PACKET_SIZE 28 /* Max ARP packet size */
  75. #define XEL_HEADER_IP_LENGTH_OFFSET 16 /* IP Length Offset */
  76. #define TX_TIMEOUT (60 * HZ) /* Tx timeout is 60 seconds. */
  77. #define ALIGNMENT 4
  78. /* BUFFER_ALIGN(adr) calculates the number of bytes to the next alignment. */
  79. #define BUFFER_ALIGN(adr) ((ALIGNMENT - ((u32)adr)) % ALIGNMENT)
  80. #ifdef __BIG_ENDIAN
  81. #define xemaclite_readl ioread32be
  82. #define xemaclite_writel iowrite32be
  83. #else
  84. #define xemaclite_readl ioread32
  85. #define xemaclite_writel iowrite32
  86. #endif
  87. /**
  88. * struct net_local - Our private per device data
  89. * @ndev: instance of the network device
  90. * @tx_ping_pong: indicates whether Tx Pong buffer is configured in HW
  91. * @rx_ping_pong: indicates whether Rx Pong buffer is configured in HW
  92. * @next_tx_buf_to_use: next Tx buffer to write to
  93. * @next_rx_buf_to_use: next Rx buffer to read from
  94. * @base_addr: base address of the Emaclite device
  95. * @reset_lock: lock used for synchronization
  96. * @deferred_skb: holds an skb (for transmission at a later time) when the
  97. * Tx buffer is not free
  98. * @phy_dev: pointer to the PHY device
  99. * @phy_node: pointer to the PHY device node
  100. * @mii_bus: pointer to the MII bus
  101. * @last_link: last link status
  102. */
  103. struct net_local {
  104. struct net_device *ndev;
  105. bool tx_ping_pong;
  106. bool rx_ping_pong;
  107. u32 next_tx_buf_to_use;
  108. u32 next_rx_buf_to_use;
  109. void __iomem *base_addr;
  110. spinlock_t reset_lock;
  111. struct sk_buff *deferred_skb;
  112. struct phy_device *phy_dev;
  113. struct device_node *phy_node;
  114. struct mii_bus *mii_bus;
  115. int last_link;
  116. };
  117. /*************************/
  118. /* EmacLite driver calls */
  119. /*************************/
  120. /**
  121. * xemaclite_enable_interrupts - Enable the interrupts for the EmacLite device
  122. * @drvdata: Pointer to the Emaclite device private data
  123. *
  124. * This function enables the Tx and Rx interrupts for the Emaclite device along
  125. * with the Global Interrupt Enable.
  126. */
  127. static void xemaclite_enable_interrupts(struct net_local *drvdata)
  128. {
  129. u32 reg_data;
  130. /* Enable the Tx interrupts for the first Buffer */
  131. reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET);
  132. xemaclite_writel(reg_data | XEL_TSR_XMIT_IE_MASK,
  133. drvdata->base_addr + XEL_TSR_OFFSET);
  134. /* Enable the Rx interrupts for the first buffer */
  135. xemaclite_writel(XEL_RSR_RECV_IE_MASK, drvdata->base_addr + XEL_RSR_OFFSET);
  136. /* Enable the Global Interrupt Enable */
  137. xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
  138. }
  139. /**
  140. * xemaclite_disable_interrupts - Disable the interrupts for the EmacLite device
  141. * @drvdata: Pointer to the Emaclite device private data
  142. *
  143. * This function disables the Tx and Rx interrupts for the Emaclite device,
  144. * along with the Global Interrupt Enable.
  145. */
  146. static void xemaclite_disable_interrupts(struct net_local *drvdata)
  147. {
  148. u32 reg_data;
  149. /* Disable the Global Interrupt Enable */
  150. xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
  151. /* Disable the Tx interrupts for the first buffer */
  152. reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET);
  153. xemaclite_writel(reg_data & (~XEL_TSR_XMIT_IE_MASK),
  154. drvdata->base_addr + XEL_TSR_OFFSET);
  155. /* Disable the Rx interrupts for the first buffer */
  156. reg_data = xemaclite_readl(drvdata->base_addr + XEL_RSR_OFFSET);
  157. xemaclite_writel(reg_data & (~XEL_RSR_RECV_IE_MASK),
  158. drvdata->base_addr + XEL_RSR_OFFSET);
  159. }
  160. /**
  161. * xemaclite_aligned_write - Write from 16-bit aligned to 32-bit aligned address
  162. * @src_ptr: Void pointer to the 16-bit aligned source address
  163. * @dest_ptr: Pointer to the 32-bit aligned destination address
  164. * @length: Number bytes to write from source to destination
  165. *
  166. * This function writes data from a 16-bit aligned buffer to a 32-bit aligned
  167. * address in the EmacLite device.
  168. */
  169. static void xemaclite_aligned_write(void *src_ptr, u32 *dest_ptr,
  170. unsigned length)
  171. {
  172. u32 align_buffer;
  173. u32 *to_u32_ptr;
  174. u16 *from_u16_ptr, *to_u16_ptr;
  175. to_u32_ptr = dest_ptr;
  176. from_u16_ptr = src_ptr;
  177. align_buffer = 0;
  178. for (; length > 3; length -= 4) {
  179. to_u16_ptr = (u16 *)&align_buffer;
  180. *to_u16_ptr++ = *from_u16_ptr++;
  181. *to_u16_ptr++ = *from_u16_ptr++;
  182. /* This barrier resolves occasional issues seen around
  183. * cases where the data is not properly flushed out
  184. * from the processor store buffers to the destination
  185. * memory locations.
  186. */
  187. wmb();
  188. /* Output a word */
  189. *to_u32_ptr++ = align_buffer;
  190. }
  191. if (length) {
  192. u8 *from_u8_ptr, *to_u8_ptr;
  193. /* Set up to output the remaining data */
  194. align_buffer = 0;
  195. to_u8_ptr = (u8 *)&align_buffer;
  196. from_u8_ptr = (u8 *)from_u16_ptr;
  197. /* Output the remaining data */
  198. for (; length > 0; length--)
  199. *to_u8_ptr++ = *from_u8_ptr++;
  200. /* This barrier resolves occasional issues seen around
  201. * cases where the data is not properly flushed out
  202. * from the processor store buffers to the destination
  203. * memory locations.
  204. */
  205. wmb();
  206. *to_u32_ptr = align_buffer;
  207. }
  208. }
  209. /**
  210. * xemaclite_aligned_read - Read from 32-bit aligned to 16-bit aligned buffer
  211. * @src_ptr: Pointer to the 32-bit aligned source address
  212. * @dest_ptr: Pointer to the 16-bit aligned destination address
  213. * @length: Number bytes to read from source to destination
  214. *
  215. * This function reads data from a 32-bit aligned address in the EmacLite device
  216. * to a 16-bit aligned buffer.
  217. */
  218. static void xemaclite_aligned_read(u32 *src_ptr, u8 *dest_ptr,
  219. unsigned length)
  220. {
  221. u16 *to_u16_ptr, *from_u16_ptr;
  222. u32 *from_u32_ptr;
  223. u32 align_buffer;
  224. from_u32_ptr = src_ptr;
  225. to_u16_ptr = (u16 *)dest_ptr;
  226. for (; length > 3; length -= 4) {
  227. /* Copy each word into the temporary buffer */
  228. align_buffer = *from_u32_ptr++;
  229. from_u16_ptr = (u16 *)&align_buffer;
  230. /* Read data from source */
  231. *to_u16_ptr++ = *from_u16_ptr++;
  232. *to_u16_ptr++ = *from_u16_ptr++;
  233. }
  234. if (length) {
  235. u8 *to_u8_ptr, *from_u8_ptr;
  236. /* Set up to read the remaining data */
  237. to_u8_ptr = (u8 *)to_u16_ptr;
  238. align_buffer = *from_u32_ptr++;
  239. from_u8_ptr = (u8 *)&align_buffer;
  240. /* Read the remaining data */
  241. for (; length > 0; length--)
  242. *to_u8_ptr = *from_u8_ptr;
  243. }
  244. }
  245. /**
  246. * xemaclite_send_data - Send an Ethernet frame
  247. * @drvdata: Pointer to the Emaclite device private data
  248. * @data: Pointer to the data to be sent
  249. * @byte_count: Total frame size, including header
  250. *
  251. * This function checks if the Tx buffer of the Emaclite device is free to send
  252. * data. If so, it fills the Tx buffer with data for transmission. Otherwise, it
  253. * returns an error.
  254. *
  255. * Return: 0 upon success or -1 if the buffer(s) are full.
  256. *
  257. * Note: The maximum Tx packet size can not be more than Ethernet header
  258. * (14 Bytes) + Maximum MTU (1500 bytes). This is excluding FCS.
  259. */
  260. static int xemaclite_send_data(struct net_local *drvdata, u8 *data,
  261. unsigned int byte_count)
  262. {
  263. u32 reg_data;
  264. void __iomem *addr;
  265. /* Determine the expected Tx buffer address */
  266. addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
  267. /* If the length is too large, truncate it */
  268. if (byte_count > ETH_FRAME_LEN)
  269. byte_count = ETH_FRAME_LEN;
  270. /* Check if the expected buffer is available */
  271. reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
  272. if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
  273. XEL_TSR_XMIT_ACTIVE_MASK)) == 0) {
  274. /* Switch to next buffer if configured */
  275. if (drvdata->tx_ping_pong != 0)
  276. drvdata->next_tx_buf_to_use ^= XEL_BUFFER_OFFSET;
  277. } else if (drvdata->tx_ping_pong != 0) {
  278. /* If the expected buffer is full, try the other buffer,
  279. * if it is configured in HW
  280. */
  281. addr = (void __iomem __force *)((u32 __force)addr ^
  282. XEL_BUFFER_OFFSET);
  283. reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
  284. if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
  285. XEL_TSR_XMIT_ACTIVE_MASK)) != 0)
  286. return -1; /* Buffers were full, return failure */
  287. } else
  288. return -1; /* Buffer was full, return failure */
  289. /* Write the frame to the buffer */
  290. xemaclite_aligned_write(data, (u32 __force *)addr, byte_count);
  291. xemaclite_writel((byte_count & XEL_TPLR_LENGTH_MASK),
  292. addr + XEL_TPLR_OFFSET);
  293. /* Update the Tx Status Register to indicate that there is a
  294. * frame to send. Set the XEL_TSR_XMIT_ACTIVE_MASK flag which
  295. * is used by the interrupt handler to check whether a frame
  296. * has been transmitted
  297. */
  298. reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
  299. reg_data |= (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_XMIT_ACTIVE_MASK);
  300. xemaclite_writel(reg_data, addr + XEL_TSR_OFFSET);
  301. return 0;
  302. }
  303. /**
  304. * xemaclite_recv_data - Receive a frame
  305. * @drvdata: Pointer to the Emaclite device private data
  306. * @data: Address where the data is to be received
  307. * @maxlen: Maximum supported ethernet packet length
  308. *
  309. * This function is intended to be called from the interrupt context or
  310. * with a wrapper which waits for the receive frame to be available.
  311. *
  312. * Return: Total number of bytes received
  313. */
  314. static u16 xemaclite_recv_data(struct net_local *drvdata, u8 *data, int maxlen)
  315. {
  316. void __iomem *addr;
  317. u16 length, proto_type;
  318. u32 reg_data;
  319. /* Determine the expected buffer address */
  320. addr = (drvdata->base_addr + drvdata->next_rx_buf_to_use);
  321. /* Verify which buffer has valid data */
  322. reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
  323. if ((reg_data & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
  324. if (drvdata->rx_ping_pong != 0)
  325. drvdata->next_rx_buf_to_use ^= XEL_BUFFER_OFFSET;
  326. } else {
  327. /* The instance is out of sync, try other buffer if other
  328. * buffer is configured, return 0 otherwise. If the instance is
  329. * out of sync, do not update the 'next_rx_buf_to_use' since it
  330. * will correct on subsequent calls
  331. */
  332. if (drvdata->rx_ping_pong != 0)
  333. addr = (void __iomem __force *)((u32 __force)addr ^
  334. XEL_BUFFER_OFFSET);
  335. else
  336. return 0; /* No data was available */
  337. /* Verify that buffer has valid data */
  338. reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
  339. if ((reg_data & XEL_RSR_RECV_DONE_MASK) !=
  340. XEL_RSR_RECV_DONE_MASK)
  341. return 0; /* No data was available */
  342. }
  343. /* Get the protocol type of the ethernet frame that arrived
  344. */
  345. proto_type = ((ntohl(xemaclite_readl(addr + XEL_HEADER_OFFSET +
  346. XEL_RXBUFF_OFFSET)) >> XEL_HEADER_SHIFT) &
  347. XEL_RPLR_LENGTH_MASK);
  348. /* Check if received ethernet frame is a raw ethernet frame
  349. * or an IP packet or an ARP packet
  350. */
  351. if (proto_type > ETH_DATA_LEN) {
  352. if (proto_type == ETH_P_IP) {
  353. length = ((ntohl(xemaclite_readl(addr +
  354. XEL_HEADER_IP_LENGTH_OFFSET +
  355. XEL_RXBUFF_OFFSET)) >>
  356. XEL_HEADER_SHIFT) &
  357. XEL_RPLR_LENGTH_MASK);
  358. length = min_t(u16, length, ETH_DATA_LEN);
  359. length += ETH_HLEN + ETH_FCS_LEN;
  360. } else if (proto_type == ETH_P_ARP)
  361. length = XEL_ARP_PACKET_SIZE + ETH_HLEN + ETH_FCS_LEN;
  362. else
  363. /* Field contains type other than IP or ARP, use max
  364. * frame size and let user parse it
  365. */
  366. length = ETH_FRAME_LEN + ETH_FCS_LEN;
  367. } else
  368. /* Use the length in the frame, plus the header and trailer */
  369. length = proto_type + ETH_HLEN + ETH_FCS_LEN;
  370. if (WARN_ON(length > maxlen))
  371. length = maxlen;
  372. /* Read from the EmacLite device */
  373. xemaclite_aligned_read((u32 __force *)(addr + XEL_RXBUFF_OFFSET),
  374. data, length);
  375. /* Acknowledge the frame */
  376. reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
  377. reg_data &= ~XEL_RSR_RECV_DONE_MASK;
  378. xemaclite_writel(reg_data, addr + XEL_RSR_OFFSET);
  379. return length;
  380. }
  381. /**
  382. * xemaclite_update_address - Update the MAC address in the device
  383. * @drvdata: Pointer to the Emaclite device private data
  384. * @address_ptr:Pointer to the MAC address (MAC address is a 48-bit value)
  385. *
  386. * Tx must be idle and Rx should be idle for deterministic results.
  387. * It is recommended that this function should be called after the
  388. * initialization and before transmission of any packets from the device.
  389. * The MAC address can be programmed using any of the two transmit
  390. * buffers (if configured).
  391. */
  392. static void xemaclite_update_address(struct net_local *drvdata,
  393. u8 *address_ptr)
  394. {
  395. void __iomem *addr;
  396. u32 reg_data;
  397. /* Determine the expected Tx buffer address */
  398. addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
  399. xemaclite_aligned_write(address_ptr, (u32 __force *)addr, ETH_ALEN);
  400. xemaclite_writel(ETH_ALEN, addr + XEL_TPLR_OFFSET);
  401. /* Update the MAC address in the EmacLite */
  402. reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
  403. xemaclite_writel(reg_data | XEL_TSR_PROG_MAC_ADDR, addr + XEL_TSR_OFFSET);
  404. /* Wait for EmacLite to finish with the MAC address update */
  405. while ((xemaclite_readl(addr + XEL_TSR_OFFSET) &
  406. XEL_TSR_PROG_MAC_ADDR) != 0)
  407. ;
  408. }
  409. /**
  410. * xemaclite_set_mac_address - Set the MAC address for this device
  411. * @dev: Pointer to the network device instance
  412. * @address: Void pointer to the sockaddr structure
  413. *
  414. * This function copies the HW address from the sockaddr strucutre to the
  415. * net_device structure and updates the address in HW.
  416. *
  417. * Return: Error if the net device is busy or 0 if the addr is set
  418. * successfully
  419. */
  420. static int xemaclite_set_mac_address(struct net_device *dev, void *address)
  421. {
  422. struct net_local *lp = netdev_priv(dev);
  423. struct sockaddr *addr = address;
  424. if (netif_running(dev))
  425. return -EBUSY;
  426. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  427. xemaclite_update_address(lp, dev->dev_addr);
  428. return 0;
  429. }
  430. /**
  431. * xemaclite_tx_timeout - Callback for Tx Timeout
  432. * @dev: Pointer to the network device
  433. *
  434. * This function is called when Tx time out occurs for Emaclite device.
  435. */
  436. static void xemaclite_tx_timeout(struct net_device *dev)
  437. {
  438. struct net_local *lp = netdev_priv(dev);
  439. unsigned long flags;
  440. dev_err(&lp->ndev->dev, "Exceeded transmit timeout of %lu ms\n",
  441. TX_TIMEOUT * 1000UL / HZ);
  442. dev->stats.tx_errors++;
  443. /* Reset the device */
  444. spin_lock_irqsave(&lp->reset_lock, flags);
  445. /* Shouldn't really be necessary, but shouldn't hurt */
  446. netif_stop_queue(dev);
  447. xemaclite_disable_interrupts(lp);
  448. xemaclite_enable_interrupts(lp);
  449. if (lp->deferred_skb) {
  450. dev_kfree_skb(lp->deferred_skb);
  451. lp->deferred_skb = NULL;
  452. dev->stats.tx_errors++;
  453. }
  454. /* To exclude tx timeout */
  455. netif_trans_update(dev); /* prevent tx timeout */
  456. /* We're all ready to go. Start the queue */
  457. netif_wake_queue(dev);
  458. spin_unlock_irqrestore(&lp->reset_lock, flags);
  459. }
  460. /**********************/
  461. /* Interrupt Handlers */
  462. /**********************/
  463. /**
  464. * xemaclite_tx_handler - Interrupt handler for frames sent
  465. * @dev: Pointer to the network device
  466. *
  467. * This function updates the number of packets transmitted and handles the
  468. * deferred skb, if there is one.
  469. */
  470. static void xemaclite_tx_handler(struct net_device *dev)
  471. {
  472. struct net_local *lp = netdev_priv(dev);
  473. dev->stats.tx_packets++;
  474. if (!lp->deferred_skb)
  475. return;
  476. if (xemaclite_send_data(lp, (u8 *)lp->deferred_skb->data,
  477. lp->deferred_skb->len))
  478. return;
  479. dev->stats.tx_bytes += lp->deferred_skb->len;
  480. dev_consume_skb_irq(lp->deferred_skb);
  481. lp->deferred_skb = NULL;
  482. netif_trans_update(dev); /* prevent tx timeout */
  483. netif_wake_queue(dev);
  484. }
  485. /**
  486. * xemaclite_rx_handler- Interrupt handler for frames received
  487. * @dev: Pointer to the network device
  488. *
  489. * This function allocates memory for a socket buffer, fills it with data
  490. * received and hands it over to the TCP/IP stack.
  491. */
  492. static void xemaclite_rx_handler(struct net_device *dev)
  493. {
  494. struct net_local *lp = netdev_priv(dev);
  495. struct sk_buff *skb;
  496. unsigned int align;
  497. u32 len;
  498. len = ETH_FRAME_LEN + ETH_FCS_LEN;
  499. skb = netdev_alloc_skb(dev, len + ALIGNMENT);
  500. if (!skb) {
  501. /* Couldn't get memory. */
  502. dev->stats.rx_dropped++;
  503. dev_err(&lp->ndev->dev, "Could not allocate receive buffer\n");
  504. return;
  505. }
  506. /* A new skb should have the data halfword aligned, but this code is
  507. * here just in case that isn't true. Calculate how many
  508. * bytes we should reserve to get the data to start on a word
  509. * boundary
  510. */
  511. align = BUFFER_ALIGN(skb->data);
  512. if (align)
  513. skb_reserve(skb, align);
  514. skb_reserve(skb, 2);
  515. len = xemaclite_recv_data(lp, (u8 *)skb->data, len);
  516. if (!len) {
  517. dev->stats.rx_errors++;
  518. dev_kfree_skb_irq(skb);
  519. return;
  520. }
  521. skb_put(skb, len); /* Tell the skb how much data we got */
  522. skb->protocol = eth_type_trans(skb, dev);
  523. skb_checksum_none_assert(skb);
  524. dev->stats.rx_packets++;
  525. dev->stats.rx_bytes += len;
  526. if (!skb_defer_rx_timestamp(skb))
  527. netif_rx(skb); /* Send the packet upstream */
  528. }
  529. /**
  530. * xemaclite_interrupt - Interrupt handler for this driver
  531. * @irq: Irq of the Emaclite device
  532. * @dev_id: Void pointer to the network device instance used as callback
  533. * reference
  534. *
  535. * Return: IRQ_HANDLED
  536. *
  537. * This function handles the Tx and Rx interrupts of the EmacLite device.
  538. */
  539. static irqreturn_t xemaclite_interrupt(int irq, void *dev_id)
  540. {
  541. bool tx_complete = false;
  542. struct net_device *dev = dev_id;
  543. struct net_local *lp = netdev_priv(dev);
  544. void __iomem *base_addr = lp->base_addr;
  545. u32 tx_status;
  546. /* Check if there is Rx Data available */
  547. if ((xemaclite_readl(base_addr + XEL_RSR_OFFSET) &
  548. XEL_RSR_RECV_DONE_MASK) ||
  549. (xemaclite_readl(base_addr + XEL_BUFFER_OFFSET + XEL_RSR_OFFSET)
  550. & XEL_RSR_RECV_DONE_MASK))
  551. xemaclite_rx_handler(dev);
  552. /* Check if the Transmission for the first buffer is completed */
  553. tx_status = xemaclite_readl(base_addr + XEL_TSR_OFFSET);
  554. if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
  555. (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
  556. tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
  557. xemaclite_writel(tx_status, base_addr + XEL_TSR_OFFSET);
  558. tx_complete = true;
  559. }
  560. /* Check if the Transmission for the second buffer is completed */
  561. tx_status = xemaclite_readl(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
  562. if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
  563. (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
  564. tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
  565. xemaclite_writel(tx_status, base_addr + XEL_BUFFER_OFFSET +
  566. XEL_TSR_OFFSET);
  567. tx_complete = true;
  568. }
  569. /* If there was a Tx interrupt, call the Tx Handler */
  570. if (tx_complete != 0)
  571. xemaclite_tx_handler(dev);
  572. return IRQ_HANDLED;
  573. }
  574. /**********************/
  575. /* MDIO Bus functions */
  576. /**********************/
  577. /**
  578. * xemaclite_mdio_wait - Wait for the MDIO to be ready to use
  579. * @lp: Pointer to the Emaclite device private data
  580. *
  581. * This function waits till the device is ready to accept a new MDIO
  582. * request.
  583. *
  584. * Return: 0 for success or ETIMEDOUT for a timeout
  585. */
  586. static int xemaclite_mdio_wait(struct net_local *lp)
  587. {
  588. u32 val;
  589. /* wait for the MDIO interface to not be busy or timeout
  590. * after some time.
  591. */
  592. return readx_poll_timeout(xemaclite_readl,
  593. lp->base_addr + XEL_MDIOCTRL_OFFSET,
  594. val, !(val & XEL_MDIOCTRL_MDIOSTS_MASK),
  595. 1000, 20000);
  596. }
  597. /**
  598. * xemaclite_mdio_read - Read from a given MII management register
  599. * @bus: the mii_bus struct
  600. * @phy_id: the phy address
  601. * @reg: register number to read from
  602. *
  603. * This function waits till the device is ready to accept a new MDIO
  604. * request and then writes the phy address to the MDIO Address register
  605. * and reads data from MDIO Read Data register, when its available.
  606. *
  607. * Return: Value read from the MII management register
  608. */
  609. static int xemaclite_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  610. {
  611. struct net_local *lp = bus->priv;
  612. u32 ctrl_reg;
  613. u32 rc;
  614. if (xemaclite_mdio_wait(lp))
  615. return -ETIMEDOUT;
  616. /* Write the PHY address, register number and set the OP bit in the
  617. * MDIO Address register. Set the Status bit in the MDIO Control
  618. * register to start a MDIO read transaction.
  619. */
  620. ctrl_reg = xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET);
  621. xemaclite_writel(XEL_MDIOADDR_OP_MASK |
  622. ((phy_id << XEL_MDIOADDR_PHYADR_SHIFT) | reg),
  623. lp->base_addr + XEL_MDIOADDR_OFFSET);
  624. xemaclite_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK,
  625. lp->base_addr + XEL_MDIOCTRL_OFFSET);
  626. if (xemaclite_mdio_wait(lp))
  627. return -ETIMEDOUT;
  628. rc = xemaclite_readl(lp->base_addr + XEL_MDIORD_OFFSET);
  629. dev_dbg(&lp->ndev->dev,
  630. "%s(phy_id=%i, reg=%x) == %x\n", __func__,
  631. phy_id, reg, rc);
  632. return rc;
  633. }
  634. /**
  635. * xemaclite_mdio_write - Write to a given MII management register
  636. * @bus: the mii_bus struct
  637. * @phy_id: the phy address
  638. * @reg: register number to write to
  639. * @val: value to write to the register number specified by reg
  640. *
  641. * This function waits till the device is ready to accept a new MDIO
  642. * request and then writes the val to the MDIO Write Data register.
  643. *
  644. * Return: 0 upon success or a negative error upon failure
  645. */
  646. static int xemaclite_mdio_write(struct mii_bus *bus, int phy_id, int reg,
  647. u16 val)
  648. {
  649. struct net_local *lp = bus->priv;
  650. u32 ctrl_reg;
  651. dev_dbg(&lp->ndev->dev,
  652. "%s(phy_id=%i, reg=%x, val=%x)\n", __func__,
  653. phy_id, reg, val);
  654. if (xemaclite_mdio_wait(lp))
  655. return -ETIMEDOUT;
  656. /* Write the PHY address, register number and clear the OP bit in the
  657. * MDIO Address register and then write the value into the MDIO Write
  658. * Data register. Finally, set the Status bit in the MDIO Control
  659. * register to start a MDIO write transaction.
  660. */
  661. ctrl_reg = xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET);
  662. xemaclite_writel(~XEL_MDIOADDR_OP_MASK &
  663. ((phy_id << XEL_MDIOADDR_PHYADR_SHIFT) | reg),
  664. lp->base_addr + XEL_MDIOADDR_OFFSET);
  665. xemaclite_writel(val, lp->base_addr + XEL_MDIOWR_OFFSET);
  666. xemaclite_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK,
  667. lp->base_addr + XEL_MDIOCTRL_OFFSET);
  668. return 0;
  669. }
  670. /**
  671. * xemaclite_mdio_setup - Register mii_bus for the Emaclite device
  672. * @lp: Pointer to the Emaclite device private data
  673. * @dev: Pointer to OF device structure
  674. *
  675. * This function enables MDIO bus in the Emaclite device and registers a
  676. * mii_bus.
  677. *
  678. * Return: 0 upon success or a negative error upon failure
  679. */
  680. static int xemaclite_mdio_setup(struct net_local *lp, struct device *dev)
  681. {
  682. struct mii_bus *bus;
  683. int rc;
  684. struct resource res;
  685. struct device_node *np = of_get_parent(lp->phy_node);
  686. struct device_node *npp;
  687. /* Don't register the MDIO bus if the phy_node or its parent node
  688. * can't be found.
  689. */
  690. if (!np) {
  691. dev_err(dev, "Failed to register mdio bus.\n");
  692. return -ENODEV;
  693. }
  694. npp = of_get_parent(np);
  695. of_address_to_resource(npp, 0, &res);
  696. if (lp->ndev->mem_start != res.start) {
  697. struct phy_device *phydev;
  698. phydev = of_phy_find_device(lp->phy_node);
  699. if (!phydev)
  700. dev_info(dev,
  701. "MDIO of the phy is not registered yet\n");
  702. else
  703. put_device(&phydev->mdio.dev);
  704. return 0;
  705. }
  706. /* Enable the MDIO bus by asserting the enable bit in MDIO Control
  707. * register.
  708. */
  709. xemaclite_writel(XEL_MDIOCTRL_MDIOEN_MASK,
  710. lp->base_addr + XEL_MDIOCTRL_OFFSET);
  711. bus = mdiobus_alloc();
  712. if (!bus) {
  713. dev_err(dev, "Failed to allocate mdiobus\n");
  714. return -ENOMEM;
  715. }
  716. snprintf(bus->id, MII_BUS_ID_SIZE, "%.8llx",
  717. (unsigned long long)res.start);
  718. bus->priv = lp;
  719. bus->name = "Xilinx Emaclite MDIO";
  720. bus->read = xemaclite_mdio_read;
  721. bus->write = xemaclite_mdio_write;
  722. bus->parent = dev;
  723. rc = of_mdiobus_register(bus, np);
  724. if (rc) {
  725. dev_err(dev, "Failed to register mdio bus.\n");
  726. goto err_register;
  727. }
  728. lp->mii_bus = bus;
  729. return 0;
  730. err_register:
  731. mdiobus_free(bus);
  732. return rc;
  733. }
  734. /**
  735. * xemaclite_adjust_link - Link state callback for the Emaclite device
  736. * @ndev: pointer to net_device struct
  737. *
  738. * There's nothing in the Emaclite device to be configured when the link
  739. * state changes. We just print the status.
  740. */
  741. static void xemaclite_adjust_link(struct net_device *ndev)
  742. {
  743. struct net_local *lp = netdev_priv(ndev);
  744. struct phy_device *phy = lp->phy_dev;
  745. int link_state;
  746. /* hash together the state values to decide if something has changed */
  747. link_state = phy->speed | (phy->duplex << 1) | phy->link;
  748. if (lp->last_link != link_state) {
  749. lp->last_link = link_state;
  750. phy_print_status(phy);
  751. }
  752. }
  753. /**
  754. * xemaclite_open - Open the network device
  755. * @dev: Pointer to the network device
  756. *
  757. * This function sets the MAC address, requests an IRQ and enables interrupts
  758. * for the Emaclite device and starts the Tx queue.
  759. * It also connects to the phy device, if MDIO is included in Emaclite device.
  760. *
  761. * Return: 0 on success. -ENODEV, if PHY cannot be connected.
  762. * Non-zero error value on failure.
  763. */
  764. static int xemaclite_open(struct net_device *dev)
  765. {
  766. struct net_local *lp = netdev_priv(dev);
  767. int retval;
  768. /* Just to be safe, stop the device first */
  769. xemaclite_disable_interrupts(lp);
  770. if (lp->phy_node) {
  771. u32 bmcr;
  772. lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
  773. xemaclite_adjust_link, 0,
  774. PHY_INTERFACE_MODE_MII);
  775. if (!lp->phy_dev) {
  776. dev_err(&lp->ndev->dev, "of_phy_connect() failed\n");
  777. return -ENODEV;
  778. }
  779. /* EmacLite doesn't support giga-bit speeds */
  780. phy_set_max_speed(lp->phy_dev, SPEED_100);
  781. /* Don't advertise 1000BASE-T Full/Half duplex speeds */
  782. phy_write(lp->phy_dev, MII_CTRL1000, 0);
  783. /* Advertise only 10 and 100mbps full/half duplex speeds */
  784. phy_write(lp->phy_dev, MII_ADVERTISE, ADVERTISE_ALL |
  785. ADVERTISE_CSMA);
  786. /* Restart auto negotiation */
  787. bmcr = phy_read(lp->phy_dev, MII_BMCR);
  788. bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
  789. phy_write(lp->phy_dev, MII_BMCR, bmcr);
  790. phy_start(lp->phy_dev);
  791. }
  792. /* Set the MAC address each time opened */
  793. xemaclite_update_address(lp, dev->dev_addr);
  794. /* Grab the IRQ */
  795. retval = request_irq(dev->irq, xemaclite_interrupt, 0, dev->name, dev);
  796. if (retval) {
  797. dev_err(&lp->ndev->dev, "Could not allocate interrupt %d\n",
  798. dev->irq);
  799. if (lp->phy_dev)
  800. phy_disconnect(lp->phy_dev);
  801. lp->phy_dev = NULL;
  802. return retval;
  803. }
  804. /* Enable Interrupts */
  805. xemaclite_enable_interrupts(lp);
  806. /* We're ready to go */
  807. netif_start_queue(dev);
  808. return 0;
  809. }
  810. /**
  811. * xemaclite_close - Close the network device
  812. * @dev: Pointer to the network device
  813. *
  814. * This function stops the Tx queue, disables interrupts and frees the IRQ for
  815. * the Emaclite device.
  816. * It also disconnects the phy device associated with the Emaclite device.
  817. *
  818. * Return: 0, always.
  819. */
  820. static int xemaclite_close(struct net_device *dev)
  821. {
  822. struct net_local *lp = netdev_priv(dev);
  823. netif_stop_queue(dev);
  824. xemaclite_disable_interrupts(lp);
  825. free_irq(dev->irq, dev);
  826. if (lp->phy_dev)
  827. phy_disconnect(lp->phy_dev);
  828. lp->phy_dev = NULL;
  829. return 0;
  830. }
  831. /**
  832. * xemaclite_send - Transmit a frame
  833. * @orig_skb: Pointer to the socket buffer to be transmitted
  834. * @dev: Pointer to the network device
  835. *
  836. * This function checks if the Tx buffer of the Emaclite device is free to send
  837. * data. If so, it fills the Tx buffer with data from socket buffer data,
  838. * updates the stats and frees the socket buffer. The Tx completion is signaled
  839. * by an interrupt. If the Tx buffer isn't free, then the socket buffer is
  840. * deferred and the Tx queue is stopped so that the deferred socket buffer can
  841. * be transmitted when the Emaclite device is free to transmit data.
  842. *
  843. * Return: NETDEV_TX_OK, always.
  844. */
  845. static netdev_tx_t
  846. xemaclite_send(struct sk_buff *orig_skb, struct net_device *dev)
  847. {
  848. struct net_local *lp = netdev_priv(dev);
  849. struct sk_buff *new_skb;
  850. unsigned int len;
  851. unsigned long flags;
  852. len = orig_skb->len;
  853. new_skb = orig_skb;
  854. spin_lock_irqsave(&lp->reset_lock, flags);
  855. if (xemaclite_send_data(lp, (u8 *)new_skb->data, len) != 0) {
  856. /* If the Emaclite Tx buffer is busy, stop the Tx queue and
  857. * defer the skb for transmission during the ISR, after the
  858. * current transmission is complete
  859. */
  860. netif_stop_queue(dev);
  861. lp->deferred_skb = new_skb;
  862. /* Take the time stamp now, since we can't do this in an ISR. */
  863. skb_tx_timestamp(new_skb);
  864. spin_unlock_irqrestore(&lp->reset_lock, flags);
  865. return NETDEV_TX_OK;
  866. }
  867. spin_unlock_irqrestore(&lp->reset_lock, flags);
  868. skb_tx_timestamp(new_skb);
  869. dev->stats.tx_bytes += len;
  870. dev_consume_skb_any(new_skb);
  871. return NETDEV_TX_OK;
  872. }
  873. /**
  874. * get_bool - Get a parameter from the OF device
  875. * @ofdev: Pointer to OF device structure
  876. * @s: Property to be retrieved
  877. *
  878. * This function looks for a property in the device node and returns the value
  879. * of the property if its found or 0 if the property is not found.
  880. *
  881. * Return: Value of the parameter if the parameter is found, or 0 otherwise
  882. */
  883. static bool get_bool(struct platform_device *ofdev, const char *s)
  884. {
  885. u32 *p = (u32 *)of_get_property(ofdev->dev.of_node, s, NULL);
  886. if (!p) {
  887. dev_warn(&ofdev->dev, "Parameter %s not found, defaulting to false\n", s);
  888. return false;
  889. }
  890. return (bool)*p;
  891. }
  892. /**
  893. * xemaclite_ethtools_get_drvinfo - Get various Axi Emac Lite driver info
  894. * @ndev: Pointer to net_device structure
  895. * @ed: Pointer to ethtool_drvinfo structure
  896. *
  897. * This implements ethtool command for getting the driver information.
  898. * Issue "ethtool -i ethX" under linux prompt to execute this function.
  899. */
  900. static void xemaclite_ethtools_get_drvinfo(struct net_device *ndev,
  901. struct ethtool_drvinfo *ed)
  902. {
  903. strlcpy(ed->driver, DRIVER_NAME, sizeof(ed->driver));
  904. }
  905. static const struct ethtool_ops xemaclite_ethtool_ops = {
  906. .get_drvinfo = xemaclite_ethtools_get_drvinfo,
  907. .get_link = ethtool_op_get_link,
  908. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  909. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  910. };
  911. static const struct net_device_ops xemaclite_netdev_ops;
  912. /**
  913. * xemaclite_of_probe - Probe method for the Emaclite device.
  914. * @ofdev: Pointer to OF device structure
  915. *
  916. * This function probes for the Emaclite device in the device tree.
  917. * It initializes the driver data structure and the hardware, sets the MAC
  918. * address and registers the network device.
  919. * It also registers a mii_bus for the Emaclite device, if MDIO is included
  920. * in the device.
  921. *
  922. * Return: 0, if the driver is bound to the Emaclite device, or
  923. * a negative error if there is failure.
  924. */
  925. static int xemaclite_of_probe(struct platform_device *ofdev)
  926. {
  927. struct resource *res;
  928. struct net_device *ndev = NULL;
  929. struct net_local *lp = NULL;
  930. struct device *dev = &ofdev->dev;
  931. const void *mac_address;
  932. int rc = 0;
  933. dev_info(dev, "Device Tree Probing\n");
  934. /* Create an ethernet device instance */
  935. ndev = alloc_etherdev(sizeof(struct net_local));
  936. if (!ndev)
  937. return -ENOMEM;
  938. dev_set_drvdata(dev, ndev);
  939. SET_NETDEV_DEV(ndev, &ofdev->dev);
  940. lp = netdev_priv(ndev);
  941. lp->ndev = ndev;
  942. /* Get IRQ for the device */
  943. res = platform_get_resource(ofdev, IORESOURCE_IRQ, 0);
  944. if (!res) {
  945. dev_err(dev, "no IRQ found\n");
  946. rc = -ENXIO;
  947. goto error;
  948. }
  949. ndev->irq = res->start;
  950. res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
  951. lp->base_addr = devm_ioremap_resource(&ofdev->dev, res);
  952. if (IS_ERR(lp->base_addr)) {
  953. rc = PTR_ERR(lp->base_addr);
  954. goto error;
  955. }
  956. ndev->mem_start = res->start;
  957. ndev->mem_end = res->end;
  958. spin_lock_init(&lp->reset_lock);
  959. lp->next_tx_buf_to_use = 0x0;
  960. lp->next_rx_buf_to_use = 0x0;
  961. lp->tx_ping_pong = get_bool(ofdev, "xlnx,tx-ping-pong");
  962. lp->rx_ping_pong = get_bool(ofdev, "xlnx,rx-ping-pong");
  963. mac_address = of_get_mac_address(ofdev->dev.of_node);
  964. if (!IS_ERR(mac_address)) {
  965. /* Set the MAC address. */
  966. ether_addr_copy(ndev->dev_addr, mac_address);
  967. } else {
  968. dev_warn(dev, "No MAC address found, using random\n");
  969. eth_hw_addr_random(ndev);
  970. }
  971. /* Clear the Tx CSR's in case this is a restart */
  972. xemaclite_writel(0, lp->base_addr + XEL_TSR_OFFSET);
  973. xemaclite_writel(0, lp->base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
  974. /* Set the MAC address in the EmacLite device */
  975. xemaclite_update_address(lp, ndev->dev_addr);
  976. lp->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
  977. xemaclite_mdio_setup(lp, &ofdev->dev);
  978. dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr);
  979. ndev->netdev_ops = &xemaclite_netdev_ops;
  980. ndev->ethtool_ops = &xemaclite_ethtool_ops;
  981. ndev->flags &= ~IFF_MULTICAST;
  982. ndev->watchdog_timeo = TX_TIMEOUT;
  983. /* Finally, register the device */
  984. rc = register_netdev(ndev);
  985. if (rc) {
  986. dev_err(dev,
  987. "Cannot register network device, aborting\n");
  988. goto error;
  989. }
  990. dev_info(dev,
  991. "Xilinx EmacLite at 0x%08X mapped to 0x%p, irq=%d\n",
  992. (unsigned int __force)ndev->mem_start, lp->base_addr, ndev->irq);
  993. return 0;
  994. error:
  995. free_netdev(ndev);
  996. return rc;
  997. }
  998. /**
  999. * xemaclite_of_remove - Unbind the driver from the Emaclite device.
  1000. * @of_dev: Pointer to OF device structure
  1001. *
  1002. * This function is called if a device is physically removed from the system or
  1003. * if the driver module is being unloaded. It frees any resources allocated to
  1004. * the device.
  1005. *
  1006. * Return: 0, always.
  1007. */
  1008. static int xemaclite_of_remove(struct platform_device *of_dev)
  1009. {
  1010. struct net_device *ndev = platform_get_drvdata(of_dev);
  1011. struct net_local *lp = netdev_priv(ndev);
  1012. /* Un-register the mii_bus, if configured */
  1013. if (lp->mii_bus) {
  1014. mdiobus_unregister(lp->mii_bus);
  1015. mdiobus_free(lp->mii_bus);
  1016. lp->mii_bus = NULL;
  1017. }
  1018. unregister_netdev(ndev);
  1019. of_node_put(lp->phy_node);
  1020. lp->phy_node = NULL;
  1021. free_netdev(ndev);
  1022. return 0;
  1023. }
  1024. #ifdef CONFIG_NET_POLL_CONTROLLER
  1025. static void
  1026. xemaclite_poll_controller(struct net_device *ndev)
  1027. {
  1028. disable_irq(ndev->irq);
  1029. xemaclite_interrupt(ndev->irq, ndev);
  1030. enable_irq(ndev->irq);
  1031. }
  1032. #endif
  1033. /* Ioctl MII Interface */
  1034. static int xemaclite_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1035. {
  1036. if (!dev->phydev || !netif_running(dev))
  1037. return -EINVAL;
  1038. switch (cmd) {
  1039. case SIOCGMIIPHY:
  1040. case SIOCGMIIREG:
  1041. case SIOCSMIIREG:
  1042. return phy_mii_ioctl(dev->phydev, rq, cmd);
  1043. default:
  1044. return -EOPNOTSUPP;
  1045. }
  1046. }
  1047. static const struct net_device_ops xemaclite_netdev_ops = {
  1048. .ndo_open = xemaclite_open,
  1049. .ndo_stop = xemaclite_close,
  1050. .ndo_start_xmit = xemaclite_send,
  1051. .ndo_set_mac_address = xemaclite_set_mac_address,
  1052. .ndo_tx_timeout = xemaclite_tx_timeout,
  1053. .ndo_do_ioctl = xemaclite_ioctl,
  1054. #ifdef CONFIG_NET_POLL_CONTROLLER
  1055. .ndo_poll_controller = xemaclite_poll_controller,
  1056. #endif
  1057. };
  1058. /* Match table for OF platform binding */
  1059. static const struct of_device_id xemaclite_of_match[] = {
  1060. { .compatible = "xlnx,opb-ethernetlite-1.01.a", },
  1061. { .compatible = "xlnx,opb-ethernetlite-1.01.b", },
  1062. { .compatible = "xlnx,xps-ethernetlite-1.00.a", },
  1063. { .compatible = "xlnx,xps-ethernetlite-2.00.a", },
  1064. { .compatible = "xlnx,xps-ethernetlite-2.01.a", },
  1065. { .compatible = "xlnx,xps-ethernetlite-3.00.a", },
  1066. { /* end of list */ },
  1067. };
  1068. MODULE_DEVICE_TABLE(of, xemaclite_of_match);
  1069. static struct platform_driver xemaclite_of_driver = {
  1070. .driver = {
  1071. .name = DRIVER_NAME,
  1072. .of_match_table = xemaclite_of_match,
  1073. },
  1074. .probe = xemaclite_of_probe,
  1075. .remove = xemaclite_of_remove,
  1076. };
  1077. module_platform_driver(xemaclite_of_driver);
  1078. MODULE_AUTHOR("Xilinx, Inc.");
  1079. MODULE_DESCRIPTION("Xilinx Ethernet MAC Lite driver");
  1080. MODULE_LICENSE("GPL");