xilinx_emaclite.c 38 KB

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