bfin_mac.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. /*
  2. * Blackfin On-Chip MAC Driver
  3. *
  4. * Copyright 2004-2007 Analog Devices Inc.
  5. *
  6. * Enter bugs at http://blackfin.uclinux.org/
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/delay.h>
  16. #include <linux/timer.h>
  17. #include <linux/errno.h>
  18. #include <linux/irq.h>
  19. #include <linux/io.h>
  20. #include <linux/ioport.h>
  21. #include <linux/crc32.h>
  22. #include <linux/device.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/mii.h>
  26. #include <linux/phy.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/platform_device.h>
  31. #include <asm/dma.h>
  32. #include <linux/dma-mapping.h>
  33. #include <asm/blackfin.h>
  34. #include <asm/cacheflush.h>
  35. #include <asm/portmux.h>
  36. #include "bfin_mac.h"
  37. #define DRV_NAME "bfin_mac"
  38. #define DRV_VERSION "1.1"
  39. #define DRV_AUTHOR "Bryan Wu, Luke Yang"
  40. #define DRV_DESC "Blackfin BF53[67] BF527 on-chip Ethernet MAC driver"
  41. MODULE_AUTHOR(DRV_AUTHOR);
  42. MODULE_LICENSE("GPL");
  43. MODULE_DESCRIPTION(DRV_DESC);
  44. #if defined(CONFIG_BFIN_MAC_USE_L1)
  45. # define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size)
  46. # define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr)
  47. #else
  48. # define bfin_mac_alloc(dma_handle, size) \
  49. dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL)
  50. # define bfin_mac_free(dma_handle, ptr) \
  51. dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle)
  52. #endif
  53. #define PKT_BUF_SZ 1580
  54. #define MAX_TIMEOUT_CNT 500
  55. /* pointers to maintain transmit list */
  56. static struct net_dma_desc_tx *tx_list_head;
  57. static struct net_dma_desc_tx *tx_list_tail;
  58. static struct net_dma_desc_rx *rx_list_head;
  59. static struct net_dma_desc_rx *rx_list_tail;
  60. static struct net_dma_desc_rx *current_rx_ptr;
  61. static struct net_dma_desc_tx *current_tx_ptr;
  62. static struct net_dma_desc_tx *tx_desc;
  63. static struct net_dma_desc_rx *rx_desc;
  64. static void bf537mac_disable(void);
  65. static void bf537mac_enable(void);
  66. static void desc_list_free(void)
  67. {
  68. struct net_dma_desc_rx *r;
  69. struct net_dma_desc_tx *t;
  70. int i;
  71. #if !defined(CONFIG_BFIN_MAC_USE_L1)
  72. dma_addr_t dma_handle = 0;
  73. #endif
  74. if (tx_desc) {
  75. t = tx_list_head;
  76. for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
  77. if (t) {
  78. if (t->skb) {
  79. dev_kfree_skb(t->skb);
  80. t->skb = NULL;
  81. }
  82. t = t->next;
  83. }
  84. }
  85. bfin_mac_free(dma_handle, tx_desc);
  86. }
  87. if (rx_desc) {
  88. r = rx_list_head;
  89. for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
  90. if (r) {
  91. if (r->skb) {
  92. dev_kfree_skb(r->skb);
  93. r->skb = NULL;
  94. }
  95. r = r->next;
  96. }
  97. }
  98. bfin_mac_free(dma_handle, rx_desc);
  99. }
  100. }
  101. static int desc_list_init(void)
  102. {
  103. int i;
  104. struct sk_buff *new_skb;
  105. #if !defined(CONFIG_BFIN_MAC_USE_L1)
  106. /*
  107. * This dma_handle is useless in Blackfin dma_alloc_coherent().
  108. * The real dma handler is the return value of dma_alloc_coherent().
  109. */
  110. dma_addr_t dma_handle;
  111. #endif
  112. tx_desc = bfin_mac_alloc(&dma_handle,
  113. sizeof(struct net_dma_desc_tx) *
  114. CONFIG_BFIN_TX_DESC_NUM);
  115. if (tx_desc == NULL)
  116. goto init_error;
  117. rx_desc = bfin_mac_alloc(&dma_handle,
  118. sizeof(struct net_dma_desc_rx) *
  119. CONFIG_BFIN_RX_DESC_NUM);
  120. if (rx_desc == NULL)
  121. goto init_error;
  122. /* init tx_list */
  123. tx_list_head = tx_list_tail = tx_desc;
  124. for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
  125. struct net_dma_desc_tx *t = tx_desc + i;
  126. struct dma_descriptor *a = &(t->desc_a);
  127. struct dma_descriptor *b = &(t->desc_b);
  128. /*
  129. * disable DMA
  130. * read from memory WNR = 0
  131. * wordsize is 32 bits
  132. * 6 half words is desc size
  133. * large desc flow
  134. */
  135. a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
  136. a->start_addr = (unsigned long)t->packet;
  137. a->x_count = 0;
  138. a->next_dma_desc = b;
  139. /*
  140. * enabled DMA
  141. * write to memory WNR = 1
  142. * wordsize is 32 bits
  143. * disable interrupt
  144. * 6 half words is desc size
  145. * large desc flow
  146. */
  147. b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
  148. b->start_addr = (unsigned long)(&(t->status));
  149. b->x_count = 0;
  150. t->skb = NULL;
  151. tx_list_tail->desc_b.next_dma_desc = a;
  152. tx_list_tail->next = t;
  153. tx_list_tail = t;
  154. }
  155. tx_list_tail->next = tx_list_head; /* tx_list is a circle */
  156. tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a);
  157. current_tx_ptr = tx_list_head;
  158. /* init rx_list */
  159. rx_list_head = rx_list_tail = rx_desc;
  160. for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
  161. struct net_dma_desc_rx *r = rx_desc + i;
  162. struct dma_descriptor *a = &(r->desc_a);
  163. struct dma_descriptor *b = &(r->desc_b);
  164. /* allocate a new skb for next time receive */
  165. new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
  166. if (!new_skb) {
  167. printk(KERN_NOTICE DRV_NAME
  168. ": init: low on mem - packet dropped\n");
  169. goto init_error;
  170. }
  171. skb_reserve(new_skb, 2);
  172. r->skb = new_skb;
  173. /*
  174. * enabled DMA
  175. * write to memory WNR = 1
  176. * wordsize is 32 bits
  177. * disable interrupt
  178. * 6 half words is desc size
  179. * large desc flow
  180. */
  181. a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
  182. /* since RXDWA is enabled */
  183. a->start_addr = (unsigned long)new_skb->data - 2;
  184. a->x_count = 0;
  185. a->next_dma_desc = b;
  186. /*
  187. * enabled DMA
  188. * write to memory WNR = 1
  189. * wordsize is 32 bits
  190. * enable interrupt
  191. * 6 half words is desc size
  192. * large desc flow
  193. */
  194. b->config = DMAEN | WNR | WDSIZE_32 | DI_EN |
  195. NDSIZE_6 | DMAFLOW_LARGE;
  196. b->start_addr = (unsigned long)(&(r->status));
  197. b->x_count = 0;
  198. rx_list_tail->desc_b.next_dma_desc = a;
  199. rx_list_tail->next = r;
  200. rx_list_tail = r;
  201. }
  202. rx_list_tail->next = rx_list_head; /* rx_list is a circle */
  203. rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a);
  204. current_rx_ptr = rx_list_head;
  205. return 0;
  206. init_error:
  207. desc_list_free();
  208. printk(KERN_ERR DRV_NAME ": kmalloc failed\n");
  209. return -ENOMEM;
  210. }
  211. /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
  212. /* Set FER regs to MUX in Ethernet pins */
  213. static int setup_pin_mux(int action)
  214. {
  215. #if defined(CONFIG_BFIN_MAC_RMII)
  216. u16 pin_req[] = P_RMII0;
  217. #else
  218. u16 pin_req[] = P_MII0;
  219. #endif
  220. if (action) {
  221. if (peripheral_request_list(pin_req, DRV_NAME)) {
  222. printk(KERN_ERR DRV_NAME
  223. ": Requesting Peripherals failed\n");
  224. return -EFAULT;
  225. }
  226. } else
  227. peripheral_free_list(pin_req);
  228. return 0;
  229. }
  230. /*
  231. * MII operations
  232. */
  233. /* Wait until the previous MDC/MDIO transaction has completed */
  234. static void mdio_poll(void)
  235. {
  236. int timeout_cnt = MAX_TIMEOUT_CNT;
  237. /* poll the STABUSY bit */
  238. while ((bfin_read_EMAC_STAADD()) & STABUSY) {
  239. udelay(1);
  240. if (timeout_cnt-- < 0) {
  241. printk(KERN_ERR DRV_NAME
  242. ": wait MDC/MDIO transaction to complete timeout\n");
  243. break;
  244. }
  245. }
  246. }
  247. /* Read an off-chip register in a PHY through the MDC/MDIO port */
  248. static int mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
  249. {
  250. mdio_poll();
  251. /* read mode */
  252. bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
  253. SET_REGAD((u16) regnum) |
  254. STABUSY);
  255. mdio_poll();
  256. return (int) bfin_read_EMAC_STADAT();
  257. }
  258. /* Write an off-chip register in a PHY through the MDC/MDIO port */
  259. static int mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
  260. u16 value)
  261. {
  262. mdio_poll();
  263. bfin_write_EMAC_STADAT((u32) value);
  264. /* write mode */
  265. bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
  266. SET_REGAD((u16) regnum) |
  267. STAOP |
  268. STABUSY);
  269. mdio_poll();
  270. return 0;
  271. }
  272. static int mdiobus_reset(struct mii_bus *bus)
  273. {
  274. return 0;
  275. }
  276. static void bf537_adjust_link(struct net_device *dev)
  277. {
  278. struct bf537mac_local *lp = netdev_priv(dev);
  279. struct phy_device *phydev = lp->phydev;
  280. unsigned long flags;
  281. int new_state = 0;
  282. spin_lock_irqsave(&lp->lock, flags);
  283. if (phydev->link) {
  284. /* Now we make sure that we can be in full duplex mode.
  285. * If not, we operate in half-duplex mode. */
  286. if (phydev->duplex != lp->old_duplex) {
  287. u32 opmode = bfin_read_EMAC_OPMODE();
  288. new_state = 1;
  289. if (phydev->duplex)
  290. opmode |= FDMODE;
  291. else
  292. opmode &= ~(FDMODE);
  293. bfin_write_EMAC_OPMODE(opmode);
  294. lp->old_duplex = phydev->duplex;
  295. }
  296. if (phydev->speed != lp->old_speed) {
  297. #if defined(CONFIG_BFIN_MAC_RMII)
  298. u32 opmode = bfin_read_EMAC_OPMODE();
  299. switch (phydev->speed) {
  300. case 10:
  301. opmode |= RMII_10;
  302. break;
  303. case 100:
  304. opmode &= ~(RMII_10);
  305. break;
  306. default:
  307. printk(KERN_WARNING
  308. "%s: Ack! Speed (%d) is not 10/100!\n",
  309. DRV_NAME, phydev->speed);
  310. break;
  311. }
  312. bfin_write_EMAC_OPMODE(opmode);
  313. #endif
  314. new_state = 1;
  315. lp->old_speed = phydev->speed;
  316. }
  317. if (!lp->old_link) {
  318. new_state = 1;
  319. lp->old_link = 1;
  320. netif_schedule(dev);
  321. }
  322. } else if (lp->old_link) {
  323. new_state = 1;
  324. lp->old_link = 0;
  325. lp->old_speed = 0;
  326. lp->old_duplex = -1;
  327. }
  328. if (new_state) {
  329. u32 opmode = bfin_read_EMAC_OPMODE();
  330. phy_print_status(phydev);
  331. pr_debug("EMAC_OPMODE = 0x%08x\n", opmode);
  332. }
  333. spin_unlock_irqrestore(&lp->lock, flags);
  334. }
  335. /* MDC = 2.5 MHz */
  336. #define MDC_CLK 2500000
  337. static int mii_probe(struct net_device *dev)
  338. {
  339. struct bf537mac_local *lp = netdev_priv(dev);
  340. struct phy_device *phydev = NULL;
  341. unsigned short sysctl;
  342. int i;
  343. u32 sclk, mdc_div;
  344. /* Enable PHY output early */
  345. if (!(bfin_read_VR_CTL() & PHYCLKOE))
  346. bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
  347. sclk = get_sclk();
  348. mdc_div = ((sclk / MDC_CLK) / 2) - 1;
  349. sysctl = bfin_read_EMAC_SYSCTL();
  350. sysctl = (sysctl & ~MDCDIV) | SET_MDCDIV(mdc_div);
  351. bfin_write_EMAC_SYSCTL(sysctl);
  352. /* search for connect PHY device */
  353. for (i = 0; i < PHY_MAX_ADDR; i++) {
  354. struct phy_device *const tmp_phydev = lp->mii_bus.phy_map[i];
  355. if (!tmp_phydev)
  356. continue; /* no PHY here... */
  357. phydev = tmp_phydev;
  358. break; /* found it */
  359. }
  360. /* now we are supposed to have a proper phydev, to attach to... */
  361. if (!phydev) {
  362. printk(KERN_INFO "%s: Don't found any phy device at all\n",
  363. dev->name);
  364. return -ENODEV;
  365. }
  366. #if defined(CONFIG_BFIN_MAC_RMII)
  367. phydev = phy_connect(dev, phydev->dev.bus_id, &bf537_adjust_link, 0,
  368. PHY_INTERFACE_MODE_RMII);
  369. #else
  370. phydev = phy_connect(dev, phydev->dev.bus_id, &bf537_adjust_link, 0,
  371. PHY_INTERFACE_MODE_MII);
  372. #endif
  373. if (IS_ERR(phydev)) {
  374. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  375. return PTR_ERR(phydev);
  376. }
  377. /* mask with MAC supported features */
  378. phydev->supported &= (SUPPORTED_10baseT_Half
  379. | SUPPORTED_10baseT_Full
  380. | SUPPORTED_100baseT_Half
  381. | SUPPORTED_100baseT_Full
  382. | SUPPORTED_Autoneg
  383. | SUPPORTED_Pause | SUPPORTED_Asym_Pause
  384. | SUPPORTED_MII
  385. | SUPPORTED_TP);
  386. phydev->advertising = phydev->supported;
  387. lp->old_link = 0;
  388. lp->old_speed = 0;
  389. lp->old_duplex = -1;
  390. lp->phydev = phydev;
  391. printk(KERN_INFO "%s: attached PHY driver [%s] "
  392. "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)"
  393. "@sclk=%dMHz)\n",
  394. DRV_NAME, phydev->drv->name, phydev->dev.bus_id, phydev->irq,
  395. MDC_CLK, mdc_div, sclk/1000000);
  396. return 0;
  397. }
  398. /**************************************************************************/
  399. void setup_system_regs(struct net_device *dev)
  400. {
  401. unsigned short sysctl;
  402. /*
  403. * Odd word alignment for Receive Frame DMA word
  404. * Configure checksum support and rcve frame word alignment
  405. */
  406. sysctl = bfin_read_EMAC_SYSCTL();
  407. #if defined(BFIN_MAC_CSUM_OFFLOAD)
  408. sysctl |= RXDWA | RXCKS;
  409. #else
  410. sysctl |= RXDWA;
  411. #endif
  412. bfin_write_EMAC_SYSCTL(sysctl);
  413. bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
  414. /* Initialize the TX DMA channel registers */
  415. bfin_write_DMA2_X_COUNT(0);
  416. bfin_write_DMA2_X_MODIFY(4);
  417. bfin_write_DMA2_Y_COUNT(0);
  418. bfin_write_DMA2_Y_MODIFY(0);
  419. /* Initialize the RX DMA channel registers */
  420. bfin_write_DMA1_X_COUNT(0);
  421. bfin_write_DMA1_X_MODIFY(4);
  422. bfin_write_DMA1_Y_COUNT(0);
  423. bfin_write_DMA1_Y_MODIFY(0);
  424. }
  425. static void setup_mac_addr(u8 *mac_addr)
  426. {
  427. u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
  428. u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
  429. /* this depends on a little-endian machine */
  430. bfin_write_EMAC_ADDRLO(addr_low);
  431. bfin_write_EMAC_ADDRHI(addr_hi);
  432. }
  433. static int bf537mac_set_mac_address(struct net_device *dev, void *p)
  434. {
  435. struct sockaddr *addr = p;
  436. if (netif_running(dev))
  437. return -EBUSY;
  438. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  439. setup_mac_addr(dev->dev_addr);
  440. return 0;
  441. }
  442. static void adjust_tx_list(void)
  443. {
  444. int timeout_cnt = MAX_TIMEOUT_CNT;
  445. if (tx_list_head->status.status_word != 0
  446. && current_tx_ptr != tx_list_head) {
  447. goto adjust_head; /* released something, just return; */
  448. }
  449. /*
  450. * if nothing released, check wait condition
  451. * current's next can not be the head,
  452. * otherwise the dma will not stop as we want
  453. */
  454. if (current_tx_ptr->next->next == tx_list_head) {
  455. while (tx_list_head->status.status_word == 0) {
  456. mdelay(1);
  457. if (tx_list_head->status.status_word != 0
  458. || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) {
  459. goto adjust_head;
  460. }
  461. if (timeout_cnt-- < 0) {
  462. printk(KERN_ERR DRV_NAME
  463. ": wait for adjust tx list head timeout\n");
  464. break;
  465. }
  466. }
  467. if (tx_list_head->status.status_word != 0) {
  468. goto adjust_head;
  469. }
  470. }
  471. return;
  472. adjust_head:
  473. do {
  474. tx_list_head->desc_a.config &= ~DMAEN;
  475. tx_list_head->status.status_word = 0;
  476. if (tx_list_head->skb) {
  477. dev_kfree_skb(tx_list_head->skb);
  478. tx_list_head->skb = NULL;
  479. } else {
  480. printk(KERN_ERR DRV_NAME
  481. ": no sk_buff in a transmitted frame!\n");
  482. }
  483. tx_list_head = tx_list_head->next;
  484. } while (tx_list_head->status.status_word != 0
  485. && current_tx_ptr != tx_list_head);
  486. return;
  487. }
  488. static int bf537mac_hard_start_xmit(struct sk_buff *skb,
  489. struct net_device *dev)
  490. {
  491. struct bf537mac_local *lp = netdev_priv(dev);
  492. unsigned int data;
  493. current_tx_ptr->skb = skb;
  494. /*
  495. * Is skb->data always 16-bit aligned?
  496. * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)?
  497. */
  498. if ((((unsigned int)(skb->data)) & 0x02) == 2) {
  499. /* move skb->data to current_tx_ptr payload */
  500. data = (unsigned int)(skb->data) - 2;
  501. *((unsigned short *)data) = (unsigned short)(skb->len);
  502. current_tx_ptr->desc_a.start_addr = (unsigned long)data;
  503. /* this is important! */
  504. blackfin_dcache_flush_range(data, (data + (skb->len)) + 2);
  505. } else {
  506. *((unsigned short *)(current_tx_ptr->packet)) =
  507. (unsigned short)(skb->len);
  508. memcpy((char *)(current_tx_ptr->packet + 2), skb->data,
  509. (skb->len));
  510. current_tx_ptr->desc_a.start_addr =
  511. (unsigned long)current_tx_ptr->packet;
  512. if (current_tx_ptr->status.status_word != 0)
  513. current_tx_ptr->status.status_word = 0;
  514. blackfin_dcache_flush_range((unsigned int)current_tx_ptr->
  515. packet,
  516. (unsigned int)(current_tx_ptr->
  517. packet + skb->len) +
  518. 2);
  519. }
  520. /* enable this packet's dma */
  521. current_tx_ptr->desc_a.config |= DMAEN;
  522. /* tx dma is running, just return */
  523. if (bfin_read_DMA2_IRQ_STATUS() & 0x08)
  524. goto out;
  525. /* tx dma is not running */
  526. bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
  527. /* dma enabled, read from memory, size is 6 */
  528. bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
  529. /* Turn on the EMAC tx */
  530. bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
  531. out:
  532. adjust_tx_list();
  533. current_tx_ptr = current_tx_ptr->next;
  534. dev->trans_start = jiffies;
  535. dev->stats.tx_packets++;
  536. dev->stats.tx_bytes += (skb->len);
  537. return 0;
  538. }
  539. static void bf537mac_rx(struct net_device *dev)
  540. {
  541. struct sk_buff *skb, *new_skb;
  542. struct bf537mac_local *lp = netdev_priv(dev);
  543. unsigned short len;
  544. /* allocate a new skb for next time receive */
  545. skb = current_rx_ptr->skb;
  546. new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
  547. if (!new_skb) {
  548. printk(KERN_NOTICE DRV_NAME
  549. ": rx: low on mem - packet dropped\n");
  550. dev->stats.rx_dropped++;
  551. goto out;
  552. }
  553. /* reserve 2 bytes for RXDWA padding */
  554. skb_reserve(new_skb, 2);
  555. current_rx_ptr->skb = new_skb;
  556. current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
  557. /* Invidate the data cache of skb->data range when it is write back
  558. * cache. It will prevent overwritting the new data from DMA
  559. */
  560. blackfin_dcache_invalidate_range((unsigned long)new_skb->head,
  561. (unsigned long)new_skb->end);
  562. len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
  563. skb_put(skb, len);
  564. blackfin_dcache_invalidate_range((unsigned long)skb->head,
  565. (unsigned long)skb->tail);
  566. dev->last_rx = jiffies;
  567. skb->dev = dev;
  568. skb->protocol = eth_type_trans(skb, dev);
  569. #if defined(BFIN_MAC_CSUM_OFFLOAD)
  570. skb->csum = current_rx_ptr->status.ip_payload_csum;
  571. skb->ip_summed = CHECKSUM_COMPLETE;
  572. #endif
  573. netif_rx(skb);
  574. dev->stats.rx_packets++;
  575. dev->stats.rx_bytes += len;
  576. current_rx_ptr->status.status_word = 0x00000000;
  577. current_rx_ptr = current_rx_ptr->next;
  578. out:
  579. return;
  580. }
  581. /* interrupt routine to handle rx and error signal */
  582. static irqreturn_t bf537mac_interrupt(int irq, void *dev_id)
  583. {
  584. struct net_device *dev = dev_id;
  585. int number = 0;
  586. get_one_packet:
  587. if (current_rx_ptr->status.status_word == 0) {
  588. /* no more new packet received */
  589. if (number == 0) {
  590. if (current_rx_ptr->next->status.status_word != 0) {
  591. current_rx_ptr = current_rx_ptr->next;
  592. goto real_rx;
  593. }
  594. }
  595. bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
  596. DMA_DONE | DMA_ERR);
  597. return IRQ_HANDLED;
  598. }
  599. real_rx:
  600. bf537mac_rx(dev);
  601. number++;
  602. goto get_one_packet;
  603. }
  604. #ifdef CONFIG_NET_POLL_CONTROLLER
  605. static void bf537mac_poll(struct net_device *dev)
  606. {
  607. disable_irq(IRQ_MAC_RX);
  608. bf537mac_interrupt(IRQ_MAC_RX, dev);
  609. enable_irq(IRQ_MAC_RX);
  610. }
  611. #endif /* CONFIG_NET_POLL_CONTROLLER */
  612. static void bf537mac_disable(void)
  613. {
  614. unsigned int opmode;
  615. opmode = bfin_read_EMAC_OPMODE();
  616. opmode &= (~RE);
  617. opmode &= (~TE);
  618. /* Turn off the EMAC */
  619. bfin_write_EMAC_OPMODE(opmode);
  620. }
  621. /*
  622. * Enable Interrupts, Receive, and Transmit
  623. */
  624. static void bf537mac_enable(void)
  625. {
  626. u32 opmode;
  627. pr_debug("%s: %s\n", DRV_NAME, __FUNCTION__);
  628. /* Set RX DMA */
  629. bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
  630. bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
  631. /* Wait MII done */
  632. mdio_poll();
  633. /* We enable only RX here */
  634. /* ASTP : Enable Automatic Pad Stripping
  635. PR : Promiscuous Mode for test
  636. PSF : Receive frames with total length less than 64 bytes.
  637. FDMODE : Full Duplex Mode
  638. LB : Internal Loopback for test
  639. RE : Receiver Enable */
  640. opmode = bfin_read_EMAC_OPMODE();
  641. if (opmode & FDMODE)
  642. opmode |= PSF;
  643. else
  644. opmode |= DRO | DC | PSF;
  645. opmode |= RE;
  646. #if defined(CONFIG_BFIN_MAC_RMII)
  647. opmode |= RMII; /* For Now only 100MBit are supported */
  648. #if (defined(CONFIG_BF537) || defined(CONFIG_BF536)) && CONFIG_BF_REV_0_2
  649. opmode |= TE;
  650. #endif
  651. #endif
  652. /* Turn on the EMAC rx */
  653. bfin_write_EMAC_OPMODE(opmode);
  654. }
  655. /* Our watchdog timed out. Called by the networking layer */
  656. static void bf537mac_timeout(struct net_device *dev)
  657. {
  658. pr_debug("%s: %s\n", dev->name, __FUNCTION__);
  659. bf537mac_disable();
  660. /* reset tx queue */
  661. tx_list_tail = tx_list_head->next;
  662. bf537mac_enable();
  663. /* We can accept TX packets again */
  664. dev->trans_start = jiffies;
  665. netif_wake_queue(dev);
  666. }
  667. static void bf537mac_multicast_hash(struct net_device *dev)
  668. {
  669. u32 emac_hashhi, emac_hashlo;
  670. struct dev_mc_list *dmi = dev->mc_list;
  671. char *addrs;
  672. int i;
  673. u32 crc;
  674. emac_hashhi = emac_hashlo = 0;
  675. for (i = 0; i < dev->mc_count; i++) {
  676. addrs = dmi->dmi_addr;
  677. dmi = dmi->next;
  678. /* skip non-multicast addresses */
  679. if (!(*addrs & 1))
  680. continue;
  681. crc = ether_crc(ETH_ALEN, addrs);
  682. crc >>= 26;
  683. if (crc & 0x20)
  684. emac_hashhi |= 1 << (crc & 0x1f);
  685. else
  686. emac_hashlo |= 1 << (crc & 0x1f);
  687. }
  688. bfin_write_EMAC_HASHHI(emac_hashhi);
  689. bfin_write_EMAC_HASHLO(emac_hashlo);
  690. return;
  691. }
  692. /*
  693. * This routine will, depending on the values passed to it,
  694. * either make it accept multicast packets, go into
  695. * promiscuous mode (for TCPDUMP and cousins) or accept
  696. * a select set of multicast packets
  697. */
  698. static void bf537mac_set_multicast_list(struct net_device *dev)
  699. {
  700. u32 sysctl;
  701. if (dev->flags & IFF_PROMISC) {
  702. printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
  703. sysctl = bfin_read_EMAC_OPMODE();
  704. sysctl |= RAF;
  705. bfin_write_EMAC_OPMODE(sysctl);
  706. } else if (dev->flags & IFF_ALLMULTI) {
  707. /* accept all multicast */
  708. sysctl = bfin_read_EMAC_OPMODE();
  709. sysctl |= PAM;
  710. bfin_write_EMAC_OPMODE(sysctl);
  711. } else if (dev->mc_count) {
  712. /* set up multicast hash table */
  713. sysctl = bfin_read_EMAC_OPMODE();
  714. sysctl |= HM;
  715. bfin_write_EMAC_OPMODE(sysctl);
  716. bf537mac_multicast_hash(dev);
  717. } else {
  718. /* clear promisc or multicast mode */
  719. sysctl = bfin_read_EMAC_OPMODE();
  720. sysctl &= ~(RAF | PAM);
  721. bfin_write_EMAC_OPMODE(sysctl);
  722. }
  723. }
  724. /*
  725. * this puts the device in an inactive state
  726. */
  727. static void bf537mac_shutdown(struct net_device *dev)
  728. {
  729. /* Turn off the EMAC */
  730. bfin_write_EMAC_OPMODE(0x00000000);
  731. /* Turn off the EMAC RX DMA */
  732. bfin_write_DMA1_CONFIG(0x0000);
  733. bfin_write_DMA2_CONFIG(0x0000);
  734. }
  735. /*
  736. * Open and Initialize the interface
  737. *
  738. * Set up everything, reset the card, etc..
  739. */
  740. static int bf537mac_open(struct net_device *dev)
  741. {
  742. struct bf537mac_local *lp = netdev_priv(dev);
  743. int retval;
  744. pr_debug("%s: %s\n", dev->name, __FUNCTION__);
  745. /*
  746. * Check that the address is valid. If its not, refuse
  747. * to bring the device up. The user must specify an
  748. * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
  749. */
  750. if (!is_valid_ether_addr(dev->dev_addr)) {
  751. printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
  752. return -EINVAL;
  753. }
  754. /* initial rx and tx list */
  755. retval = desc_list_init();
  756. if (retval)
  757. return retval;
  758. phy_start(lp->phydev);
  759. phy_write(lp->phydev, MII_BMCR, BMCR_RESET);
  760. setup_system_regs(dev);
  761. bf537mac_disable();
  762. bf537mac_enable();
  763. pr_debug("hardware init finished\n");
  764. netif_start_queue(dev);
  765. netif_carrier_on(dev);
  766. return 0;
  767. }
  768. /*
  769. *
  770. * this makes the board clean up everything that it can
  771. * and not talk to the outside world. Caused by
  772. * an 'ifconfig ethX down'
  773. */
  774. static int bf537mac_close(struct net_device *dev)
  775. {
  776. struct bf537mac_local *lp = netdev_priv(dev);
  777. pr_debug("%s: %s\n", dev->name, __FUNCTION__);
  778. netif_stop_queue(dev);
  779. netif_carrier_off(dev);
  780. phy_stop(lp->phydev);
  781. phy_write(lp->phydev, MII_BMCR, BMCR_PDOWN);
  782. /* clear everything */
  783. bf537mac_shutdown(dev);
  784. /* free the rx/tx buffers */
  785. desc_list_free();
  786. return 0;
  787. }
  788. static int __init bf537mac_probe(struct net_device *dev)
  789. {
  790. struct bf537mac_local *lp = netdev_priv(dev);
  791. int retval;
  792. int i;
  793. /* Grab the MAC address in the MAC */
  794. *(__le32 *) (&(dev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
  795. *(__le16 *) (&(dev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
  796. /* probe mac */
  797. /*todo: how to proble? which is revision_register */
  798. bfin_write_EMAC_ADDRLO(0x12345678);
  799. if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
  800. pr_debug("can't detect bf537 mac!\n");
  801. retval = -ENODEV;
  802. goto err_out;
  803. }
  804. /* set the GPIO pins to Ethernet mode */
  805. retval = setup_pin_mux(1);
  806. if (retval)
  807. return retval;
  808. /*Is it valid? (Did bootloader initialize it?) */
  809. if (!is_valid_ether_addr(dev->dev_addr)) {
  810. /* Grab the MAC from the board somehow - this is done in the
  811. arch/blackfin/mach-bf537/boards/eth_mac.c */
  812. bfin_get_ether_addr(dev->dev_addr);
  813. }
  814. /* If still not valid, get a random one */
  815. if (!is_valid_ether_addr(dev->dev_addr)) {
  816. random_ether_addr(dev->dev_addr);
  817. }
  818. setup_mac_addr(dev->dev_addr);
  819. /* MDIO bus initial */
  820. lp->mii_bus.priv = dev;
  821. lp->mii_bus.read = mdiobus_read;
  822. lp->mii_bus.write = mdiobus_write;
  823. lp->mii_bus.reset = mdiobus_reset;
  824. lp->mii_bus.name = "bfin_mac_mdio";
  825. lp->mii_bus.id = 0;
  826. lp->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  827. for (i = 0; i < PHY_MAX_ADDR; ++i)
  828. lp->mii_bus.irq[i] = PHY_POLL;
  829. mdiobus_register(&lp->mii_bus);
  830. retval = mii_probe(dev);
  831. if (retval)
  832. return retval;
  833. /* Fill in the fields of the device structure with ethernet values. */
  834. ether_setup(dev);
  835. dev->open = bf537mac_open;
  836. dev->stop = bf537mac_close;
  837. dev->hard_start_xmit = bf537mac_hard_start_xmit;
  838. dev->set_mac_address = bf537mac_set_mac_address;
  839. dev->tx_timeout = bf537mac_timeout;
  840. dev->set_multicast_list = bf537mac_set_multicast_list;
  841. #ifdef CONFIG_NET_POLL_CONTROLLER
  842. dev->poll_controller = bf537mac_poll;
  843. #endif
  844. spin_lock_init(&lp->lock);
  845. /* now, enable interrupts */
  846. /* register irq handler */
  847. if (request_irq
  848. (IRQ_MAC_RX, bf537mac_interrupt, IRQF_DISABLED | IRQF_SHARED,
  849. "EMAC_RX", dev)) {
  850. printk(KERN_WARNING DRV_NAME
  851. ": Unable to attach BlackFin MAC RX interrupt\n");
  852. return -EBUSY;
  853. }
  854. retval = register_netdev(dev);
  855. if (retval == 0) {
  856. /* now, print out the card info, in a short format.. */
  857. printk(KERN_INFO "%s: Version %s, %s\n",
  858. DRV_NAME, DRV_VERSION, DRV_DESC);
  859. }
  860. err_out:
  861. return retval;
  862. }
  863. static int bfin_mac_probe(struct platform_device *pdev)
  864. {
  865. struct net_device *ndev;
  866. ndev = alloc_etherdev(sizeof(struct bf537mac_local));
  867. if (!ndev) {
  868. printk(KERN_WARNING DRV_NAME ": could not allocate device\n");
  869. return -ENOMEM;
  870. }
  871. SET_NETDEV_DEV(ndev, &pdev->dev);
  872. platform_set_drvdata(pdev, ndev);
  873. if (bf537mac_probe(ndev) != 0) {
  874. platform_set_drvdata(pdev, NULL);
  875. free_netdev(ndev);
  876. printk(KERN_WARNING DRV_NAME ": not found\n");
  877. return -ENODEV;
  878. }
  879. return 0;
  880. }
  881. static int bfin_mac_remove(struct platform_device *pdev)
  882. {
  883. struct net_device *ndev = platform_get_drvdata(pdev);
  884. platform_set_drvdata(pdev, NULL);
  885. unregister_netdev(ndev);
  886. free_irq(IRQ_MAC_RX, ndev);
  887. free_netdev(ndev);
  888. setup_pin_mux(0);
  889. return 0;
  890. }
  891. #ifdef CONFIG_PM
  892. static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
  893. {
  894. struct net_device *net_dev = platform_get_drvdata(pdev);
  895. if (netif_running(net_dev))
  896. bf537mac_close(net_dev);
  897. return 0;
  898. }
  899. static int bfin_mac_resume(struct platform_device *pdev)
  900. {
  901. struct net_device *net_dev = platform_get_drvdata(pdev);
  902. if (netif_running(net_dev))
  903. bf537mac_open(net_dev);
  904. return 0;
  905. }
  906. #else
  907. #define bfin_mac_suspend NULL
  908. #define bfin_mac_resume NULL
  909. #endif /* CONFIG_PM */
  910. static struct platform_driver bfin_mac_driver = {
  911. .probe = bfin_mac_probe,
  912. .remove = bfin_mac_remove,
  913. .resume = bfin_mac_resume,
  914. .suspend = bfin_mac_suspend,
  915. .driver = {
  916. .name = DRV_NAME,
  917. },
  918. };
  919. static int __init bfin_mac_init(void)
  920. {
  921. return platform_driver_register(&bfin_mac_driver);
  922. }
  923. module_init(bfin_mac_init);
  924. static void __exit bfin_mac_cleanup(void)
  925. {
  926. platform_driver_unregister(&bfin_mac_driver);
  927. }
  928. module_exit(bfin_mac_cleanup);