xilinx_emaclite.c 30 KB

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