pxa168_eth.c 41 KB

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