xilinx_emaclite.c 36 KB

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