pxa168_eth.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  1. /*
  2. * PXA168 ethernet driver.
  3. * Most of the code is derived from mv643xx ethernet driver.
  4. *
  5. * Copyright (C) 2010 Marvell International Ltd.
  6. * Sachin Sanap <ssanap@marvell.com>
  7. * Zhangfei Gao <zgao6@marvell.com>
  8. * Philip Rakity <prakity@marvell.com>
  9. * Mark Brown <markb@marvell.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include <linux/bitops.h>
  25. #include <linux/clk.h>
  26. #include <linux/delay.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/in.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/io.h>
  33. #include <linux/ip.h>
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/of.h>
  37. #include <linux/of_net.h>
  38. #include <linux/phy.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/pxa168_eth.h>
  41. #include <linux/tcp.h>
  42. #include <linux/types.h>
  43. #include <linux/udp.h>
  44. #include <linux/workqueue.h>
  45. #include <asm/pgtable.h>
  46. #include <asm/cacheflush.h>
  47. #define DRIVER_NAME "pxa168-eth"
  48. #define DRIVER_VERSION "0.3"
  49. /*
  50. * Registers
  51. */
  52. #define PHY_ADDRESS 0x0000
  53. #define SMI 0x0010
  54. #define PORT_CONFIG 0x0400
  55. #define PORT_CONFIG_EXT 0x0408
  56. #define PORT_COMMAND 0x0410
  57. #define PORT_STATUS 0x0418
  58. #define HTPR 0x0428
  59. #define MAC_ADDR_LOW 0x0430
  60. #define MAC_ADDR_HIGH 0x0438
  61. #define SDMA_CONFIG 0x0440
  62. #define SDMA_CMD 0x0448
  63. #define INT_CAUSE 0x0450
  64. #define INT_W_CLEAR 0x0454
  65. #define INT_MASK 0x0458
  66. #define ETH_F_RX_DESC_0 0x0480
  67. #define ETH_C_RX_DESC_0 0x04A0
  68. #define ETH_C_TX_DESC_1 0x04E4
  69. /* smi register */
  70. #define SMI_BUSY (1 << 28) /* 0 - Write, 1 - Read */
  71. #define SMI_R_VALID (1 << 27) /* 0 - Write, 1 - Read */
  72. #define SMI_OP_W (0 << 26) /* Write operation */
  73. #define SMI_OP_R (1 << 26) /* Read operation */
  74. #define PHY_WAIT_ITERATIONS 10
  75. #define PXA168_ETH_PHY_ADDR_DEFAULT 0
  76. /* RX & TX descriptor command */
  77. #define BUF_OWNED_BY_DMA (1 << 31)
  78. /* RX descriptor status */
  79. #define RX_EN_INT (1 << 23)
  80. #define RX_FIRST_DESC (1 << 17)
  81. #define RX_LAST_DESC (1 << 16)
  82. #define RX_ERROR (1 << 15)
  83. /* TX descriptor command */
  84. #define TX_EN_INT (1 << 23)
  85. #define TX_GEN_CRC (1 << 22)
  86. #define TX_ZERO_PADDING (1 << 18)
  87. #define TX_FIRST_DESC (1 << 17)
  88. #define TX_LAST_DESC (1 << 16)
  89. #define TX_ERROR (1 << 15)
  90. /* SDMA_CMD */
  91. #define SDMA_CMD_AT (1 << 31)
  92. #define SDMA_CMD_TXDL (1 << 24)
  93. #define SDMA_CMD_TXDH (1 << 23)
  94. #define SDMA_CMD_AR (1 << 15)
  95. #define SDMA_CMD_ERD (1 << 7)
  96. /* Bit definitions of the Port Config Reg */
  97. #define PCR_DUPLEX_FULL (1 << 15)
  98. #define PCR_HS (1 << 12)
  99. #define PCR_EN (1 << 7)
  100. #define PCR_PM (1 << 0)
  101. /* Bit definitions of the Port Config Extend Reg */
  102. #define PCXR_2BSM (1 << 28)
  103. #define PCXR_DSCP_EN (1 << 21)
  104. #define PCXR_RMII_EN (1 << 20)
  105. #define PCXR_AN_SPEED_DIS (1 << 19)
  106. #define PCXR_SPEED_100 (1 << 18)
  107. #define PCXR_MFL_1518 (0 << 14)
  108. #define PCXR_MFL_1536 (1 << 14)
  109. #define PCXR_MFL_2048 (2 << 14)
  110. #define PCXR_MFL_64K (3 << 14)
  111. #define PCXR_FLOWCTL_DIS (1 << 12)
  112. #define PCXR_FLP (1 << 11)
  113. #define PCXR_AN_FLOWCTL_DIS (1 << 10)
  114. #define PCXR_AN_DUPLEX_DIS (1 << 9)
  115. #define PCXR_PRIO_TX_OFF 3
  116. #define PCXR_TX_HIGH_PRI (7 << PCXR_PRIO_TX_OFF)
  117. /* Bit definitions of the SDMA Config Reg */
  118. #define SDCR_BSZ_OFF 12
  119. #define SDCR_BSZ8 (3 << SDCR_BSZ_OFF)
  120. #define SDCR_BSZ4 (2 << SDCR_BSZ_OFF)
  121. #define SDCR_BSZ2 (1 << SDCR_BSZ_OFF)
  122. #define SDCR_BSZ1 (0 << SDCR_BSZ_OFF)
  123. #define SDCR_BLMR (1 << 6)
  124. #define SDCR_BLMT (1 << 7)
  125. #define SDCR_RIFB (1 << 9)
  126. #define SDCR_RC_OFF 2
  127. #define SDCR_RC_MAX_RETRANS (0xf << SDCR_RC_OFF)
  128. /*
  129. * Bit definitions of the Interrupt Cause Reg
  130. * and Interrupt MASK Reg is the same
  131. */
  132. #define ICR_RXBUF (1 << 0)
  133. #define ICR_TXBUF_H (1 << 2)
  134. #define ICR_TXBUF_L (1 << 3)
  135. #define ICR_TXEND_H (1 << 6)
  136. #define ICR_TXEND_L (1 << 7)
  137. #define ICR_RXERR (1 << 8)
  138. #define ICR_TXERR_H (1 << 10)
  139. #define ICR_TXERR_L (1 << 11)
  140. #define ICR_TX_UDR (1 << 13)
  141. #define ICR_MII_CH (1 << 28)
  142. #define ALL_INTS (ICR_TXBUF_H | ICR_TXBUF_L | ICR_TX_UDR |\
  143. ICR_TXERR_H | ICR_TXERR_L |\
  144. ICR_TXEND_H | ICR_TXEND_L |\
  145. ICR_RXBUF | ICR_RXERR | ICR_MII_CH)
  146. #define ETH_HW_IP_ALIGN 2 /* hw aligns IP header */
  147. #define NUM_RX_DESCS 64
  148. #define NUM_TX_DESCS 64
  149. #define HASH_ADD 0
  150. #define HASH_DELETE 1
  151. #define HASH_ADDR_TABLE_SIZE 0x4000 /* 16K (1/2K address - PCR_HS == 1) */
  152. #define HOP_NUMBER 12
  153. /* Bit definitions for Port status */
  154. #define PORT_SPEED_100 (1 << 0)
  155. #define FULL_DUPLEX (1 << 1)
  156. #define FLOW_CONTROL_DISABLED (1 << 2)
  157. #define LINK_UP (1 << 3)
  158. /* Bit definitions for work to be done */
  159. #define WORK_TX_DONE (1 << 1)
  160. /*
  161. * Misc definitions.
  162. */
  163. #define SKB_DMA_REALIGN ((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
  164. struct rx_desc {
  165. u32 cmd_sts; /* Descriptor command status */
  166. u16 byte_cnt; /* Descriptor buffer byte count */
  167. u16 buf_size; /* Buffer size */
  168. u32 buf_ptr; /* Descriptor buffer pointer */
  169. u32 next_desc_ptr; /* Next descriptor pointer */
  170. };
  171. struct tx_desc {
  172. u32 cmd_sts; /* Command/status field */
  173. u16 reserved;
  174. u16 byte_cnt; /* buffer byte count */
  175. u32 buf_ptr; /* pointer to buffer for this descriptor */
  176. u32 next_desc_ptr; /* Pointer to next descriptor */
  177. };
  178. struct pxa168_eth_private {
  179. int port_num; /* User Ethernet port number */
  180. int phy_addr;
  181. int phy_speed;
  182. int phy_duplex;
  183. phy_interface_t phy_intf;
  184. int rx_resource_err; /* Rx ring resource error flag */
  185. /* Next available and first returning Rx resource */
  186. int rx_curr_desc_q, rx_used_desc_q;
  187. /* Next available and first returning Tx resource */
  188. int tx_curr_desc_q, tx_used_desc_q;
  189. struct rx_desc *p_rx_desc_area;
  190. dma_addr_t rx_desc_dma;
  191. int rx_desc_area_size;
  192. struct sk_buff **rx_skb;
  193. struct tx_desc *p_tx_desc_area;
  194. dma_addr_t tx_desc_dma;
  195. int tx_desc_area_size;
  196. struct sk_buff **tx_skb;
  197. struct work_struct tx_timeout_task;
  198. struct net_device *dev;
  199. struct napi_struct napi;
  200. u8 work_todo;
  201. int skb_size;
  202. /* Size of Tx Ring per queue */
  203. int tx_ring_size;
  204. /* Number of tx descriptors in use */
  205. int tx_desc_count;
  206. /* Size of Rx Ring per queue */
  207. int rx_ring_size;
  208. /* Number of rx descriptors in use */
  209. int rx_desc_count;
  210. /*
  211. * Used in case RX Ring is empty, which can occur when
  212. * system does not have resources (skb's)
  213. */
  214. struct timer_list timeout;
  215. struct mii_bus *smi_bus;
  216. struct phy_device *phy;
  217. /* clock */
  218. struct clk *clk;
  219. struct pxa168_eth_platform_data *pd;
  220. /*
  221. * Ethernet controller base address.
  222. */
  223. void __iomem *base;
  224. /* Pointer to the hardware address filter table */
  225. void *htpr;
  226. dma_addr_t htpr_dma;
  227. };
  228. struct addr_table_entry {
  229. __le32 lo;
  230. __le32 hi;
  231. };
  232. /* Bit fields of a Hash Table Entry */
  233. enum hash_table_entry {
  234. HASH_ENTRY_VALID = 1,
  235. SKIP = 2,
  236. HASH_ENTRY_RECEIVE_DISCARD = 4,
  237. HASH_ENTRY_RECEIVE_DISCARD_BIT = 2
  238. };
  239. static int pxa168_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
  240. static int pxa168_set_settings(struct net_device *dev, struct ethtool_cmd *cmd);
  241. static int pxa168_init_hw(struct pxa168_eth_private *pep);
  242. static int pxa168_init_phy(struct net_device *dev);
  243. static void eth_port_reset(struct net_device *dev);
  244. static void eth_port_start(struct net_device *dev);
  245. static int pxa168_eth_open(struct net_device *dev);
  246. static int pxa168_eth_stop(struct net_device *dev);
  247. static inline u32 rdl(struct pxa168_eth_private *pep, int offset)
  248. {
  249. return readl(pep->base + offset);
  250. }
  251. static inline void wrl(struct pxa168_eth_private *pep, int offset, u32 data)
  252. {
  253. writel(data, pep->base + offset);
  254. }
  255. static void abort_dma(struct pxa168_eth_private *pep)
  256. {
  257. int delay;
  258. int max_retries = 40;
  259. do {
  260. wrl(pep, SDMA_CMD, SDMA_CMD_AR | SDMA_CMD_AT);
  261. udelay(100);
  262. delay = 10;
  263. while ((rdl(pep, SDMA_CMD) & (SDMA_CMD_AR | SDMA_CMD_AT))
  264. && delay-- > 0) {
  265. udelay(10);
  266. }
  267. } while (max_retries-- > 0 && delay <= 0);
  268. if (max_retries <= 0)
  269. netdev_err(pep->dev, "%s : DMA Stuck\n", __func__);
  270. }
  271. static void rxq_refill(struct net_device *dev)
  272. {
  273. struct pxa168_eth_private *pep = netdev_priv(dev);
  274. struct sk_buff *skb;
  275. struct rx_desc *p_used_rx_desc;
  276. int used_rx_desc;
  277. while (pep->rx_desc_count < pep->rx_ring_size) {
  278. int size;
  279. skb = netdev_alloc_skb(dev, pep->skb_size);
  280. if (!skb)
  281. break;
  282. if (SKB_DMA_REALIGN)
  283. skb_reserve(skb, SKB_DMA_REALIGN);
  284. pep->rx_desc_count++;
  285. /* Get 'used' Rx descriptor */
  286. used_rx_desc = pep->rx_used_desc_q;
  287. p_used_rx_desc = &pep->p_rx_desc_area[used_rx_desc];
  288. size = skb_end_pointer(skb) - skb->data;
  289. p_used_rx_desc->buf_ptr = dma_map_single(NULL,
  290. skb->data,
  291. size,
  292. DMA_FROM_DEVICE);
  293. p_used_rx_desc->buf_size = size;
  294. pep->rx_skb[used_rx_desc] = skb;
  295. /* Return the descriptor to DMA ownership */
  296. wmb();
  297. p_used_rx_desc->cmd_sts = BUF_OWNED_BY_DMA | RX_EN_INT;
  298. wmb();
  299. /* Move the used descriptor pointer to the next descriptor */
  300. pep->rx_used_desc_q = (used_rx_desc + 1) % pep->rx_ring_size;
  301. /* Any Rx return cancels the Rx resource error status */
  302. pep->rx_resource_err = 0;
  303. skb_reserve(skb, ETH_HW_IP_ALIGN);
  304. }
  305. /*
  306. * If RX ring is empty of SKB, set a timer to try allocating
  307. * again at a later time.
  308. */
  309. if (pep->rx_desc_count == 0) {
  310. pep->timeout.expires = jiffies + (HZ / 10);
  311. add_timer(&pep->timeout);
  312. }
  313. }
  314. static inline void rxq_refill_timer_wrapper(unsigned long data)
  315. {
  316. struct pxa168_eth_private *pep = (void *)data;
  317. napi_schedule(&pep->napi);
  318. }
  319. static inline u8 flip_8_bits(u8 x)
  320. {
  321. return (((x) & 0x01) << 3) | (((x) & 0x02) << 1)
  322. | (((x) & 0x04) >> 1) | (((x) & 0x08) >> 3)
  323. | (((x) & 0x10) << 3) | (((x) & 0x20) << 1)
  324. | (((x) & 0x40) >> 1) | (((x) & 0x80) >> 3);
  325. }
  326. static void nibble_swap_every_byte(unsigned char *mac_addr)
  327. {
  328. int i;
  329. for (i = 0; i < ETH_ALEN; i++) {
  330. mac_addr[i] = ((mac_addr[i] & 0x0f) << 4) |
  331. ((mac_addr[i] & 0xf0) >> 4);
  332. }
  333. }
  334. static void inverse_every_nibble(unsigned char *mac_addr)
  335. {
  336. int i;
  337. for (i = 0; i < ETH_ALEN; i++)
  338. mac_addr[i] = flip_8_bits(mac_addr[i]);
  339. }
  340. /*
  341. * ----------------------------------------------------------------------------
  342. * This function will calculate the hash function of the address.
  343. * Inputs
  344. * mac_addr_orig - MAC address.
  345. * Outputs
  346. * return the calculated entry.
  347. */
  348. static u32 hash_function(unsigned char *mac_addr_orig)
  349. {
  350. u32 hash_result;
  351. u32 addr0;
  352. u32 addr1;
  353. u32 addr2;
  354. u32 addr3;
  355. unsigned char mac_addr[ETH_ALEN];
  356. /* Make a copy of MAC address since we are going to performe bit
  357. * operations on it
  358. */
  359. memcpy(mac_addr, mac_addr_orig, ETH_ALEN);
  360. nibble_swap_every_byte(mac_addr);
  361. inverse_every_nibble(mac_addr);
  362. addr0 = (mac_addr[5] >> 2) & 0x3f;
  363. addr1 = (mac_addr[5] & 0x03) | (((mac_addr[4] & 0x7f)) << 2);
  364. addr2 = ((mac_addr[4] & 0x80) >> 7) | mac_addr[3] << 1;
  365. addr3 = (mac_addr[2] & 0xff) | ((mac_addr[1] & 1) << 8);
  366. hash_result = (addr0 << 9) | (addr1 ^ addr2 ^ addr3);
  367. hash_result = hash_result & 0x07ff;
  368. return hash_result;
  369. }
  370. /*
  371. * ----------------------------------------------------------------------------
  372. * This function will add/del an entry to the address table.
  373. * Inputs
  374. * pep - ETHERNET .
  375. * mac_addr - MAC address.
  376. * skip - if 1, skip this address.Used in case of deleting an entry which is a
  377. * part of chain in the hash table.We can't just delete the entry since
  378. * that will break the chain.We need to defragment the tables time to
  379. * time.
  380. * rd - 0 Discard packet upon match.
  381. * - 1 Receive packet upon match.
  382. * Outputs
  383. * address table entry is added/deleted.
  384. * 0 if success.
  385. * -ENOSPC if table full
  386. */
  387. static int add_del_hash_entry(struct pxa168_eth_private *pep,
  388. unsigned char *mac_addr,
  389. u32 rd, u32 skip, int del)
  390. {
  391. struct addr_table_entry *entry, *start;
  392. u32 new_high;
  393. u32 new_low;
  394. u32 i;
  395. new_low = (((mac_addr[1] >> 4) & 0xf) << 15)
  396. | (((mac_addr[1] >> 0) & 0xf) << 11)
  397. | (((mac_addr[0] >> 4) & 0xf) << 7)
  398. | (((mac_addr[0] >> 0) & 0xf) << 3)
  399. | (((mac_addr[3] >> 4) & 0x1) << 31)
  400. | (((mac_addr[3] >> 0) & 0xf) << 27)
  401. | (((mac_addr[2] >> 4) & 0xf) << 23)
  402. | (((mac_addr[2] >> 0) & 0xf) << 19)
  403. | (skip << SKIP) | (rd << HASH_ENTRY_RECEIVE_DISCARD_BIT)
  404. | HASH_ENTRY_VALID;
  405. new_high = (((mac_addr[5] >> 4) & 0xf) << 15)
  406. | (((mac_addr[5] >> 0) & 0xf) << 11)
  407. | (((mac_addr[4] >> 4) & 0xf) << 7)
  408. | (((mac_addr[4] >> 0) & 0xf) << 3)
  409. | (((mac_addr[3] >> 5) & 0x7) << 0);
  410. /*
  411. * Pick the appropriate table, start scanning for free/reusable
  412. * entries at the index obtained by hashing the specified MAC address
  413. */
  414. start = pep->htpr;
  415. entry = start + hash_function(mac_addr);
  416. for (i = 0; i < HOP_NUMBER; i++) {
  417. if (!(le32_to_cpu(entry->lo) & HASH_ENTRY_VALID)) {
  418. break;
  419. } else {
  420. /* if same address put in same position */
  421. if (((le32_to_cpu(entry->lo) & 0xfffffff8) ==
  422. (new_low & 0xfffffff8)) &&
  423. (le32_to_cpu(entry->hi) == new_high)) {
  424. break;
  425. }
  426. }
  427. if (entry == start + 0x7ff)
  428. entry = start;
  429. else
  430. entry++;
  431. }
  432. if (((le32_to_cpu(entry->lo) & 0xfffffff8) != (new_low & 0xfffffff8)) &&
  433. (le32_to_cpu(entry->hi) != new_high) && del)
  434. return 0;
  435. if (i == HOP_NUMBER) {
  436. if (!del) {
  437. netdev_info(pep->dev,
  438. "%s: table section is full, need to "
  439. "move to 16kB implementation?\n",
  440. __FILE__);
  441. return -ENOSPC;
  442. } else
  443. return 0;
  444. }
  445. /*
  446. * Update the selected entry
  447. */
  448. if (del) {
  449. entry->hi = 0;
  450. entry->lo = 0;
  451. } else {
  452. entry->hi = cpu_to_le32(new_high);
  453. entry->lo = cpu_to_le32(new_low);
  454. }
  455. return 0;
  456. }
  457. /*
  458. * ----------------------------------------------------------------------------
  459. * Create an addressTable entry from MAC address info
  460. * found in the specifed net_device struct
  461. *
  462. * Input : pointer to ethernet interface network device structure
  463. * Output : N/A
  464. */
  465. static void update_hash_table_mac_address(struct pxa168_eth_private *pep,
  466. unsigned char *oaddr,
  467. unsigned char *addr)
  468. {
  469. /* Delete old entry */
  470. if (oaddr)
  471. add_del_hash_entry(pep, oaddr, 1, 0, HASH_DELETE);
  472. /* Add new entry */
  473. add_del_hash_entry(pep, addr, 1, 0, HASH_ADD);
  474. }
  475. static int init_hash_table(struct pxa168_eth_private *pep)
  476. {
  477. /*
  478. * Hardware expects CPU to build a hash table based on a predefined
  479. * hash function and populate it based on hardware address. The
  480. * location of the hash table is identified by 32-bit pointer stored
  481. * in HTPR internal register. Two possible sizes exists for the hash
  482. * table 8kB (256kB of DRAM required (4 x 64 kB banks)) and 1/2kB
  483. * (16kB of DRAM required (4 x 4 kB banks)).We currently only support
  484. * 1/2kB.
  485. */
  486. /* TODO: Add support for 8kB hash table and alternative hash
  487. * function.Driver can dynamically switch to them if the 1/2kB hash
  488. * table is full.
  489. */
  490. if (pep->htpr == NULL) {
  491. pep->htpr = dma_zalloc_coherent(pep->dev->dev.parent,
  492. HASH_ADDR_TABLE_SIZE,
  493. &pep->htpr_dma, GFP_KERNEL);
  494. if (pep->htpr == NULL)
  495. return -ENOMEM;
  496. } else {
  497. memset(pep->htpr, 0, HASH_ADDR_TABLE_SIZE);
  498. }
  499. wrl(pep, HTPR, pep->htpr_dma);
  500. return 0;
  501. }
  502. static void pxa168_eth_set_rx_mode(struct net_device *dev)
  503. {
  504. struct pxa168_eth_private *pep = netdev_priv(dev);
  505. struct netdev_hw_addr *ha;
  506. u32 val;
  507. val = rdl(pep, PORT_CONFIG);
  508. if (dev->flags & IFF_PROMISC)
  509. val |= PCR_PM;
  510. else
  511. val &= ~PCR_PM;
  512. wrl(pep, PORT_CONFIG, val);
  513. /*
  514. * Remove the old list of MAC address and add dev->addr
  515. * and multicast address.
  516. */
  517. memset(pep->htpr, 0, HASH_ADDR_TABLE_SIZE);
  518. update_hash_table_mac_address(pep, NULL, dev->dev_addr);
  519. netdev_for_each_mc_addr(ha, dev)
  520. update_hash_table_mac_address(pep, NULL, ha->addr);
  521. }
  522. static void pxa168_eth_get_mac_address(struct net_device *dev,
  523. unsigned char *addr)
  524. {
  525. struct pxa168_eth_private *pep = netdev_priv(dev);
  526. unsigned int mac_h = rdl(pep, MAC_ADDR_HIGH);
  527. unsigned int mac_l = rdl(pep, MAC_ADDR_LOW);
  528. addr[0] = (mac_h >> 24) & 0xff;
  529. addr[1] = (mac_h >> 16) & 0xff;
  530. addr[2] = (mac_h >> 8) & 0xff;
  531. addr[3] = mac_h & 0xff;
  532. addr[4] = (mac_l >> 8) & 0xff;
  533. addr[5] = mac_l & 0xff;
  534. }
  535. static int pxa168_eth_set_mac_address(struct net_device *dev, void *addr)
  536. {
  537. struct sockaddr *sa = addr;
  538. struct pxa168_eth_private *pep = netdev_priv(dev);
  539. unsigned char oldMac[ETH_ALEN];
  540. u32 mac_h, mac_l;
  541. if (!is_valid_ether_addr(sa->sa_data))
  542. return -EADDRNOTAVAIL;
  543. memcpy(oldMac, dev->dev_addr, ETH_ALEN);
  544. memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
  545. mac_h = dev->dev_addr[0] << 24;
  546. mac_h |= dev->dev_addr[1] << 16;
  547. mac_h |= dev->dev_addr[2] << 8;
  548. mac_h |= dev->dev_addr[3];
  549. mac_l = dev->dev_addr[4] << 8;
  550. mac_l |= dev->dev_addr[5];
  551. wrl(pep, MAC_ADDR_HIGH, mac_h);
  552. wrl(pep, MAC_ADDR_LOW, mac_l);
  553. netif_addr_lock_bh(dev);
  554. update_hash_table_mac_address(pep, oldMac, dev->dev_addr);
  555. netif_addr_unlock_bh(dev);
  556. return 0;
  557. }
  558. static void eth_port_start(struct net_device *dev)
  559. {
  560. unsigned int val = 0;
  561. struct pxa168_eth_private *pep = netdev_priv(dev);
  562. int tx_curr_desc, rx_curr_desc;
  563. phy_start(pep->phy);
  564. /* Assignment of Tx CTRP of given queue */
  565. tx_curr_desc = pep->tx_curr_desc_q;
  566. wrl(pep, ETH_C_TX_DESC_1,
  567. (u32) (pep->tx_desc_dma + tx_curr_desc * sizeof(struct tx_desc)));
  568. /* Assignment of Rx CRDP of given queue */
  569. rx_curr_desc = pep->rx_curr_desc_q;
  570. wrl(pep, ETH_C_RX_DESC_0,
  571. (u32) (pep->rx_desc_dma + rx_curr_desc * sizeof(struct rx_desc)));
  572. wrl(pep, ETH_F_RX_DESC_0,
  573. (u32) (pep->rx_desc_dma + rx_curr_desc * sizeof(struct rx_desc)));
  574. /* Clear all interrupts */
  575. wrl(pep, INT_CAUSE, 0);
  576. /* Enable all interrupts for receive, transmit and error. */
  577. wrl(pep, INT_MASK, ALL_INTS);
  578. val = rdl(pep, PORT_CONFIG);
  579. val |= PCR_EN;
  580. wrl(pep, PORT_CONFIG, val);
  581. /* Start RX DMA engine */
  582. val = rdl(pep, SDMA_CMD);
  583. val |= SDMA_CMD_ERD;
  584. wrl(pep, SDMA_CMD, val);
  585. }
  586. static void eth_port_reset(struct net_device *dev)
  587. {
  588. struct pxa168_eth_private *pep = netdev_priv(dev);
  589. unsigned int val = 0;
  590. /* Stop all interrupts for receive, transmit and error. */
  591. wrl(pep, INT_MASK, 0);
  592. /* Clear all interrupts */
  593. wrl(pep, INT_CAUSE, 0);
  594. /* Stop RX DMA */
  595. val = rdl(pep, SDMA_CMD);
  596. val &= ~SDMA_CMD_ERD; /* abort dma command */
  597. /* Abort any transmit and receive operations and put DMA
  598. * in idle state.
  599. */
  600. abort_dma(pep);
  601. /* Disable port */
  602. val = rdl(pep, PORT_CONFIG);
  603. val &= ~PCR_EN;
  604. wrl(pep, PORT_CONFIG, val);
  605. phy_stop(pep->phy);
  606. }
  607. /*
  608. * txq_reclaim - Free the tx desc data for completed descriptors
  609. * If force is non-zero, frees uncompleted descriptors as well
  610. */
  611. static int txq_reclaim(struct net_device *dev, int force)
  612. {
  613. struct pxa168_eth_private *pep = netdev_priv(dev);
  614. struct tx_desc *desc;
  615. u32 cmd_sts;
  616. struct sk_buff *skb;
  617. int tx_index;
  618. dma_addr_t addr;
  619. int count;
  620. int released = 0;
  621. netif_tx_lock(dev);
  622. pep->work_todo &= ~WORK_TX_DONE;
  623. while (pep->tx_desc_count > 0) {
  624. tx_index = pep->tx_used_desc_q;
  625. desc = &pep->p_tx_desc_area[tx_index];
  626. cmd_sts = desc->cmd_sts;
  627. if (!force && (cmd_sts & BUF_OWNED_BY_DMA)) {
  628. if (released > 0) {
  629. goto txq_reclaim_end;
  630. } else {
  631. released = -1;
  632. goto txq_reclaim_end;
  633. }
  634. }
  635. pep->tx_used_desc_q = (tx_index + 1) % pep->tx_ring_size;
  636. pep->tx_desc_count--;
  637. addr = desc->buf_ptr;
  638. count = desc->byte_cnt;
  639. skb = pep->tx_skb[tx_index];
  640. if (skb)
  641. pep->tx_skb[tx_index] = NULL;
  642. if (cmd_sts & TX_ERROR) {
  643. if (net_ratelimit())
  644. netdev_err(dev, "Error in TX\n");
  645. dev->stats.tx_errors++;
  646. }
  647. dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
  648. if (skb)
  649. dev_kfree_skb_irq(skb);
  650. released++;
  651. }
  652. txq_reclaim_end:
  653. netif_tx_unlock(dev);
  654. return released;
  655. }
  656. static void pxa168_eth_tx_timeout(struct net_device *dev)
  657. {
  658. struct pxa168_eth_private *pep = netdev_priv(dev);
  659. netdev_info(dev, "TX timeout desc_count %d\n", pep->tx_desc_count);
  660. schedule_work(&pep->tx_timeout_task);
  661. }
  662. static void pxa168_eth_tx_timeout_task(struct work_struct *work)
  663. {
  664. struct pxa168_eth_private *pep = container_of(work,
  665. struct pxa168_eth_private,
  666. tx_timeout_task);
  667. struct net_device *dev = pep->dev;
  668. pxa168_eth_stop(dev);
  669. pxa168_eth_open(dev);
  670. }
  671. static int rxq_process(struct net_device *dev, int budget)
  672. {
  673. struct pxa168_eth_private *pep = netdev_priv(dev);
  674. struct net_device_stats *stats = &dev->stats;
  675. unsigned int received_packets = 0;
  676. struct sk_buff *skb;
  677. while (budget-- > 0) {
  678. int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
  679. struct rx_desc *rx_desc;
  680. unsigned int cmd_sts;
  681. /* Do not process Rx ring in case of Rx ring resource error */
  682. if (pep->rx_resource_err)
  683. break;
  684. rx_curr_desc = pep->rx_curr_desc_q;
  685. rx_used_desc = pep->rx_used_desc_q;
  686. rx_desc = &pep->p_rx_desc_area[rx_curr_desc];
  687. cmd_sts = rx_desc->cmd_sts;
  688. rmb();
  689. if (cmd_sts & (BUF_OWNED_BY_DMA))
  690. break;
  691. skb = pep->rx_skb[rx_curr_desc];
  692. pep->rx_skb[rx_curr_desc] = NULL;
  693. rx_next_curr_desc = (rx_curr_desc + 1) % pep->rx_ring_size;
  694. pep->rx_curr_desc_q = rx_next_curr_desc;
  695. /* Rx descriptors exhausted. */
  696. /* Set the Rx ring resource error flag */
  697. if (rx_next_curr_desc == rx_used_desc)
  698. pep->rx_resource_err = 1;
  699. pep->rx_desc_count--;
  700. dma_unmap_single(NULL, rx_desc->buf_ptr,
  701. rx_desc->buf_size,
  702. DMA_FROM_DEVICE);
  703. received_packets++;
  704. /*
  705. * Update statistics.
  706. * Note byte count includes 4 byte CRC count
  707. */
  708. stats->rx_packets++;
  709. stats->rx_bytes += rx_desc->byte_cnt;
  710. /*
  711. * In case received a packet without first / last bits on OR
  712. * the error summary bit is on, the packets needs to be droped.
  713. */
  714. if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
  715. (RX_FIRST_DESC | RX_LAST_DESC))
  716. || (cmd_sts & RX_ERROR)) {
  717. stats->rx_dropped++;
  718. if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
  719. (RX_FIRST_DESC | RX_LAST_DESC)) {
  720. if (net_ratelimit())
  721. netdev_err(dev,
  722. "Rx pkt on multiple desc\n");
  723. }
  724. if (cmd_sts & RX_ERROR)
  725. stats->rx_errors++;
  726. dev_kfree_skb_irq(skb);
  727. } else {
  728. /*
  729. * The -4 is for the CRC in the trailer of the
  730. * received packet
  731. */
  732. skb_put(skb, rx_desc->byte_cnt - 4);
  733. skb->protocol = eth_type_trans(skb, dev);
  734. netif_receive_skb(skb);
  735. }
  736. }
  737. /* Fill RX ring with skb's */
  738. rxq_refill(dev);
  739. return received_packets;
  740. }
  741. static int pxa168_eth_collect_events(struct pxa168_eth_private *pep,
  742. struct net_device *dev)
  743. {
  744. u32 icr;
  745. int ret = 0;
  746. icr = rdl(pep, INT_CAUSE);
  747. if (icr == 0)
  748. return IRQ_NONE;
  749. wrl(pep, INT_CAUSE, ~icr);
  750. if (icr & (ICR_TXBUF_H | ICR_TXBUF_L)) {
  751. pep->work_todo |= WORK_TX_DONE;
  752. ret = 1;
  753. }
  754. if (icr & ICR_RXBUF)
  755. ret = 1;
  756. return ret;
  757. }
  758. static irqreturn_t pxa168_eth_int_handler(int irq, void *dev_id)
  759. {
  760. struct net_device *dev = (struct net_device *)dev_id;
  761. struct pxa168_eth_private *pep = netdev_priv(dev);
  762. if (unlikely(!pxa168_eth_collect_events(pep, dev)))
  763. return IRQ_NONE;
  764. /* Disable interrupts */
  765. wrl(pep, INT_MASK, 0);
  766. napi_schedule(&pep->napi);
  767. return IRQ_HANDLED;
  768. }
  769. static void pxa168_eth_recalc_skb_size(struct pxa168_eth_private *pep)
  770. {
  771. int skb_size;
  772. /*
  773. * Reserve 2+14 bytes for an ethernet header (the hardware
  774. * automatically prepends 2 bytes of dummy data to each
  775. * received packet), 16 bytes for up to four VLAN tags, and
  776. * 4 bytes for the trailing FCS -- 36 bytes total.
  777. */
  778. skb_size = pep->dev->mtu + 36;
  779. /*
  780. * Make sure that the skb size is a multiple of 8 bytes, as
  781. * the lower three bits of the receive descriptor's buffer
  782. * size field are ignored by the hardware.
  783. */
  784. pep->skb_size = (skb_size + 7) & ~7;
  785. /*
  786. * If NET_SKB_PAD is smaller than a cache line,
  787. * netdev_alloc_skb() will cause skb->data to be misaligned
  788. * to a cache line boundary. If this is the case, include
  789. * some extra space to allow re-aligning the data area.
  790. */
  791. pep->skb_size += SKB_DMA_REALIGN;
  792. }
  793. static int set_port_config_ext(struct pxa168_eth_private *pep)
  794. {
  795. int skb_size;
  796. pxa168_eth_recalc_skb_size(pep);
  797. if (pep->skb_size <= 1518)
  798. skb_size = PCXR_MFL_1518;
  799. else if (pep->skb_size <= 1536)
  800. skb_size = PCXR_MFL_1536;
  801. else if (pep->skb_size <= 2048)
  802. skb_size = PCXR_MFL_2048;
  803. else
  804. skb_size = PCXR_MFL_64K;
  805. /* Extended Port Configuration */
  806. wrl(pep, PORT_CONFIG_EXT,
  807. PCXR_AN_SPEED_DIS | /* Disable HW AN */
  808. PCXR_AN_DUPLEX_DIS |
  809. PCXR_AN_FLOWCTL_DIS |
  810. PCXR_2BSM | /* Two byte prefix aligns IP hdr */
  811. PCXR_DSCP_EN | /* Enable DSCP in IP */
  812. skb_size | PCXR_FLP | /* do not force link pass */
  813. PCXR_TX_HIGH_PRI); /* Transmit - high priority queue */
  814. return 0;
  815. }
  816. static void pxa168_eth_adjust_link(struct net_device *dev)
  817. {
  818. struct pxa168_eth_private *pep = netdev_priv(dev);
  819. struct phy_device *phy = pep->phy;
  820. u32 cfg, cfg_o = rdl(pep, PORT_CONFIG);
  821. u32 cfgext, cfgext_o = rdl(pep, PORT_CONFIG_EXT);
  822. cfg = cfg_o & ~PCR_DUPLEX_FULL;
  823. cfgext = cfgext_o & ~(PCXR_SPEED_100 | PCXR_FLOWCTL_DIS | PCXR_RMII_EN);
  824. if (phy->interface == PHY_INTERFACE_MODE_RMII)
  825. cfgext |= PCXR_RMII_EN;
  826. if (phy->speed == SPEED_100)
  827. cfgext |= PCXR_SPEED_100;
  828. if (phy->duplex)
  829. cfg |= PCR_DUPLEX_FULL;
  830. if (!phy->pause)
  831. cfgext |= PCXR_FLOWCTL_DIS;
  832. /* Bail out if there has nothing changed */
  833. if (cfg == cfg_o && cfgext == cfgext_o)
  834. return;
  835. wrl(pep, PORT_CONFIG, cfg);
  836. wrl(pep, PORT_CONFIG_EXT, cfgext);
  837. phy_print_status(phy);
  838. }
  839. static int pxa168_init_phy(struct net_device *dev)
  840. {
  841. struct pxa168_eth_private *pep = netdev_priv(dev);
  842. struct ethtool_cmd cmd;
  843. int err;
  844. if (pep->phy)
  845. return 0;
  846. pep->phy = mdiobus_scan(pep->smi_bus, pep->phy_addr);
  847. if (!pep->phy)
  848. return -ENODEV;
  849. err = phy_connect_direct(dev, pep->phy, pxa168_eth_adjust_link,
  850. pep->phy_intf);
  851. if (err)
  852. return err;
  853. err = pxa168_get_settings(dev, &cmd);
  854. if (err)
  855. return err;
  856. cmd.phy_address = pep->phy_addr;
  857. cmd.speed = pep->phy_speed;
  858. cmd.duplex = pep->phy_duplex;
  859. cmd.advertising = PHY_BASIC_FEATURES;
  860. cmd.autoneg = AUTONEG_ENABLE;
  861. if (cmd.speed != 0)
  862. cmd.autoneg = AUTONEG_DISABLE;
  863. return pxa168_set_settings(dev, &cmd);
  864. }
  865. static int pxa168_init_hw(struct pxa168_eth_private *pep)
  866. {
  867. int err = 0;
  868. /* Disable interrupts */
  869. wrl(pep, INT_MASK, 0);
  870. wrl(pep, INT_CAUSE, 0);
  871. /* Write to ICR to clear interrupts. */
  872. wrl(pep, INT_W_CLEAR, 0);
  873. /* Abort any transmit and receive operations and put DMA
  874. * in idle state.
  875. */
  876. abort_dma(pep);
  877. /* Initialize address hash table */
  878. err = init_hash_table(pep);
  879. if (err)
  880. return err;
  881. /* SDMA configuration */
  882. wrl(pep, SDMA_CONFIG, SDCR_BSZ8 | /* Burst size = 32 bytes */
  883. SDCR_RIFB | /* Rx interrupt on frame */
  884. SDCR_BLMT | /* Little endian transmit */
  885. SDCR_BLMR | /* Little endian receive */
  886. SDCR_RC_MAX_RETRANS); /* Max retransmit count */
  887. /* Port Configuration */
  888. wrl(pep, PORT_CONFIG, PCR_HS); /* Hash size is 1/2kb */
  889. set_port_config_ext(pep);
  890. return err;
  891. }
  892. static int rxq_init(struct net_device *dev)
  893. {
  894. struct pxa168_eth_private *pep = netdev_priv(dev);
  895. struct rx_desc *p_rx_desc;
  896. int size = 0, i = 0;
  897. int rx_desc_num = pep->rx_ring_size;
  898. /* Allocate RX skb rings */
  899. pep->rx_skb = kzalloc(sizeof(*pep->rx_skb) * pep->rx_ring_size,
  900. GFP_KERNEL);
  901. if (!pep->rx_skb)
  902. return -ENOMEM;
  903. /* Allocate RX ring */
  904. pep->rx_desc_count = 0;
  905. size = pep->rx_ring_size * sizeof(struct rx_desc);
  906. pep->rx_desc_area_size = size;
  907. pep->p_rx_desc_area = dma_zalloc_coherent(pep->dev->dev.parent, size,
  908. &pep->rx_desc_dma,
  909. GFP_KERNEL);
  910. if (!pep->p_rx_desc_area)
  911. goto out;
  912. /* initialize the next_desc_ptr links in the Rx descriptors ring */
  913. p_rx_desc = pep->p_rx_desc_area;
  914. for (i = 0; i < rx_desc_num; i++) {
  915. p_rx_desc[i].next_desc_ptr = pep->rx_desc_dma +
  916. ((i + 1) % rx_desc_num) * sizeof(struct rx_desc);
  917. }
  918. /* Save Rx desc pointer to driver struct. */
  919. pep->rx_curr_desc_q = 0;
  920. pep->rx_used_desc_q = 0;
  921. pep->rx_desc_area_size = rx_desc_num * sizeof(struct rx_desc);
  922. return 0;
  923. out:
  924. kfree(pep->rx_skb);
  925. return -ENOMEM;
  926. }
  927. static void rxq_deinit(struct net_device *dev)
  928. {
  929. struct pxa168_eth_private *pep = netdev_priv(dev);
  930. int curr;
  931. /* Free preallocated skb's on RX rings */
  932. for (curr = 0; pep->rx_desc_count && curr < pep->rx_ring_size; curr++) {
  933. if (pep->rx_skb[curr]) {
  934. dev_kfree_skb(pep->rx_skb[curr]);
  935. pep->rx_desc_count--;
  936. }
  937. }
  938. if (pep->rx_desc_count)
  939. netdev_err(dev, "Error in freeing Rx Ring. %d skb's still\n",
  940. pep->rx_desc_count);
  941. /* Free RX ring */
  942. if (pep->p_rx_desc_area)
  943. dma_free_coherent(pep->dev->dev.parent, pep->rx_desc_area_size,
  944. pep->p_rx_desc_area, pep->rx_desc_dma);
  945. kfree(pep->rx_skb);
  946. }
  947. static int txq_init(struct net_device *dev)
  948. {
  949. struct pxa168_eth_private *pep = netdev_priv(dev);
  950. struct tx_desc *p_tx_desc;
  951. int size = 0, i = 0;
  952. int tx_desc_num = pep->tx_ring_size;
  953. pep->tx_skb = kzalloc(sizeof(*pep->tx_skb) * pep->tx_ring_size,
  954. GFP_KERNEL);
  955. if (!pep->tx_skb)
  956. return -ENOMEM;
  957. /* Allocate TX ring */
  958. pep->tx_desc_count = 0;
  959. size = pep->tx_ring_size * sizeof(struct tx_desc);
  960. pep->tx_desc_area_size = size;
  961. pep->p_tx_desc_area = dma_zalloc_coherent(pep->dev->dev.parent, size,
  962. &pep->tx_desc_dma,
  963. GFP_KERNEL);
  964. if (!pep->p_tx_desc_area)
  965. goto out;
  966. /* Initialize the next_desc_ptr links in the Tx descriptors ring */
  967. p_tx_desc = pep->p_tx_desc_area;
  968. for (i = 0; i < tx_desc_num; i++) {
  969. p_tx_desc[i].next_desc_ptr = pep->tx_desc_dma +
  970. ((i + 1) % tx_desc_num) * sizeof(struct tx_desc);
  971. }
  972. pep->tx_curr_desc_q = 0;
  973. pep->tx_used_desc_q = 0;
  974. pep->tx_desc_area_size = tx_desc_num * sizeof(struct tx_desc);
  975. return 0;
  976. out:
  977. kfree(pep->tx_skb);
  978. return -ENOMEM;
  979. }
  980. static void txq_deinit(struct net_device *dev)
  981. {
  982. struct pxa168_eth_private *pep = netdev_priv(dev);
  983. /* Free outstanding skb's on TX ring */
  984. txq_reclaim(dev, 1);
  985. BUG_ON(pep->tx_used_desc_q != pep->tx_curr_desc_q);
  986. /* Free TX ring */
  987. if (pep->p_tx_desc_area)
  988. dma_free_coherent(pep->dev->dev.parent, pep->tx_desc_area_size,
  989. pep->p_tx_desc_area, pep->tx_desc_dma);
  990. kfree(pep->tx_skb);
  991. }
  992. static int pxa168_eth_open(struct net_device *dev)
  993. {
  994. struct pxa168_eth_private *pep = netdev_priv(dev);
  995. int err;
  996. err = pxa168_init_phy(dev);
  997. if (err)
  998. return err;
  999. err = request_irq(dev->irq, pxa168_eth_int_handler, 0, dev->name, dev);
  1000. if (err) {
  1001. dev_err(&dev->dev, "can't assign irq\n");
  1002. return -EAGAIN;
  1003. }
  1004. pep->rx_resource_err = 0;
  1005. err = rxq_init(dev);
  1006. if (err != 0)
  1007. goto out_free_irq;
  1008. err = txq_init(dev);
  1009. if (err != 0)
  1010. goto out_free_rx_skb;
  1011. pep->rx_used_desc_q = 0;
  1012. pep->rx_curr_desc_q = 0;
  1013. /* Fill RX ring with skb's */
  1014. rxq_refill(dev);
  1015. pep->rx_used_desc_q = 0;
  1016. pep->rx_curr_desc_q = 0;
  1017. netif_carrier_off(dev);
  1018. napi_enable(&pep->napi);
  1019. eth_port_start(dev);
  1020. return 0;
  1021. out_free_rx_skb:
  1022. rxq_deinit(dev);
  1023. out_free_irq:
  1024. free_irq(dev->irq, dev);
  1025. return err;
  1026. }
  1027. static int pxa168_eth_stop(struct net_device *dev)
  1028. {
  1029. struct pxa168_eth_private *pep = netdev_priv(dev);
  1030. eth_port_reset(dev);
  1031. /* Disable interrupts */
  1032. wrl(pep, INT_MASK, 0);
  1033. wrl(pep, INT_CAUSE, 0);
  1034. /* Write to ICR to clear interrupts. */
  1035. wrl(pep, INT_W_CLEAR, 0);
  1036. napi_disable(&pep->napi);
  1037. del_timer_sync(&pep->timeout);
  1038. netif_carrier_off(dev);
  1039. free_irq(dev->irq, dev);
  1040. rxq_deinit(dev);
  1041. txq_deinit(dev);
  1042. return 0;
  1043. }
  1044. static int pxa168_eth_change_mtu(struct net_device *dev, int mtu)
  1045. {
  1046. int retval;
  1047. struct pxa168_eth_private *pep = netdev_priv(dev);
  1048. if ((mtu > 9500) || (mtu < 68))
  1049. return -EINVAL;
  1050. dev->mtu = mtu;
  1051. retval = set_port_config_ext(pep);
  1052. if (!netif_running(dev))
  1053. return 0;
  1054. /*
  1055. * Stop and then re-open the interface. This will allocate RX
  1056. * skbs of the new MTU.
  1057. * There is a possible danger that the open will not succeed,
  1058. * due to memory being full.
  1059. */
  1060. pxa168_eth_stop(dev);
  1061. if (pxa168_eth_open(dev)) {
  1062. dev_err(&dev->dev,
  1063. "fatal error on re-opening device after MTU change\n");
  1064. }
  1065. return 0;
  1066. }
  1067. static int eth_alloc_tx_desc_index(struct pxa168_eth_private *pep)
  1068. {
  1069. int tx_desc_curr;
  1070. tx_desc_curr = pep->tx_curr_desc_q;
  1071. pep->tx_curr_desc_q = (tx_desc_curr + 1) % pep->tx_ring_size;
  1072. BUG_ON(pep->tx_curr_desc_q == pep->tx_used_desc_q);
  1073. pep->tx_desc_count++;
  1074. return tx_desc_curr;
  1075. }
  1076. static int pxa168_rx_poll(struct napi_struct *napi, int budget)
  1077. {
  1078. struct pxa168_eth_private *pep =
  1079. container_of(napi, struct pxa168_eth_private, napi);
  1080. struct net_device *dev = pep->dev;
  1081. int work_done = 0;
  1082. /*
  1083. * We call txq_reclaim every time since in NAPI interupts are disabled
  1084. * and due to this we miss the TX_DONE interrupt,which is not updated in
  1085. * interrupt status register.
  1086. */
  1087. txq_reclaim(dev, 0);
  1088. if (netif_queue_stopped(dev)
  1089. && pep->tx_ring_size - pep->tx_desc_count > 1) {
  1090. netif_wake_queue(dev);
  1091. }
  1092. work_done = rxq_process(dev, budget);
  1093. if (work_done < budget) {
  1094. napi_complete(napi);
  1095. wrl(pep, INT_MASK, ALL_INTS);
  1096. }
  1097. return work_done;
  1098. }
  1099. static int pxa168_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1100. {
  1101. struct pxa168_eth_private *pep = netdev_priv(dev);
  1102. struct net_device_stats *stats = &dev->stats;
  1103. struct tx_desc *desc;
  1104. int tx_index;
  1105. int length;
  1106. tx_index = eth_alloc_tx_desc_index(pep);
  1107. desc = &pep->p_tx_desc_area[tx_index];
  1108. length = skb->len;
  1109. pep->tx_skb[tx_index] = skb;
  1110. desc->byte_cnt = length;
  1111. desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
  1112. skb_tx_timestamp(skb);
  1113. wmb();
  1114. desc->cmd_sts = BUF_OWNED_BY_DMA | TX_GEN_CRC | TX_FIRST_DESC |
  1115. TX_ZERO_PADDING | TX_LAST_DESC | TX_EN_INT;
  1116. wmb();
  1117. wrl(pep, SDMA_CMD, SDMA_CMD_TXDH | SDMA_CMD_ERD);
  1118. stats->tx_bytes += length;
  1119. stats->tx_packets++;
  1120. dev->trans_start = jiffies;
  1121. if (pep->tx_ring_size - pep->tx_desc_count <= 1) {
  1122. /* We handled the current skb, but now we are out of space.*/
  1123. netif_stop_queue(dev);
  1124. }
  1125. return NETDEV_TX_OK;
  1126. }
  1127. static int smi_wait_ready(struct pxa168_eth_private *pep)
  1128. {
  1129. int i = 0;
  1130. /* wait for the SMI register to become available */
  1131. for (i = 0; rdl(pep, SMI) & SMI_BUSY; i++) {
  1132. if (i == PHY_WAIT_ITERATIONS)
  1133. return -ETIMEDOUT;
  1134. msleep(10);
  1135. }
  1136. return 0;
  1137. }
  1138. static int pxa168_smi_read(struct mii_bus *bus, int phy_addr, int regnum)
  1139. {
  1140. struct pxa168_eth_private *pep = bus->priv;
  1141. int i = 0;
  1142. int val;
  1143. if (smi_wait_ready(pep)) {
  1144. netdev_warn(pep->dev, "pxa168_eth: SMI bus busy timeout\n");
  1145. return -ETIMEDOUT;
  1146. }
  1147. wrl(pep, SMI, (phy_addr << 16) | (regnum << 21) | SMI_OP_R);
  1148. /* now wait for the data to be valid */
  1149. for (i = 0; !((val = rdl(pep, SMI)) & SMI_R_VALID); i++) {
  1150. if (i == PHY_WAIT_ITERATIONS) {
  1151. netdev_warn(pep->dev,
  1152. "pxa168_eth: SMI bus read not valid\n");
  1153. return -ENODEV;
  1154. }
  1155. msleep(10);
  1156. }
  1157. return val & 0xffff;
  1158. }
  1159. static int pxa168_smi_write(struct mii_bus *bus, int phy_addr, int regnum,
  1160. u16 value)
  1161. {
  1162. struct pxa168_eth_private *pep = bus->priv;
  1163. if (smi_wait_ready(pep)) {
  1164. netdev_warn(pep->dev, "pxa168_eth: SMI bus busy timeout\n");
  1165. return -ETIMEDOUT;
  1166. }
  1167. wrl(pep, SMI, (phy_addr << 16) | (regnum << 21) |
  1168. SMI_OP_W | (value & 0xffff));
  1169. if (smi_wait_ready(pep)) {
  1170. netdev_err(pep->dev, "pxa168_eth: SMI bus busy timeout\n");
  1171. return -ETIMEDOUT;
  1172. }
  1173. return 0;
  1174. }
  1175. static int pxa168_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr,
  1176. int cmd)
  1177. {
  1178. struct pxa168_eth_private *pep = netdev_priv(dev);
  1179. if (pep->phy != NULL)
  1180. return phy_mii_ioctl(pep->phy, ifr, cmd);
  1181. return -EOPNOTSUPP;
  1182. }
  1183. static int pxa168_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1184. {
  1185. struct pxa168_eth_private *pep = netdev_priv(dev);
  1186. int err;
  1187. err = phy_read_status(pep->phy);
  1188. if (err == 0)
  1189. err = phy_ethtool_gset(pep->phy, cmd);
  1190. return err;
  1191. }
  1192. static int pxa168_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1193. {
  1194. struct pxa168_eth_private *pep = netdev_priv(dev);
  1195. return phy_ethtool_sset(pep->phy, cmd);
  1196. }
  1197. static void pxa168_get_drvinfo(struct net_device *dev,
  1198. struct ethtool_drvinfo *info)
  1199. {
  1200. strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
  1201. strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
  1202. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  1203. strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
  1204. }
  1205. static const struct ethtool_ops pxa168_ethtool_ops = {
  1206. .get_settings = pxa168_get_settings,
  1207. .set_settings = pxa168_set_settings,
  1208. .get_drvinfo = pxa168_get_drvinfo,
  1209. .get_link = ethtool_op_get_link,
  1210. .get_ts_info = ethtool_op_get_ts_info,
  1211. };
  1212. static const struct net_device_ops pxa168_eth_netdev_ops = {
  1213. .ndo_open = pxa168_eth_open,
  1214. .ndo_stop = pxa168_eth_stop,
  1215. .ndo_start_xmit = pxa168_eth_start_xmit,
  1216. .ndo_set_rx_mode = pxa168_eth_set_rx_mode,
  1217. .ndo_set_mac_address = pxa168_eth_set_mac_address,
  1218. .ndo_validate_addr = eth_validate_addr,
  1219. .ndo_do_ioctl = pxa168_eth_do_ioctl,
  1220. .ndo_change_mtu = pxa168_eth_change_mtu,
  1221. .ndo_tx_timeout = pxa168_eth_tx_timeout,
  1222. };
  1223. static int pxa168_eth_probe(struct platform_device *pdev)
  1224. {
  1225. struct pxa168_eth_private *pep = NULL;
  1226. struct net_device *dev = NULL;
  1227. struct resource *res;
  1228. struct clk *clk;
  1229. struct device_node *np;
  1230. const unsigned char *mac_addr = NULL;
  1231. int err;
  1232. printk(KERN_NOTICE "PXA168 10/100 Ethernet Driver\n");
  1233. clk = devm_clk_get(&pdev->dev, NULL);
  1234. if (IS_ERR(clk)) {
  1235. dev_err(&pdev->dev, "Fast Ethernet failed to get clock\n");
  1236. return -ENODEV;
  1237. }
  1238. clk_prepare_enable(clk);
  1239. dev = alloc_etherdev(sizeof(struct pxa168_eth_private));
  1240. if (!dev) {
  1241. err = -ENOMEM;
  1242. goto err_clk;
  1243. }
  1244. platform_set_drvdata(pdev, dev);
  1245. pep = netdev_priv(dev);
  1246. pep->dev = dev;
  1247. pep->clk = clk;
  1248. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1249. pep->base = devm_ioremap_resource(&pdev->dev, res);
  1250. if (IS_ERR(pep->base)) {
  1251. err = -ENOMEM;
  1252. goto err_netdev;
  1253. }
  1254. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1255. BUG_ON(!res);
  1256. dev->irq = res->start;
  1257. dev->netdev_ops = &pxa168_eth_netdev_ops;
  1258. dev->watchdog_timeo = 2 * HZ;
  1259. dev->base_addr = 0;
  1260. dev->ethtool_ops = &pxa168_ethtool_ops;
  1261. INIT_WORK(&pep->tx_timeout_task, pxa168_eth_tx_timeout_task);
  1262. if (pdev->dev.of_node)
  1263. mac_addr = of_get_mac_address(pdev->dev.of_node);
  1264. if (mac_addr && is_valid_ether_addr(mac_addr)) {
  1265. ether_addr_copy(dev->dev_addr, mac_addr);
  1266. } else {
  1267. /* try reading the mac address, if set by the bootloader */
  1268. pxa168_eth_get_mac_address(dev, dev->dev_addr);
  1269. if (!is_valid_ether_addr(dev->dev_addr)) {
  1270. dev_info(&pdev->dev, "Using random mac address\n");
  1271. eth_hw_addr_random(dev);
  1272. }
  1273. }
  1274. pep->rx_ring_size = NUM_RX_DESCS;
  1275. pep->tx_ring_size = NUM_TX_DESCS;
  1276. pep->pd = dev_get_platdata(&pdev->dev);
  1277. if (pep->pd) {
  1278. if (pep->pd->rx_queue_size)
  1279. pep->rx_ring_size = pep->pd->rx_queue_size;
  1280. if (pep->pd->tx_queue_size)
  1281. pep->tx_ring_size = pep->pd->tx_queue_size;
  1282. pep->port_num = pep->pd->port_number;
  1283. pep->phy_addr = pep->pd->phy_addr;
  1284. pep->phy_speed = pep->pd->speed;
  1285. pep->phy_duplex = pep->pd->duplex;
  1286. pep->phy_intf = pep->pd->intf;
  1287. if (pep->pd->init)
  1288. pep->pd->init();
  1289. } else if (pdev->dev.of_node) {
  1290. of_property_read_u32(pdev->dev.of_node, "port-id",
  1291. &pep->port_num);
  1292. np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0);
  1293. if (!np) {
  1294. dev_err(&pdev->dev, "missing phy-handle\n");
  1295. err = -EINVAL;
  1296. goto err_netdev;
  1297. }
  1298. of_property_read_u32(np, "reg", &pep->phy_addr);
  1299. pep->phy_intf = of_get_phy_mode(pdev->dev.of_node);
  1300. }
  1301. /* Hardware supports only 3 ports */
  1302. BUG_ON(pep->port_num > 2);
  1303. netif_napi_add(dev, &pep->napi, pxa168_rx_poll, pep->rx_ring_size);
  1304. memset(&pep->timeout, 0, sizeof(struct timer_list));
  1305. init_timer(&pep->timeout);
  1306. pep->timeout.function = rxq_refill_timer_wrapper;
  1307. pep->timeout.data = (unsigned long)pep;
  1308. pep->smi_bus = mdiobus_alloc();
  1309. if (pep->smi_bus == NULL) {
  1310. err = -ENOMEM;
  1311. goto err_netdev;
  1312. }
  1313. pep->smi_bus->priv = pep;
  1314. pep->smi_bus->name = "pxa168_eth smi";
  1315. pep->smi_bus->read = pxa168_smi_read;
  1316. pep->smi_bus->write = pxa168_smi_write;
  1317. snprintf(pep->smi_bus->id, MII_BUS_ID_SIZE, "%s-%d",
  1318. pdev->name, pdev->id);
  1319. pep->smi_bus->parent = &pdev->dev;
  1320. pep->smi_bus->phy_mask = 0xffffffff;
  1321. err = mdiobus_register(pep->smi_bus);
  1322. if (err)
  1323. goto err_free_mdio;
  1324. SET_NETDEV_DEV(dev, &pdev->dev);
  1325. pxa168_init_hw(pep);
  1326. err = register_netdev(dev);
  1327. if (err)
  1328. goto err_mdiobus;
  1329. return 0;
  1330. err_mdiobus:
  1331. mdiobus_unregister(pep->smi_bus);
  1332. err_free_mdio:
  1333. mdiobus_free(pep->smi_bus);
  1334. err_netdev:
  1335. free_netdev(dev);
  1336. err_clk:
  1337. clk_disable_unprepare(clk);
  1338. return err;
  1339. }
  1340. static int pxa168_eth_remove(struct platform_device *pdev)
  1341. {
  1342. struct net_device *dev = platform_get_drvdata(pdev);
  1343. struct pxa168_eth_private *pep = netdev_priv(dev);
  1344. if (pep->htpr) {
  1345. dma_free_coherent(pep->dev->dev.parent, HASH_ADDR_TABLE_SIZE,
  1346. pep->htpr, pep->htpr_dma);
  1347. pep->htpr = NULL;
  1348. }
  1349. if (pep->phy)
  1350. phy_disconnect(pep->phy);
  1351. if (pep->clk) {
  1352. clk_disable_unprepare(pep->clk);
  1353. }
  1354. mdiobus_unregister(pep->smi_bus);
  1355. mdiobus_free(pep->smi_bus);
  1356. unregister_netdev(dev);
  1357. cancel_work_sync(&pep->tx_timeout_task);
  1358. free_netdev(dev);
  1359. return 0;
  1360. }
  1361. static void pxa168_eth_shutdown(struct platform_device *pdev)
  1362. {
  1363. struct net_device *dev = platform_get_drvdata(pdev);
  1364. eth_port_reset(dev);
  1365. }
  1366. #ifdef CONFIG_PM
  1367. static int pxa168_eth_resume(struct platform_device *pdev)
  1368. {
  1369. return -ENOSYS;
  1370. }
  1371. static int pxa168_eth_suspend(struct platform_device *pdev, pm_message_t state)
  1372. {
  1373. return -ENOSYS;
  1374. }
  1375. #else
  1376. #define pxa168_eth_resume NULL
  1377. #define pxa168_eth_suspend NULL
  1378. #endif
  1379. static const struct of_device_id pxa168_eth_of_match[] = {
  1380. { .compatible = "marvell,pxa168-eth" },
  1381. { },
  1382. };
  1383. MODULE_DEVICE_TABLE(of, pxa168_eth_of_match);
  1384. static struct platform_driver pxa168_eth_driver = {
  1385. .probe = pxa168_eth_probe,
  1386. .remove = pxa168_eth_remove,
  1387. .shutdown = pxa168_eth_shutdown,
  1388. .resume = pxa168_eth_resume,
  1389. .suspend = pxa168_eth_suspend,
  1390. .driver = {
  1391. .name = DRIVER_NAME,
  1392. .of_match_table = of_match_ptr(pxa168_eth_of_match),
  1393. },
  1394. };
  1395. module_platform_driver(pxa168_eth_driver);
  1396. MODULE_LICENSE("GPL");
  1397. MODULE_DESCRIPTION("Ethernet driver for Marvell PXA168");
  1398. MODULE_ALIAS("platform:pxa168_eth");