bfin_mac.c 27 KB

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