pxa168_eth.c 41 KB

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