bfin_mac.c 25 KB

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