xilinx_emaclite.c 38 KB

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