pxa168_eth.c 42 KB

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