xilinx_emaclite.c 30 KB

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