xilinx_emaclite.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. dev->last_rx = jiffies;
  490. netif_rx(skb); /* Send the packet upstream */
  491. }
  492. /**
  493. * xemaclite_interrupt - Interrupt handler for this driver
  494. * @irq: Irq of the Emaclite device
  495. * @dev_id: Void pointer to the network device instance used as callback
  496. * reference
  497. *
  498. * This function handles the Tx and Rx interrupts of the EmacLite device.
  499. */
  500. static irqreturn_t xemaclite_interrupt(int irq, void *dev_id)
  501. {
  502. bool tx_complete = 0;
  503. struct net_device *dev = dev_id;
  504. struct net_local *lp = (struct net_local *) netdev_priv(dev);
  505. void __iomem *base_addr = lp->base_addr;
  506. u32 tx_status;
  507. /* Check if there is Rx Data available */
  508. if ((in_be32(base_addr + XEL_RSR_OFFSET) & XEL_RSR_RECV_DONE_MASK) ||
  509. (in_be32(base_addr + XEL_BUFFER_OFFSET + XEL_RSR_OFFSET)
  510. & XEL_RSR_RECV_DONE_MASK))
  511. xemaclite_rx_handler(dev);
  512. /* Check if the Transmission for the first buffer is completed */
  513. tx_status = in_be32(base_addr + XEL_TSR_OFFSET);
  514. if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
  515. (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
  516. tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
  517. out_be32(base_addr + XEL_TSR_OFFSET, tx_status);
  518. tx_complete = 1;
  519. }
  520. /* Check if the Transmission for the second buffer is completed */
  521. tx_status = in_be32(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
  522. if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
  523. (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
  524. tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
  525. out_be32(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET,
  526. tx_status);
  527. tx_complete = 1;
  528. }
  529. /* If there was a Tx interrupt, call the Tx Handler */
  530. if (tx_complete != 0)
  531. xemaclite_tx_handler(dev);
  532. return IRQ_HANDLED;
  533. }
  534. /**
  535. * xemaclite_open - Open the network device
  536. * @dev: Pointer to the network device
  537. *
  538. * This function sets the MAC address, requests an IRQ and enables interrupts
  539. * for the Emaclite device and starts the Tx queue.
  540. */
  541. static int xemaclite_open(struct net_device *dev)
  542. {
  543. struct net_local *lp = (struct net_local *) netdev_priv(dev);
  544. int retval;
  545. /* Just to be safe, stop the device first */
  546. xemaclite_disable_interrupts(lp);
  547. /* Set the MAC address each time opened */
  548. xemaclite_set_mac_address(lp, dev->dev_addr);
  549. /* Grab the IRQ */
  550. retval = request_irq(dev->irq, &xemaclite_interrupt, 0, dev->name, dev);
  551. if (retval) {
  552. dev_err(&lp->ndev->dev, "Could not allocate interrupt %d\n",
  553. dev->irq);
  554. return retval;
  555. }
  556. /* Enable Interrupts */
  557. xemaclite_enable_interrupts(lp);
  558. /* We're ready to go */
  559. netif_start_queue(dev);
  560. return 0;
  561. }
  562. /**
  563. * xemaclite_close - Close the network device
  564. * @dev: Pointer to the network device
  565. *
  566. * This function stops the Tx queue, disables interrupts and frees the IRQ for
  567. * the Emaclite device.
  568. */
  569. static int xemaclite_close(struct net_device *dev)
  570. {
  571. struct net_local *lp = (struct net_local *) netdev_priv(dev);
  572. netif_stop_queue(dev);
  573. xemaclite_disable_interrupts(lp);
  574. free_irq(dev->irq, dev);
  575. return 0;
  576. }
  577. /**
  578. * xemaclite_get_stats - Get the stats for the net_device
  579. * @dev: Pointer to the network device
  580. *
  581. * This function returns the address of the 'net_device_stats' structure for the
  582. * given network device. This structure holds usage statistics for the network
  583. * device.
  584. *
  585. * Return: Pointer to the net_device_stats structure.
  586. */
  587. static struct net_device_stats *xemaclite_get_stats(struct net_device *dev)
  588. {
  589. return &dev->stats;
  590. }
  591. /**
  592. * xemaclite_send - Transmit a frame
  593. * @orig_skb: Pointer to the socket buffer to be transmitted
  594. * @dev: Pointer to the network device
  595. *
  596. * This function checks if the Tx buffer of the Emaclite device is free to send
  597. * data. If so, it fills the Tx buffer with data from socket buffer data,
  598. * updates the stats and frees the socket buffer. The Tx completion is signaled
  599. * by an interrupt. If the Tx buffer isn't free, then the socket buffer is
  600. * deferred and the Tx queue is stopped so that the deferred socket buffer can
  601. * be transmitted when the Emaclite device is free to transmit data.
  602. *
  603. * Return: 0, always.
  604. */
  605. static int xemaclite_send(struct sk_buff *orig_skb, struct net_device *dev)
  606. {
  607. struct net_local *lp = (struct net_local *) netdev_priv(dev);
  608. struct sk_buff *new_skb;
  609. unsigned int len;
  610. unsigned long flags;
  611. len = orig_skb->len;
  612. new_skb = orig_skb;
  613. spin_lock_irqsave(&lp->reset_lock, flags);
  614. if (xemaclite_send_data(lp, (u8 *) new_skb->data, len) != 0) {
  615. /* If the Emaclite Tx buffer is busy, stop the Tx queue and
  616. * defer the skb for transmission at a later point when the
  617. * current transmission is complete */
  618. netif_stop_queue(dev);
  619. lp->deferred_skb = new_skb;
  620. spin_unlock_irqrestore(&lp->reset_lock, flags);
  621. return 0;
  622. }
  623. spin_unlock_irqrestore(&lp->reset_lock, flags);
  624. dev->stats.tx_bytes += len;
  625. dev_kfree_skb(new_skb);
  626. dev->trans_start = jiffies;
  627. return 0;
  628. }
  629. /**
  630. * xemaclite_ioctl - Perform IO Control operations on the network device
  631. * @dev: Pointer to the network device
  632. * @rq: Pointer to the interface request structure
  633. * @cmd: IOCTL command
  634. *
  635. * The only IOCTL operation supported by this function is setting the MAC
  636. * address. An error is reported if any other operations are requested.
  637. *
  638. * Return: 0 to indicate success, or a negative error for failure.
  639. */
  640. static int xemaclite_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  641. {
  642. struct net_local *lp = (struct net_local *) netdev_priv(dev);
  643. struct hw_addr_data *hw_addr = (struct hw_addr_data *) &rq->ifr_hwaddr;
  644. switch (cmd) {
  645. case SIOCETHTOOL:
  646. return -EIO;
  647. case SIOCSIFHWADDR:
  648. dev_err(&lp->ndev->dev, "SIOCSIFHWADDR\n");
  649. /* Copy MAC address in from user space */
  650. copy_from_user((void __force *) dev->dev_addr,
  651. (void __user __force *) hw_addr,
  652. IFHWADDRLEN);
  653. xemaclite_set_mac_address(lp, dev->dev_addr);
  654. break;
  655. default:
  656. return -EOPNOTSUPP;
  657. }
  658. return 0;
  659. }
  660. /**
  661. * xemaclite_remove_ndev - Free the network device
  662. * @ndev: Pointer to the network device to be freed
  663. *
  664. * This function un maps the IO region of the Emaclite device and frees the net
  665. * device.
  666. */
  667. static void xemaclite_remove_ndev(struct net_device *ndev)
  668. {
  669. if (ndev) {
  670. struct net_local *lp = (struct net_local *) netdev_priv(ndev);
  671. if (lp->base_addr)
  672. iounmap((void __iomem __force *) (lp->base_addr));
  673. free_netdev(ndev);
  674. }
  675. }
  676. /**
  677. * get_bool - Get a parameter from the OF device
  678. * @ofdev: Pointer to OF device structure
  679. * @s: Property to be retrieved
  680. *
  681. * This function looks for a property in the device node and returns the value
  682. * of the property if its found or 0 if the property is not found.
  683. *
  684. * Return: Value of the parameter if the parameter is found, or 0 otherwise
  685. */
  686. static bool get_bool(struct of_device *ofdev, const char *s)
  687. {
  688. u32 *p = (u32 *)of_get_property(ofdev->node, s, NULL);
  689. if (p) {
  690. return (bool)*p;
  691. } else {
  692. dev_warn(&ofdev->dev, "Parameter %s not found,"
  693. "defaulting to false\n", s);
  694. return 0;
  695. }
  696. }
  697. static struct net_device_ops xemaclite_netdev_ops;
  698. /**
  699. * xemaclite_of_probe - Probe method for the Emaclite device.
  700. * @ofdev: Pointer to OF device structure
  701. * @match: Pointer to the structure used for matching a device
  702. *
  703. * This function probes for the Emaclite device in the device tree.
  704. * It initializes the driver data structure and the hardware, sets the MAC
  705. * address and registers the network device.
  706. *
  707. * Return: 0, if the driver is bound to the Emaclite device, or
  708. * a negative error if there is failure.
  709. */
  710. static int __devinit xemaclite_of_probe(struct of_device *ofdev,
  711. const struct of_device_id *match)
  712. {
  713. struct resource r_irq; /* Interrupt resources */
  714. struct resource r_mem; /* IO mem resources */
  715. struct net_device *ndev = NULL;
  716. struct net_local *lp = NULL;
  717. struct device *dev = &ofdev->dev;
  718. const void *mac_address;
  719. int rc = 0;
  720. dev_info(dev, "Device Tree Probing\n");
  721. /* Get iospace for the device */
  722. rc = of_address_to_resource(ofdev->node, 0, &r_mem);
  723. if (rc) {
  724. dev_err(dev, "invalid address\n");
  725. return rc;
  726. }
  727. /* Get IRQ for the device */
  728. rc = of_irq_to_resource(ofdev->node, 0, &r_irq);
  729. if (rc == NO_IRQ) {
  730. dev_err(dev, "no IRQ found\n");
  731. return rc;
  732. }
  733. /* Create an ethernet device instance */
  734. ndev = alloc_etherdev(sizeof(struct net_local));
  735. if (!ndev) {
  736. dev_err(dev, "Could not allocate network device\n");
  737. return -ENOMEM;
  738. }
  739. dev_set_drvdata(dev, ndev);
  740. ndev->irq = r_irq.start;
  741. ndev->mem_start = r_mem.start;
  742. ndev->mem_end = r_mem.end;
  743. lp = netdev_priv(ndev);
  744. lp->ndev = ndev;
  745. if (!request_mem_region(ndev->mem_start,
  746. ndev->mem_end - ndev->mem_start + 1,
  747. DRIVER_NAME)) {
  748. dev_err(dev, "Couldn't lock memory region at %p\n",
  749. (void *)ndev->mem_start);
  750. rc = -EBUSY;
  751. goto error2;
  752. }
  753. /* Get the virtual base address for the device */
  754. lp->base_addr = ioremap(r_mem.start, r_mem.end - r_mem.start + 1);
  755. if (NULL == lp->base_addr) {
  756. dev_err(dev, "EmacLite: Could not allocate iomem\n");
  757. rc = -EIO;
  758. goto error1;
  759. }
  760. spin_lock_init(&lp->reset_lock);
  761. lp->next_tx_buf_to_use = 0x0;
  762. lp->next_rx_buf_to_use = 0x0;
  763. lp->tx_ping_pong = get_bool(ofdev, "xlnx,tx-ping-pong");
  764. lp->rx_ping_pong = get_bool(ofdev, "xlnx,rx-ping-pong");
  765. mac_address = of_get_mac_address(ofdev->node);
  766. if (mac_address)
  767. /* Set the MAC address. */
  768. memcpy(ndev->dev_addr, mac_address, 6);
  769. else
  770. dev_warn(dev, "No MAC address found\n");
  771. /* Clear the Tx CSR's in case this is a restart */
  772. out_be32(lp->base_addr + XEL_TSR_OFFSET, 0);
  773. out_be32(lp->base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET, 0);
  774. /* Set the MAC address in the EmacLite device */
  775. xemaclite_set_mac_address(lp, ndev->dev_addr);
  776. dev_info(dev,
  777. "MAC address is now %2x:%2x:%2x:%2x:%2x:%2x\n",
  778. ndev->dev_addr[0], ndev->dev_addr[1],
  779. ndev->dev_addr[2], ndev->dev_addr[3],
  780. ndev->dev_addr[4], ndev->dev_addr[5]);
  781. ndev->netdev_ops = &xemaclite_netdev_ops;
  782. ndev->flags &= ~IFF_MULTICAST;
  783. ndev->watchdog_timeo = TX_TIMEOUT;
  784. /* Finally, register the device */
  785. rc = register_netdev(ndev);
  786. if (rc) {
  787. dev_err(dev,
  788. "Cannot register network device, aborting\n");
  789. goto error1;
  790. }
  791. dev_info(dev,
  792. "Xilinx EmacLite at 0x%08X mapped to 0x%08X, irq=%d\n",
  793. (unsigned int __force)ndev->mem_start,
  794. (unsigned int __force)lp->base_addr, ndev->irq);
  795. return 0;
  796. error1:
  797. release_mem_region(ndev->mem_start, r_mem.end - r_mem.start + 1);
  798. error2:
  799. xemaclite_remove_ndev(ndev);
  800. return rc;
  801. }
  802. /**
  803. * xemaclite_of_remove - Unbind the driver from the Emaclite device.
  804. * @of_dev: Pointer to OF device structure
  805. *
  806. * This function is called if a device is physically removed from the system or
  807. * if the driver module is being unloaded. It frees any resources allocated to
  808. * the device.
  809. *
  810. * Return: 0, always.
  811. */
  812. static int __devexit xemaclite_of_remove(struct of_device *of_dev)
  813. {
  814. struct device *dev = &of_dev->dev;
  815. struct net_device *ndev = dev_get_drvdata(dev);
  816. unregister_netdev(ndev);
  817. release_mem_region(ndev->mem_start, ndev->mem_end-ndev->mem_start + 1);
  818. xemaclite_remove_ndev(ndev);
  819. dev_set_drvdata(dev, NULL);
  820. return 0;
  821. }
  822. static struct net_device_ops xemaclite_netdev_ops = {
  823. .ndo_open = xemaclite_open,
  824. .ndo_stop = xemaclite_close,
  825. .ndo_start_xmit = xemaclite_send,
  826. .ndo_do_ioctl = xemaclite_ioctl,
  827. .ndo_tx_timeout = xemaclite_tx_timeout,
  828. .ndo_get_stats = xemaclite_get_stats,
  829. };
  830. /* Match table for OF platform binding */
  831. static struct of_device_id xemaclite_of_match[] __devinitdata = {
  832. { .compatible = "xlnx,opb-ethernetlite-1.01.a", },
  833. { .compatible = "xlnx,opb-ethernetlite-1.01.b", },
  834. { .compatible = "xlnx,xps-ethernetlite-1.00.a", },
  835. { .compatible = "xlnx,xps-ethernetlite-2.00.a", },
  836. { .compatible = "xlnx,xps-ethernetlite-2.01.a", },
  837. { /* end of list */ },
  838. };
  839. MODULE_DEVICE_TABLE(of, xemaclite_of_match);
  840. static struct of_platform_driver xemaclite_of_driver = {
  841. .name = DRIVER_NAME,
  842. .match_table = xemaclite_of_match,
  843. .probe = xemaclite_of_probe,
  844. .remove = __devexit_p(xemaclite_of_remove),
  845. };
  846. /**
  847. * xgpiopss_init - Initial driver registration call
  848. *
  849. * Return: 0 upon success, or a negative error upon failure.
  850. */
  851. static int __init xemaclite_init(void)
  852. {
  853. /* No kernel boot options used, we just need to register the driver */
  854. return of_register_platform_driver(&xemaclite_of_driver);
  855. }
  856. /**
  857. * xemaclite_cleanup - Driver un-registration call
  858. */
  859. static void __exit xemaclite_cleanup(void)
  860. {
  861. of_unregister_platform_driver(&xemaclite_of_driver);
  862. }
  863. module_init(xemaclite_init);
  864. module_exit(xemaclite_cleanup);
  865. MODULE_AUTHOR("Xilinx, Inc.");
  866. MODULE_DESCRIPTION("Xilinx Ethernet MAC Lite driver");
  867. MODULE_LICENSE("GPL");