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