xilinx_emaclite.c 37 KB

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