xilinx_emaclite.c 38 KB

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