xilinx_emaclite.c 36 KB

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