bfin_mac.c 27 KB

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