xilinx_emaclite.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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. #ifdef __BIG_ENDIAN
  82. #define xemaclite_readl ioread32be
  83. #define xemaclite_writel iowrite32be
  84. #else
  85. #define xemaclite_readl ioread32
  86. #define xemaclite_writel iowrite32
  87. #endif
  88. /**
  89. * struct net_local - Our private per device data
  90. * @ndev: instance of the network device
  91. * @tx_ping_pong: indicates whether Tx Pong buffer is configured in HW
  92. * @rx_ping_pong: indicates whether Rx Pong buffer is configured in HW
  93. * @next_tx_buf_to_use: next Tx buffer to write to
  94. * @next_rx_buf_to_use: next Rx buffer to read from
  95. * @base_addr: base address of the Emaclite device
  96. * @reset_lock: lock used for synchronization
  97. * @deferred_skb: holds an skb (for transmission at a later time) when the
  98. * Tx buffer is not free
  99. * @phy_dev: pointer to the PHY device
  100. * @phy_node: pointer to the PHY device node
  101. * @mii_bus: pointer to the MII bus
  102. * @last_link: last link status
  103. * @has_mdio: indicates whether MDIO is included in the HW
  104. */
  105. struct net_local {
  106. struct net_device *ndev;
  107. bool tx_ping_pong;
  108. bool rx_ping_pong;
  109. u32 next_tx_buf_to_use;
  110. u32 next_rx_buf_to_use;
  111. void __iomem *base_addr;
  112. spinlock_t reset_lock;
  113. struct sk_buff *deferred_skb;
  114. struct phy_device *phy_dev;
  115. struct device_node *phy_node;
  116. struct mii_bus *mii_bus;
  117. int last_link;
  118. bool has_mdio;
  119. };
  120. /*************************/
  121. /* EmacLite driver calls */
  122. /*************************/
  123. /**
  124. * xemaclite_enable_interrupts - Enable the interrupts for the EmacLite device
  125. * @drvdata: Pointer to the Emaclite device private data
  126. *
  127. * This function enables the Tx and Rx interrupts for the Emaclite device along
  128. * with the Global Interrupt Enable.
  129. */
  130. static void xemaclite_enable_interrupts(struct net_local *drvdata)
  131. {
  132. u32 reg_data;
  133. /* Enable the Tx interrupts for the first Buffer */
  134. reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET);
  135. xemaclite_writel(reg_data | XEL_TSR_XMIT_IE_MASK,
  136. drvdata->base_addr + XEL_TSR_OFFSET);
  137. /* Enable the Rx interrupts for the first buffer */
  138. xemaclite_writel(XEL_RSR_RECV_IE_MASK, drvdata->base_addr + XEL_RSR_OFFSET);
  139. /* Enable the Global Interrupt Enable */
  140. xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
  141. }
  142. /**
  143. * xemaclite_disable_interrupts - Disable the interrupts for the EmacLite device
  144. * @drvdata: Pointer to the Emaclite device private data
  145. *
  146. * This function disables the Tx and Rx interrupts for the Emaclite device,
  147. * along with the Global Interrupt Enable.
  148. */
  149. static void xemaclite_disable_interrupts(struct net_local *drvdata)
  150. {
  151. u32 reg_data;
  152. /* Disable the Global Interrupt Enable */
  153. xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
  154. /* Disable the Tx interrupts for the first buffer */
  155. reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET);
  156. xemaclite_writel(reg_data & (~XEL_TSR_XMIT_IE_MASK),
  157. drvdata->base_addr + XEL_TSR_OFFSET);
  158. /* Disable the Rx interrupts for the first buffer */
  159. reg_data = xemaclite_readl(drvdata->base_addr + XEL_RSR_OFFSET);
  160. xemaclite_writel(reg_data & (~XEL_RSR_RECV_IE_MASK),
  161. drvdata->base_addr + XEL_RSR_OFFSET);
  162. }
  163. /**
  164. * xemaclite_aligned_write - Write from 16-bit aligned to 32-bit aligned address
  165. * @src_ptr: Void pointer to the 16-bit aligned source address
  166. * @dest_ptr: Pointer to the 32-bit aligned destination address
  167. * @length: Number bytes to write from source to destination
  168. *
  169. * This function writes data from a 16-bit aligned buffer to a 32-bit aligned
  170. * address in the EmacLite device.
  171. */
  172. static void xemaclite_aligned_write(void *src_ptr, u32 *dest_ptr,
  173. unsigned length)
  174. {
  175. u32 align_buffer;
  176. u32 *to_u32_ptr;
  177. u16 *from_u16_ptr, *to_u16_ptr;
  178. to_u32_ptr = dest_ptr;
  179. from_u16_ptr = src_ptr;
  180. align_buffer = 0;
  181. for (; length > 3; length -= 4) {
  182. to_u16_ptr = (u16 *)&align_buffer;
  183. *to_u16_ptr++ = *from_u16_ptr++;
  184. *to_u16_ptr++ = *from_u16_ptr++;
  185. /* This barrier resolves occasional issues seen around
  186. * cases where the data is not properly flushed out
  187. * from the processor store buffers to the destination
  188. * memory locations.
  189. */
  190. wmb();
  191. /* Output a word */
  192. *to_u32_ptr++ = align_buffer;
  193. }
  194. if (length) {
  195. u8 *from_u8_ptr, *to_u8_ptr;
  196. /* Set up to output the remaining data */
  197. align_buffer = 0;
  198. to_u8_ptr = (u8 *) &align_buffer;
  199. from_u8_ptr = (u8 *) from_u16_ptr;
  200. /* Output the remaining data */
  201. for (; length > 0; length--)
  202. *to_u8_ptr++ = *from_u8_ptr++;
  203. /* This barrier resolves occasional issues seen around
  204. * cases where the data is not properly flushed out
  205. * from the processor store buffers to the destination
  206. * memory locations.
  207. */
  208. wmb();
  209. *to_u32_ptr = align_buffer;
  210. }
  211. }
  212. /**
  213. * xemaclite_aligned_read - Read from 32-bit aligned to 16-bit aligned buffer
  214. * @src_ptr: Pointer to the 32-bit aligned source address
  215. * @dest_ptr: Pointer to the 16-bit aligned destination address
  216. * @length: Number bytes to read from source to destination
  217. *
  218. * This function reads data from a 32-bit aligned address in the EmacLite device
  219. * to a 16-bit aligned buffer.
  220. */
  221. static void xemaclite_aligned_read(u32 *src_ptr, u8 *dest_ptr,
  222. unsigned length)
  223. {
  224. u16 *to_u16_ptr, *from_u16_ptr;
  225. u32 *from_u32_ptr;
  226. u32 align_buffer;
  227. from_u32_ptr = src_ptr;
  228. to_u16_ptr = (u16 *) dest_ptr;
  229. for (; length > 3; length -= 4) {
  230. /* Copy each word into the temporary buffer */
  231. align_buffer = *from_u32_ptr++;
  232. from_u16_ptr = (u16 *)&align_buffer;
  233. /* Read data from source */
  234. *to_u16_ptr++ = *from_u16_ptr++;
  235. *to_u16_ptr++ = *from_u16_ptr++;
  236. }
  237. if (length) {
  238. u8 *to_u8_ptr, *from_u8_ptr;
  239. /* Set up to read the remaining data */
  240. to_u8_ptr = (u8 *) to_u16_ptr;
  241. align_buffer = *from_u32_ptr++;
  242. from_u8_ptr = (u8 *) &align_buffer;
  243. /* Read the remaining data */
  244. for (; length > 0; length--)
  245. *to_u8_ptr = *from_u8_ptr;
  246. }
  247. }
  248. /**
  249. * xemaclite_send_data - Send an Ethernet frame
  250. * @drvdata: Pointer to the Emaclite device private data
  251. * @data: Pointer to the data to be sent
  252. * @byte_count: Total frame size, including header
  253. *
  254. * This function checks if the Tx buffer of the Emaclite device is free to send
  255. * data. If so, it fills the Tx buffer with data for transmission. Otherwise, it
  256. * returns an error.
  257. *
  258. * Return: 0 upon success or -1 if the buffer(s) are full.
  259. *
  260. * Note: The maximum Tx packet size can not be more than Ethernet header
  261. * (14 Bytes) + Maximum MTU (1500 bytes). This is excluding FCS.
  262. */
  263. static int xemaclite_send_data(struct net_local *drvdata, u8 *data,
  264. unsigned int byte_count)
  265. {
  266. u32 reg_data;
  267. void __iomem *addr;
  268. /* Determine the expected Tx buffer address */
  269. addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
  270. /* If the length is too large, truncate it */
  271. if (byte_count > ETH_FRAME_LEN)
  272. byte_count = ETH_FRAME_LEN;
  273. /* Check if the expected buffer is available */
  274. reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
  275. if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
  276. XEL_TSR_XMIT_ACTIVE_MASK)) == 0) {
  277. /* Switch to next buffer if configured */
  278. if (drvdata->tx_ping_pong != 0)
  279. drvdata->next_tx_buf_to_use ^= XEL_BUFFER_OFFSET;
  280. } else if (drvdata->tx_ping_pong != 0) {
  281. /* If the expected buffer is full, try the other buffer,
  282. * if it is configured in HW */
  283. addr = (void __iomem __force *)((u32 __force)addr ^
  284. XEL_BUFFER_OFFSET);
  285. reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
  286. if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
  287. XEL_TSR_XMIT_ACTIVE_MASK)) != 0)
  288. return -1; /* Buffers were full, return failure */
  289. } else
  290. return -1; /* Buffer was full, return failure */
  291. /* Write the frame to the buffer */
  292. xemaclite_aligned_write(data, (u32 __force *) addr, byte_count);
  293. xemaclite_writel((byte_count & XEL_TPLR_LENGTH_MASK),
  294. addr + XEL_TPLR_OFFSET);
  295. /* Update the Tx Status Register to indicate that there is a
  296. * frame to send. Set the XEL_TSR_XMIT_ACTIVE_MASK flag which
  297. * is used by the interrupt handler to check whether a frame
  298. * has been transmitted */
  299. reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
  300. reg_data |= (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_XMIT_ACTIVE_MASK);
  301. xemaclite_writel(reg_data, addr + XEL_TSR_OFFSET);
  302. return 0;
  303. }
  304. /**
  305. * xemaclite_recv_data - Receive a frame
  306. * @drvdata: Pointer to the Emaclite device private data
  307. * @data: Address where the data is to be received
  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. if (drvdata->rx_ping_pong != 0)
  332. addr = (void __iomem __force *)((u32 __force)addr ^
  333. XEL_BUFFER_OFFSET);
  334. else
  335. return 0; /* No data was available */
  336. /* Verify that buffer has valid data */
  337. reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
  338. if ((reg_data & XEL_RSR_RECV_DONE_MASK) !=
  339. XEL_RSR_RECV_DONE_MASK)
  340. return 0; /* No data was available */
  341. }
  342. /* Get the protocol type of the ethernet frame that arrived */
  343. proto_type = ((ntohl(xemaclite_readl(addr + XEL_HEADER_OFFSET +
  344. XEL_RXBUFF_OFFSET)) >> XEL_HEADER_SHIFT) &
  345. XEL_RPLR_LENGTH_MASK);
  346. /* Check if received ethernet frame is a raw ethernet frame
  347. * or an IP packet or an ARP packet */
  348. if (proto_type > ETH_DATA_LEN) {
  349. if (proto_type == ETH_P_IP) {
  350. length = ((ntohl(xemaclite_readl(addr +
  351. XEL_HEADER_IP_LENGTH_OFFSET +
  352. XEL_RXBUFF_OFFSET)) >>
  353. XEL_HEADER_SHIFT) &
  354. XEL_RPLR_LENGTH_MASK);
  355. length = min_t(u16, length, ETH_DATA_LEN);
  356. length += ETH_HLEN + ETH_FCS_LEN;
  357. } else if (proto_type == ETH_P_ARP)
  358. length = XEL_ARP_PACKET_SIZE + ETH_HLEN + ETH_FCS_LEN;
  359. else
  360. /* Field contains type other than IP or ARP, use max
  361. * frame size and let user parse it */
  362. length = ETH_FRAME_LEN + ETH_FCS_LEN;
  363. } else
  364. /* Use the length in the frame, plus the header and trailer */
  365. length = proto_type + ETH_HLEN + ETH_FCS_LEN;
  366. if (WARN_ON(length > maxlen))
  367. length = maxlen;
  368. /* Read from the EmacLite device */
  369. xemaclite_aligned_read((u32 __force *) (addr + XEL_RXBUFF_OFFSET),
  370. data, length);
  371. /* Acknowledge the frame */
  372. reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
  373. reg_data &= ~XEL_RSR_RECV_DONE_MASK;
  374. xemaclite_writel(reg_data, addr + XEL_RSR_OFFSET);
  375. return length;
  376. }
  377. /**
  378. * xemaclite_update_address - Update the MAC address in the device
  379. * @drvdata: Pointer to the Emaclite device private data
  380. * @address_ptr:Pointer to the MAC address (MAC address is a 48-bit value)
  381. *
  382. * Tx must be idle and Rx should be idle for deterministic results.
  383. * It is recommended that this function should be called after the
  384. * initialization and before transmission of any packets from the device.
  385. * The MAC address can be programmed using any of the two transmit
  386. * buffers (if configured).
  387. */
  388. static void xemaclite_update_address(struct net_local *drvdata,
  389. u8 *address_ptr)
  390. {
  391. void __iomem *addr;
  392. u32 reg_data;
  393. /* Determine the expected Tx buffer address */
  394. addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
  395. xemaclite_aligned_write(address_ptr, (u32 __force *) addr, ETH_ALEN);
  396. xemaclite_writel(ETH_ALEN, addr + XEL_TPLR_OFFSET);
  397. /* Update the MAC address in the EmacLite */
  398. reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
  399. xemaclite_writel(reg_data | XEL_TSR_PROG_MAC_ADDR, addr + XEL_TSR_OFFSET);
  400. /* Wait for EmacLite to finish with the MAC address update */
  401. while ((xemaclite_readl(addr + XEL_TSR_OFFSET) &
  402. XEL_TSR_PROG_MAC_ADDR) != 0)
  403. ;
  404. }
  405. /**
  406. * xemaclite_set_mac_address - Set the MAC address for this device
  407. * @dev: Pointer to the network device instance
  408. * @addr: Void pointer to the sockaddr structure
  409. *
  410. * This function copies the HW address from the sockaddr strucutre to the
  411. * net_device structure and updates the address in HW.
  412. *
  413. * Return: Error if the net device is busy or 0 if the addr is set
  414. * successfully
  415. */
  416. static int xemaclite_set_mac_address(struct net_device *dev, void *address)
  417. {
  418. struct net_local *lp = netdev_priv(dev);
  419. struct sockaddr *addr = address;
  420. if (netif_running(dev))
  421. return -EBUSY;
  422. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  423. xemaclite_update_address(lp, dev->dev_addr);
  424. return 0;
  425. }
  426. /**
  427. * xemaclite_tx_timeout - Callback for Tx Timeout
  428. * @dev: Pointer to the network device
  429. *
  430. * This function is called when Tx time out occurs for Emaclite device.
  431. */
  432. static void xemaclite_tx_timeout(struct net_device *dev)
  433. {
  434. struct net_local *lp = netdev_priv(dev);
  435. unsigned long flags;
  436. dev_err(&lp->ndev->dev, "Exceeded transmit timeout of %lu ms\n",
  437. TX_TIMEOUT * 1000UL / HZ);
  438. dev->stats.tx_errors++;
  439. /* Reset the device */
  440. spin_lock_irqsave(&lp->reset_lock, flags);
  441. /* Shouldn't really be necessary, but shouldn't hurt */
  442. netif_stop_queue(dev);
  443. xemaclite_disable_interrupts(lp);
  444. xemaclite_enable_interrupts(lp);
  445. if (lp->deferred_skb) {
  446. dev_kfree_skb(lp->deferred_skb);
  447. lp->deferred_skb = NULL;
  448. dev->stats.tx_errors++;
  449. }
  450. /* To exclude tx timeout */
  451. netif_trans_update(dev); /* prevent tx timeout */
  452. /* We're all ready to go. Start the queue */
  453. netif_wake_queue(dev);
  454. spin_unlock_irqrestore(&lp->reset_lock, flags);
  455. }
  456. /**********************/
  457. /* Interrupt Handlers */
  458. /**********************/
  459. /**
  460. * xemaclite_tx_handler - Interrupt handler for frames sent
  461. * @dev: Pointer to the network device
  462. *
  463. * This function updates the number of packets transmitted and handles the
  464. * deferred skb, if there is one.
  465. */
  466. static void xemaclite_tx_handler(struct net_device *dev)
  467. {
  468. struct net_local *lp = netdev_priv(dev);
  469. dev->stats.tx_packets++;
  470. if (lp->deferred_skb) {
  471. if (xemaclite_send_data(lp,
  472. (u8 *) lp->deferred_skb->data,
  473. lp->deferred_skb->len) != 0)
  474. return;
  475. else {
  476. dev->stats.tx_bytes += lp->deferred_skb->len;
  477. dev_kfree_skb_irq(lp->deferred_skb);
  478. lp->deferred_skb = NULL;
  479. netif_trans_update(dev); /* prevent tx timeout */
  480. netif_wake_queue(dev);
  481. }
  482. }
  483. }
  484. /**
  485. * xemaclite_rx_handler- Interrupt handler for frames received
  486. * @dev: Pointer to the network device
  487. *
  488. * This function allocates memory for a socket buffer, fills it with data
  489. * received and hands it over to the TCP/IP stack.
  490. */
  491. static void xemaclite_rx_handler(struct net_device *dev)
  492. {
  493. struct net_local *lp = netdev_priv(dev);
  494. struct sk_buff *skb;
  495. unsigned int align;
  496. u32 len;
  497. len = ETH_FRAME_LEN + ETH_FCS_LEN;
  498. skb = netdev_alloc_skb(dev, len + ALIGNMENT);
  499. if (!skb) {
  500. /* Couldn't get memory. */
  501. dev->stats.rx_dropped++;
  502. dev_err(&lp->ndev->dev, "Could not allocate receive buffer\n");
  503. return;
  504. }
  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. align = BUFFER_ALIGN(skb->data);
  511. if (align)
  512. skb_reserve(skb, align);
  513. skb_reserve(skb, 2);
  514. len = xemaclite_recv_data(lp, (u8 *) skb->data, len);
  515. if (!len) {
  516. dev->stats.rx_errors++;
  517. dev_kfree_skb_irq(skb);
  518. return;
  519. }
  520. skb_put(skb, len); /* Tell the skb how much data we got */
  521. skb->protocol = eth_type_trans(skb, dev);
  522. skb_checksum_none_assert(skb);
  523. dev->stats.rx_packets++;
  524. dev->stats.rx_bytes += len;
  525. if (!skb_defer_rx_timestamp(skb))
  526. netif_rx(skb); /* Send the packet upstream */
  527. }
  528. /**
  529. * xemaclite_interrupt - Interrupt handler for this driver
  530. * @irq: Irq of the Emaclite device
  531. * @dev_id: Void pointer to the network device instance used as callback
  532. * reference
  533. *
  534. * This function handles the Tx and Rx interrupts of the EmacLite device.
  535. */
  536. static irqreturn_t xemaclite_interrupt(int irq, void *dev_id)
  537. {
  538. bool tx_complete = false;
  539. struct net_device *dev = dev_id;
  540. struct net_local *lp = netdev_priv(dev);
  541. void __iomem *base_addr = lp->base_addr;
  542. u32 tx_status;
  543. /* Check if there is Rx Data available */
  544. if ((xemaclite_readl(base_addr + XEL_RSR_OFFSET) &
  545. XEL_RSR_RECV_DONE_MASK) ||
  546. (xemaclite_readl(base_addr + XEL_BUFFER_OFFSET + XEL_RSR_OFFSET)
  547. & XEL_RSR_RECV_DONE_MASK))
  548. xemaclite_rx_handler(dev);
  549. /* Check if the Transmission for the first buffer is completed */
  550. tx_status = xemaclite_readl(base_addr + XEL_TSR_OFFSET);
  551. if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
  552. (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
  553. tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
  554. xemaclite_writel(tx_status, base_addr + XEL_TSR_OFFSET);
  555. tx_complete = true;
  556. }
  557. /* Check if the Transmission for the second buffer is completed */
  558. tx_status = xemaclite_readl(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
  559. if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
  560. (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
  561. tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
  562. xemaclite_writel(tx_status, base_addr + XEL_BUFFER_OFFSET +
  563. XEL_TSR_OFFSET);
  564. tx_complete = true;
  565. }
  566. /* If there was a Tx interrupt, call the Tx Handler */
  567. if (tx_complete != 0)
  568. xemaclite_tx_handler(dev);
  569. return IRQ_HANDLED;
  570. }
  571. /**********************/
  572. /* MDIO Bus functions */
  573. /**********************/
  574. /**
  575. * xemaclite_mdio_wait - Wait for the MDIO to be ready to use
  576. * @lp: Pointer to the Emaclite device private data
  577. *
  578. * This function waits till the device is ready to accept a new MDIO
  579. * request.
  580. *
  581. * Return: 0 for success or ETIMEDOUT for a timeout
  582. */
  583. static int xemaclite_mdio_wait(struct net_local *lp)
  584. {
  585. unsigned long end = jiffies + 2;
  586. /* wait for the MDIO interface to not be busy or timeout
  587. after some time.
  588. */
  589. while (xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET) &
  590. XEL_MDIOCTRL_MDIOSTS_MASK) {
  591. if (time_before_eq(end, jiffies)) {
  592. WARN_ON(1);
  593. return -ETIMEDOUT;
  594. }
  595. msleep(1);
  596. }
  597. return 0;
  598. }
  599. /**
  600. * xemaclite_mdio_read - Read from a given MII management register
  601. * @bus: the mii_bus struct
  602. * @phy_id: the phy address
  603. * @reg: register number to read from
  604. *
  605. * This function waits till the device is ready to accept a new MDIO
  606. * request and then writes the phy address to the MDIO Address register
  607. * and reads data from MDIO Read Data register, when its available.
  608. *
  609. * Return: Value read from the MII management register
  610. */
  611. static int xemaclite_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  612. {
  613. struct net_local *lp = bus->priv;
  614. u32 ctrl_reg;
  615. u32 rc;
  616. if (xemaclite_mdio_wait(lp))
  617. return -ETIMEDOUT;
  618. /* Write the PHY address, register number and set the OP bit in the
  619. * MDIO Address register. Set the Status bit in the MDIO Control
  620. * register to start a MDIO read transaction.
  621. */
  622. ctrl_reg = xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET);
  623. xemaclite_writel(XEL_MDIOADDR_OP_MASK |
  624. ((phy_id << XEL_MDIOADDR_PHYADR_SHIFT) | reg),
  625. lp->base_addr + XEL_MDIOADDR_OFFSET);
  626. xemaclite_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK,
  627. lp->base_addr + XEL_MDIOCTRL_OFFSET);
  628. if (xemaclite_mdio_wait(lp))
  629. return -ETIMEDOUT;
  630. rc = xemaclite_readl(lp->base_addr + XEL_MDIORD_OFFSET);
  631. dev_dbg(&lp->ndev->dev,
  632. "xemaclite_mdio_read(phy_id=%i, reg=%x) == %x\n",
  633. phy_id, reg, rc);
  634. return rc;
  635. }
  636. /**
  637. * xemaclite_mdio_write - Write to a given MII management register
  638. * @bus: the mii_bus struct
  639. * @phy_id: the phy address
  640. * @reg: register number to write to
  641. * @val: value to write to the register number specified by reg
  642. *
  643. * This function waits till the device is ready to accept a new MDIO
  644. * request and then writes the val to the MDIO Write Data register.
  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. "xemaclite_mdio_write(phy_id=%i, reg=%x, val=%x)\n",
  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. * @ofdev: 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. lp->mii_bus = bus;
  724. rc = of_mdiobus_register(bus, np);
  725. if (rc) {
  726. dev_err(dev, "Failed to register mdio bus.\n");
  727. goto err_register;
  728. }
  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. static int xemaclite_open(struct net_device *dev)
  762. {
  763. struct net_local *lp = netdev_priv(dev);
  764. int retval;
  765. /* Just to be safe, stop the device first */
  766. xemaclite_disable_interrupts(lp);
  767. if (lp->phy_node) {
  768. u32 bmcr;
  769. lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
  770. xemaclite_adjust_link, 0,
  771. PHY_INTERFACE_MODE_MII);
  772. if (!lp->phy_dev) {
  773. dev_err(&lp->ndev->dev, "of_phy_connect() failed\n");
  774. return -ENODEV;
  775. }
  776. /* EmacLite doesn't support giga-bit speeds */
  777. lp->phy_dev->supported &= (PHY_BASIC_FEATURES);
  778. lp->phy_dev->advertising = lp->phy_dev->supported;
  779. /* Don't advertise 1000BASE-T Full/Half duplex speeds */
  780. phy_write(lp->phy_dev, MII_CTRL1000, 0);
  781. /* Advertise only 10 and 100mbps full/half duplex speeds */
  782. phy_write(lp->phy_dev, MII_ADVERTISE, ADVERTISE_ALL |
  783. ADVERTISE_CSMA);
  784. /* Restart auto negotiation */
  785. bmcr = phy_read(lp->phy_dev, MII_BMCR);
  786. bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
  787. phy_write(lp->phy_dev, MII_BMCR, bmcr);
  788. phy_start(lp->phy_dev);
  789. }
  790. /* Set the MAC address each time opened */
  791. xemaclite_update_address(lp, dev->dev_addr);
  792. /* Grab the IRQ */
  793. retval = request_irq(dev->irq, xemaclite_interrupt, 0, dev->name, dev);
  794. if (retval) {
  795. dev_err(&lp->ndev->dev, "Could not allocate interrupt %d\n",
  796. dev->irq);
  797. if (lp->phy_dev)
  798. phy_disconnect(lp->phy_dev);
  799. lp->phy_dev = NULL;
  800. return retval;
  801. }
  802. /* Enable Interrupts */
  803. xemaclite_enable_interrupts(lp);
  804. /* We're ready to go */
  805. netif_start_queue(dev);
  806. return 0;
  807. }
  808. /**
  809. * xemaclite_close - Close the network device
  810. * @dev: Pointer to the network device
  811. *
  812. * This function stops the Tx queue, disables interrupts and frees the IRQ for
  813. * the Emaclite device.
  814. * It also disconnects the phy device associated with the Emaclite device.
  815. */
  816. static int xemaclite_close(struct net_device *dev)
  817. {
  818. struct net_local *lp = netdev_priv(dev);
  819. netif_stop_queue(dev);
  820. xemaclite_disable_interrupts(lp);
  821. free_irq(dev->irq, dev);
  822. if (lp->phy_dev)
  823. phy_disconnect(lp->phy_dev);
  824. lp->phy_dev = NULL;
  825. return 0;
  826. }
  827. /**
  828. * xemaclite_send - Transmit a frame
  829. * @orig_skb: Pointer to the socket buffer to be transmitted
  830. * @dev: Pointer to the network device
  831. *
  832. * This function checks if the Tx buffer of the Emaclite device is free to send
  833. * data. If so, it fills the Tx buffer with data from socket buffer data,
  834. * updates the stats and frees the socket buffer. The Tx completion is signaled
  835. * by an interrupt. If the Tx buffer isn't free, then the socket buffer is
  836. * deferred and the Tx queue is stopped so that the deferred socket buffer can
  837. * be transmitted when the Emaclite device is free to transmit data.
  838. *
  839. * Return: 0, always.
  840. */
  841. static int xemaclite_send(struct sk_buff *orig_skb, struct net_device *dev)
  842. {
  843. struct net_local *lp = netdev_priv(dev);
  844. struct sk_buff *new_skb;
  845. unsigned int len;
  846. unsigned long flags;
  847. len = orig_skb->len;
  848. new_skb = orig_skb;
  849. spin_lock_irqsave(&lp->reset_lock, flags);
  850. if (xemaclite_send_data(lp, (u8 *) new_skb->data, len) != 0) {
  851. /* If the Emaclite Tx buffer is busy, stop the Tx queue and
  852. * defer the skb for transmission during the ISR, after the
  853. * current transmission is complete */
  854. netif_stop_queue(dev);
  855. lp->deferred_skb = new_skb;
  856. /* Take the time stamp now, since we can't do this in an ISR. */
  857. skb_tx_timestamp(new_skb);
  858. spin_unlock_irqrestore(&lp->reset_lock, flags);
  859. return 0;
  860. }
  861. spin_unlock_irqrestore(&lp->reset_lock, flags);
  862. skb_tx_timestamp(new_skb);
  863. dev->stats.tx_bytes += len;
  864. dev_consume_skb_any(new_skb);
  865. return 0;
  866. }
  867. /**
  868. * get_bool - Get a parameter from the OF device
  869. * @ofdev: Pointer to OF device structure
  870. * @s: Property to be retrieved
  871. *
  872. * This function looks for a property in the device node and returns the value
  873. * of the property if its found or 0 if the property is not found.
  874. *
  875. * Return: Value of the parameter if the parameter is found, or 0 otherwise
  876. */
  877. static bool get_bool(struct platform_device *ofdev, const char *s)
  878. {
  879. u32 *p = (u32 *)of_get_property(ofdev->dev.of_node, s, NULL);
  880. if (p) {
  881. return (bool)*p;
  882. } else {
  883. dev_warn(&ofdev->dev, "Parameter %s not found,"
  884. "defaulting to false\n", s);
  885. return false;
  886. }
  887. }
  888. static const struct net_device_ops xemaclite_netdev_ops;
  889. /**
  890. * xemaclite_of_probe - Probe method for the Emaclite device.
  891. * @ofdev: Pointer to OF device structure
  892. * @match: Pointer to the structure used for matching a device
  893. *
  894. * This function probes for the Emaclite device in the device tree.
  895. * It initializes the driver data structure and the hardware, sets the MAC
  896. * address and registers the network device.
  897. * It also registers a mii_bus for the Emaclite device, if MDIO is included
  898. * in the device.
  899. *
  900. * Return: 0, if the driver is bound to the Emaclite device, or
  901. * a negative error if there is failure.
  902. */
  903. static int xemaclite_of_probe(struct platform_device *ofdev)
  904. {
  905. struct resource *res;
  906. struct net_device *ndev = NULL;
  907. struct net_local *lp = NULL;
  908. struct device *dev = &ofdev->dev;
  909. const void *mac_address;
  910. int rc = 0;
  911. dev_info(dev, "Device Tree Probing\n");
  912. /* Create an ethernet device instance */
  913. ndev = alloc_etherdev(sizeof(struct net_local));
  914. if (!ndev)
  915. return -ENOMEM;
  916. dev_set_drvdata(dev, ndev);
  917. SET_NETDEV_DEV(ndev, &ofdev->dev);
  918. lp = netdev_priv(ndev);
  919. lp->ndev = ndev;
  920. /* Get IRQ for the device */
  921. res = platform_get_resource(ofdev, IORESOURCE_IRQ, 0);
  922. if (!res) {
  923. dev_err(dev, "no IRQ found\n");
  924. rc = -ENXIO;
  925. goto error;
  926. }
  927. ndev->irq = res->start;
  928. res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
  929. lp->base_addr = devm_ioremap_resource(&ofdev->dev, res);
  930. if (IS_ERR(lp->base_addr)) {
  931. rc = PTR_ERR(lp->base_addr);
  932. goto error;
  933. }
  934. ndev->mem_start = res->start;
  935. ndev->mem_end = res->end;
  936. spin_lock_init(&lp->reset_lock);
  937. lp->next_tx_buf_to_use = 0x0;
  938. lp->next_rx_buf_to_use = 0x0;
  939. lp->tx_ping_pong = get_bool(ofdev, "xlnx,tx-ping-pong");
  940. lp->rx_ping_pong = get_bool(ofdev, "xlnx,rx-ping-pong");
  941. mac_address = of_get_mac_address(ofdev->dev.of_node);
  942. if (mac_address) {
  943. /* Set the MAC address. */
  944. memcpy(ndev->dev_addr, mac_address, ETH_ALEN);
  945. } else {
  946. dev_warn(dev, "No MAC address found, using random\n");
  947. eth_hw_addr_random(ndev);
  948. }
  949. /* Clear the Tx CSR's in case this is a restart */
  950. xemaclite_writel(0, lp->base_addr + XEL_TSR_OFFSET);
  951. xemaclite_writel(0, lp->base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
  952. /* Set the MAC address in the EmacLite device */
  953. xemaclite_update_address(lp, ndev->dev_addr);
  954. lp->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
  955. rc = xemaclite_mdio_setup(lp, &ofdev->dev);
  956. if (rc)
  957. dev_warn(&ofdev->dev, "error registering MDIO bus\n");
  958. dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr);
  959. ndev->netdev_ops = &xemaclite_netdev_ops;
  960. ndev->flags &= ~IFF_MULTICAST;
  961. ndev->watchdog_timeo = TX_TIMEOUT;
  962. /* Finally, register the device */
  963. rc = register_netdev(ndev);
  964. if (rc) {
  965. dev_err(dev,
  966. "Cannot register network device, aborting\n");
  967. goto error;
  968. }
  969. dev_info(dev,
  970. "Xilinx EmacLite at 0x%08X mapped to 0x%08X, irq=%d\n",
  971. (unsigned int __force)ndev->mem_start,
  972. (unsigned int __force)lp->base_addr, ndev->irq);
  973. return 0;
  974. error:
  975. free_netdev(ndev);
  976. return rc;
  977. }
  978. /**
  979. * xemaclite_of_remove - Unbind the driver from the Emaclite device.
  980. * @of_dev: Pointer to OF device structure
  981. *
  982. * This function is called if a device is physically removed from the system or
  983. * if the driver module is being unloaded. It frees any resources allocated to
  984. * the device.
  985. *
  986. * Return: 0, always.
  987. */
  988. static int xemaclite_of_remove(struct platform_device *of_dev)
  989. {
  990. struct net_device *ndev = platform_get_drvdata(of_dev);
  991. struct net_local *lp = netdev_priv(ndev);
  992. /* Un-register the mii_bus, if configured */
  993. if (lp->has_mdio) {
  994. mdiobus_unregister(lp->mii_bus);
  995. mdiobus_free(lp->mii_bus);
  996. lp->mii_bus = NULL;
  997. }
  998. unregister_netdev(ndev);
  999. of_node_put(lp->phy_node);
  1000. lp->phy_node = NULL;
  1001. free_netdev(ndev);
  1002. return 0;
  1003. }
  1004. #ifdef CONFIG_NET_POLL_CONTROLLER
  1005. static void
  1006. xemaclite_poll_controller(struct net_device *ndev)
  1007. {
  1008. disable_irq(ndev->irq);
  1009. xemaclite_interrupt(ndev->irq, ndev);
  1010. enable_irq(ndev->irq);
  1011. }
  1012. #endif
  1013. static const struct net_device_ops xemaclite_netdev_ops = {
  1014. .ndo_open = xemaclite_open,
  1015. .ndo_stop = xemaclite_close,
  1016. .ndo_start_xmit = xemaclite_send,
  1017. .ndo_set_mac_address = xemaclite_set_mac_address,
  1018. .ndo_tx_timeout = xemaclite_tx_timeout,
  1019. #ifdef CONFIG_NET_POLL_CONTROLLER
  1020. .ndo_poll_controller = xemaclite_poll_controller,
  1021. #endif
  1022. };
  1023. /* Match table for OF platform binding */
  1024. static const struct of_device_id xemaclite_of_match[] = {
  1025. { .compatible = "xlnx,opb-ethernetlite-1.01.a", },
  1026. { .compatible = "xlnx,opb-ethernetlite-1.01.b", },
  1027. { .compatible = "xlnx,xps-ethernetlite-1.00.a", },
  1028. { .compatible = "xlnx,xps-ethernetlite-2.00.a", },
  1029. { .compatible = "xlnx,xps-ethernetlite-2.01.a", },
  1030. { .compatible = "xlnx,xps-ethernetlite-3.00.a", },
  1031. { /* end of list */ },
  1032. };
  1033. MODULE_DEVICE_TABLE(of, xemaclite_of_match);
  1034. static struct platform_driver xemaclite_of_driver = {
  1035. .driver = {
  1036. .name = DRIVER_NAME,
  1037. .of_match_table = xemaclite_of_match,
  1038. },
  1039. .probe = xemaclite_of_probe,
  1040. .remove = xemaclite_of_remove,
  1041. };
  1042. module_platform_driver(xemaclite_of_driver);
  1043. MODULE_AUTHOR("Xilinx, Inc.");
  1044. MODULE_DESCRIPTION("Xilinx Ethernet MAC Lite driver");
  1045. MODULE_LICENSE("GPL");