xilinx_emaclite.c 36 KB

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