bfin_mac.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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/dpmc.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 + NET_IP_ALIGN);
  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, NET_IP_ALIGN);
  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 bfin_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 bfin_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
  238. {
  239. bfin_mdio_poll();
  240. /* read mode */
  241. bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
  242. SET_REGAD((u16) regnum) |
  243. STABUSY);
  244. bfin_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 bfin_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
  249. u16 value)
  250. {
  251. bfin_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. bfin_mdio_poll();
  259. return 0;
  260. }
  261. static int bfin_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. }
  310. } else if (lp->old_link) {
  311. new_state = 1;
  312. lp->old_link = 0;
  313. lp->old_speed = 0;
  314. lp->old_duplex = -1;
  315. }
  316. if (new_state) {
  317. u32 opmode = bfin_read_EMAC_OPMODE();
  318. phy_print_status(phydev);
  319. pr_debug("EMAC_OPMODE = 0x%08x\n", opmode);
  320. }
  321. spin_unlock_irqrestore(&lp->lock, flags);
  322. }
  323. /* MDC = 2.5 MHz */
  324. #define MDC_CLK 2500000
  325. static int mii_probe(struct net_device *dev)
  326. {
  327. struct bfin_mac_local *lp = netdev_priv(dev);
  328. struct phy_device *phydev = NULL;
  329. unsigned short sysctl;
  330. int i;
  331. u32 sclk, mdc_div;
  332. /* Enable PHY output early */
  333. if (!(bfin_read_VR_CTL() & CLKBUFOE))
  334. bfin_write_VR_CTL(bfin_read_VR_CTL() | CLKBUFOE);
  335. sclk = get_sclk();
  336. mdc_div = ((sclk / MDC_CLK) / 2) - 1;
  337. sysctl = bfin_read_EMAC_SYSCTL();
  338. sysctl = (sysctl & ~MDCDIV) | SET_MDCDIV(mdc_div);
  339. bfin_write_EMAC_SYSCTL(sysctl);
  340. /* search for connect PHY device */
  341. for (i = 0; i < PHY_MAX_ADDR; i++) {
  342. struct phy_device *const tmp_phydev = lp->mii_bus->phy_map[i];
  343. if (!tmp_phydev)
  344. continue; /* no PHY here... */
  345. phydev = tmp_phydev;
  346. break; /* found it */
  347. }
  348. /* now we are supposed to have a proper phydev, to attach to... */
  349. if (!phydev) {
  350. printk(KERN_INFO "%s: Don't found any phy device at all\n",
  351. dev->name);
  352. return -ENODEV;
  353. }
  354. #if defined(CONFIG_BFIN_MAC_RMII)
  355. phydev = phy_connect(dev, dev_name(&phydev->dev), &bfin_mac_adjust_link,
  356. 0, PHY_INTERFACE_MODE_RMII);
  357. #else
  358. phydev = phy_connect(dev, dev_name(&phydev->dev), &bfin_mac_adjust_link,
  359. 0, PHY_INTERFACE_MODE_MII);
  360. #endif
  361. if (IS_ERR(phydev)) {
  362. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  363. return PTR_ERR(phydev);
  364. }
  365. /* mask with MAC supported features */
  366. phydev->supported &= (SUPPORTED_10baseT_Half
  367. | SUPPORTED_10baseT_Full
  368. | SUPPORTED_100baseT_Half
  369. | SUPPORTED_100baseT_Full
  370. | SUPPORTED_Autoneg
  371. | SUPPORTED_Pause | SUPPORTED_Asym_Pause
  372. | SUPPORTED_MII
  373. | SUPPORTED_TP);
  374. phydev->advertising = phydev->supported;
  375. lp->old_link = 0;
  376. lp->old_speed = 0;
  377. lp->old_duplex = -1;
  378. lp->phydev = phydev;
  379. printk(KERN_INFO "%s: attached PHY driver [%s] "
  380. "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)"
  381. "@sclk=%dMHz)\n",
  382. DRV_NAME, phydev->drv->name, dev_name(&phydev->dev), phydev->irq,
  383. MDC_CLK, mdc_div, sclk/1000000);
  384. return 0;
  385. }
  386. /*
  387. * Ethtool support
  388. */
  389. static int
  390. bfin_mac_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  391. {
  392. struct bfin_mac_local *lp = netdev_priv(dev);
  393. if (lp->phydev)
  394. return phy_ethtool_gset(lp->phydev, cmd);
  395. return -EINVAL;
  396. }
  397. static int
  398. bfin_mac_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  399. {
  400. struct bfin_mac_local *lp = netdev_priv(dev);
  401. if (!capable(CAP_NET_ADMIN))
  402. return -EPERM;
  403. if (lp->phydev)
  404. return phy_ethtool_sset(lp->phydev, cmd);
  405. return -EINVAL;
  406. }
  407. static void bfin_mac_ethtool_getdrvinfo(struct net_device *dev,
  408. struct ethtool_drvinfo *info)
  409. {
  410. strcpy(info->driver, DRV_NAME);
  411. strcpy(info->version, DRV_VERSION);
  412. strcpy(info->fw_version, "N/A");
  413. strcpy(info->bus_info, dev_name(&dev->dev));
  414. }
  415. static const struct ethtool_ops bfin_mac_ethtool_ops = {
  416. .get_settings = bfin_mac_ethtool_getsettings,
  417. .set_settings = bfin_mac_ethtool_setsettings,
  418. .get_link = ethtool_op_get_link,
  419. .get_drvinfo = bfin_mac_ethtool_getdrvinfo,
  420. };
  421. /**************************************************************************/
  422. void setup_system_regs(struct net_device *dev)
  423. {
  424. unsigned short sysctl;
  425. /*
  426. * Odd word alignment for Receive Frame DMA word
  427. * Configure checksum support and rcve frame word alignment
  428. */
  429. sysctl = bfin_read_EMAC_SYSCTL();
  430. #if defined(BFIN_MAC_CSUM_OFFLOAD)
  431. sysctl |= RXDWA | RXCKS;
  432. #else
  433. sysctl |= RXDWA;
  434. #endif
  435. bfin_write_EMAC_SYSCTL(sysctl);
  436. bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
  437. /* Initialize the TX DMA channel registers */
  438. bfin_write_DMA2_X_COUNT(0);
  439. bfin_write_DMA2_X_MODIFY(4);
  440. bfin_write_DMA2_Y_COUNT(0);
  441. bfin_write_DMA2_Y_MODIFY(0);
  442. /* Initialize the RX DMA channel registers */
  443. bfin_write_DMA1_X_COUNT(0);
  444. bfin_write_DMA1_X_MODIFY(4);
  445. bfin_write_DMA1_Y_COUNT(0);
  446. bfin_write_DMA1_Y_MODIFY(0);
  447. }
  448. static void setup_mac_addr(u8 *mac_addr)
  449. {
  450. u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
  451. u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
  452. /* this depends on a little-endian machine */
  453. bfin_write_EMAC_ADDRLO(addr_low);
  454. bfin_write_EMAC_ADDRHI(addr_hi);
  455. }
  456. static int bfin_mac_set_mac_address(struct net_device *dev, void *p)
  457. {
  458. struct sockaddr *addr = p;
  459. if (netif_running(dev))
  460. return -EBUSY;
  461. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  462. setup_mac_addr(dev->dev_addr);
  463. return 0;
  464. }
  465. static void adjust_tx_list(void)
  466. {
  467. int timeout_cnt = MAX_TIMEOUT_CNT;
  468. if (tx_list_head->status.status_word != 0 &&
  469. current_tx_ptr != tx_list_head) {
  470. goto adjust_head; /* released something, just return; */
  471. }
  472. /*
  473. * if nothing released, check wait condition
  474. * current's next can not be the head,
  475. * otherwise the dma will not stop as we want
  476. */
  477. if (current_tx_ptr->next->next == tx_list_head) {
  478. while (tx_list_head->status.status_word == 0) {
  479. udelay(10);
  480. if (tx_list_head->status.status_word != 0 ||
  481. !(bfin_read_DMA2_IRQ_STATUS() & DMA_RUN)) {
  482. goto adjust_head;
  483. }
  484. if (timeout_cnt-- < 0) {
  485. printk(KERN_ERR DRV_NAME
  486. ": wait for adjust tx list head timeout\n");
  487. break;
  488. }
  489. }
  490. if (tx_list_head->status.status_word != 0) {
  491. goto adjust_head;
  492. }
  493. }
  494. return;
  495. adjust_head:
  496. do {
  497. tx_list_head->desc_a.config &= ~DMAEN;
  498. tx_list_head->status.status_word = 0;
  499. if (tx_list_head->skb) {
  500. dev_kfree_skb(tx_list_head->skb);
  501. tx_list_head->skb = NULL;
  502. } else {
  503. printk(KERN_ERR DRV_NAME
  504. ": no sk_buff in a transmitted frame!\n");
  505. }
  506. tx_list_head = tx_list_head->next;
  507. } while (tx_list_head->status.status_word != 0 &&
  508. current_tx_ptr != tx_list_head);
  509. return;
  510. }
  511. static int bfin_mac_hard_start_xmit(struct sk_buff *skb,
  512. struct net_device *dev)
  513. {
  514. u16 *data;
  515. u32 data_align = (unsigned long)(skb->data) & 0x3;
  516. current_tx_ptr->skb = skb;
  517. if (data_align == 0x2) {
  518. /* move skb->data to current_tx_ptr payload */
  519. data = (u16 *)(skb->data) - 1;
  520. *data = (u16)(skb->len);
  521. current_tx_ptr->desc_a.start_addr = (u32)data;
  522. /* this is important! */
  523. blackfin_dcache_flush_range((u32)data,
  524. (u32)((u8 *)data + skb->len + 4));
  525. } else {
  526. *((u16 *)(current_tx_ptr->packet)) = (u16)(skb->len);
  527. memcpy((u8 *)(current_tx_ptr->packet + 2), skb->data,
  528. skb->len);
  529. current_tx_ptr->desc_a.start_addr =
  530. (u32)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(
  534. (u32)current_tx_ptr->packet,
  535. (u32)(current_tx_ptr->packet + skb->len + 2));
  536. }
  537. /* make sure the internal data buffers in the core are drained
  538. * so that the DMA descriptors are completely written when the
  539. * DMA engine goes to fetch them below
  540. */
  541. SSYNC();
  542. /* enable this packet's dma */
  543. current_tx_ptr->desc_a.config |= DMAEN;
  544. /* tx dma is running, just return */
  545. if (bfin_read_DMA2_IRQ_STATUS() & DMA_RUN)
  546. goto out;
  547. /* tx dma is not running */
  548. bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
  549. /* dma enabled, read from memory, size is 6 */
  550. bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
  551. /* Turn on the EMAC tx */
  552. bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
  553. out:
  554. adjust_tx_list();
  555. current_tx_ptr = current_tx_ptr->next;
  556. dev->trans_start = jiffies;
  557. dev->stats.tx_packets++;
  558. dev->stats.tx_bytes += (skb->len);
  559. return NETDEV_TX_OK;
  560. }
  561. static void bfin_mac_rx(struct net_device *dev)
  562. {
  563. struct sk_buff *skb, *new_skb;
  564. unsigned short len;
  565. /* allocate a new skb for next time receive */
  566. skb = current_rx_ptr->skb;
  567. new_skb = dev_alloc_skb(PKT_BUF_SZ + NET_IP_ALIGN);
  568. if (!new_skb) {
  569. printk(KERN_NOTICE DRV_NAME
  570. ": rx: low on mem - packet dropped\n");
  571. dev->stats.rx_dropped++;
  572. goto out;
  573. }
  574. /* reserve 2 bytes for RXDWA padding */
  575. skb_reserve(new_skb, NET_IP_ALIGN);
  576. current_rx_ptr->skb = new_skb;
  577. current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
  578. /* Invidate the data cache of skb->data range when it is write back
  579. * cache. It will prevent overwritting the new data from DMA
  580. */
  581. blackfin_dcache_invalidate_range((unsigned long)new_skb->head,
  582. (unsigned long)new_skb->end);
  583. len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
  584. skb_put(skb, len);
  585. blackfin_dcache_invalidate_range((unsigned long)skb->head,
  586. (unsigned long)skb->tail);
  587. skb->protocol = eth_type_trans(skb, dev);
  588. #if defined(BFIN_MAC_CSUM_OFFLOAD)
  589. skb->csum = current_rx_ptr->status.ip_payload_csum;
  590. skb->ip_summed = CHECKSUM_COMPLETE;
  591. #endif
  592. netif_rx(skb);
  593. dev->stats.rx_packets++;
  594. dev->stats.rx_bytes += len;
  595. current_rx_ptr->status.status_word = 0x00000000;
  596. current_rx_ptr = current_rx_ptr->next;
  597. out:
  598. return;
  599. }
  600. /* interrupt routine to handle rx and error signal */
  601. static irqreturn_t bfin_mac_interrupt(int irq, void *dev_id)
  602. {
  603. struct net_device *dev = dev_id;
  604. int number = 0;
  605. get_one_packet:
  606. if (current_rx_ptr->status.status_word == 0) {
  607. /* no more new packet received */
  608. if (number == 0) {
  609. if (current_rx_ptr->next->status.status_word != 0) {
  610. current_rx_ptr = current_rx_ptr->next;
  611. goto real_rx;
  612. }
  613. }
  614. bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
  615. DMA_DONE | DMA_ERR);
  616. return IRQ_HANDLED;
  617. }
  618. real_rx:
  619. bfin_mac_rx(dev);
  620. number++;
  621. goto get_one_packet;
  622. }
  623. #ifdef CONFIG_NET_POLL_CONTROLLER
  624. static void bfin_mac_poll(struct net_device *dev)
  625. {
  626. disable_irq(IRQ_MAC_RX);
  627. bfin_mac_interrupt(IRQ_MAC_RX, dev);
  628. enable_irq(IRQ_MAC_RX);
  629. }
  630. #endif /* CONFIG_NET_POLL_CONTROLLER */
  631. static void bfin_mac_disable(void)
  632. {
  633. unsigned int opmode;
  634. opmode = bfin_read_EMAC_OPMODE();
  635. opmode &= (~RE);
  636. opmode &= (~TE);
  637. /* Turn off the EMAC */
  638. bfin_write_EMAC_OPMODE(opmode);
  639. }
  640. /*
  641. * Enable Interrupts, Receive, and Transmit
  642. */
  643. static void bfin_mac_enable(void)
  644. {
  645. u32 opmode;
  646. pr_debug("%s: %s\n", DRV_NAME, __func__);
  647. /* Set RX DMA */
  648. bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
  649. bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
  650. /* Wait MII done */
  651. bfin_mdio_poll();
  652. /* We enable only RX here */
  653. /* ASTP : Enable Automatic Pad Stripping
  654. PR : Promiscuous Mode for test
  655. PSF : Receive frames with total length less than 64 bytes.
  656. FDMODE : Full Duplex Mode
  657. LB : Internal Loopback for test
  658. RE : Receiver Enable */
  659. opmode = bfin_read_EMAC_OPMODE();
  660. if (opmode & FDMODE)
  661. opmode |= PSF;
  662. else
  663. opmode |= DRO | DC | PSF;
  664. opmode |= RE;
  665. #if defined(CONFIG_BFIN_MAC_RMII)
  666. opmode |= RMII; /* For Now only 100MBit are supported */
  667. #if (defined(CONFIG_BF537) || defined(CONFIG_BF536)) && CONFIG_BF_REV_0_2
  668. opmode |= TE;
  669. #endif
  670. #endif
  671. /* Turn on the EMAC rx */
  672. bfin_write_EMAC_OPMODE(opmode);
  673. }
  674. /* Our watchdog timed out. Called by the networking layer */
  675. static void bfin_mac_timeout(struct net_device *dev)
  676. {
  677. pr_debug("%s: %s\n", dev->name, __func__);
  678. bfin_mac_disable();
  679. /* reset tx queue */
  680. tx_list_tail = tx_list_head->next;
  681. bfin_mac_enable();
  682. /* We can accept TX packets again */
  683. dev->trans_start = jiffies;
  684. netif_wake_queue(dev);
  685. }
  686. static void bfin_mac_multicast_hash(struct net_device *dev)
  687. {
  688. u32 emac_hashhi, emac_hashlo;
  689. struct dev_mc_list *dmi = dev->mc_list;
  690. char *addrs;
  691. int i;
  692. u32 crc;
  693. emac_hashhi = emac_hashlo = 0;
  694. for (i = 0; i < dev->mc_count; i++) {
  695. addrs = dmi->dmi_addr;
  696. dmi = dmi->next;
  697. /* skip non-multicast addresses */
  698. if (!(*addrs & 1))
  699. continue;
  700. crc = ether_crc(ETH_ALEN, addrs);
  701. crc >>= 26;
  702. if (crc & 0x20)
  703. emac_hashhi |= 1 << (crc & 0x1f);
  704. else
  705. emac_hashlo |= 1 << (crc & 0x1f);
  706. }
  707. bfin_write_EMAC_HASHHI(emac_hashhi);
  708. bfin_write_EMAC_HASHLO(emac_hashlo);
  709. return;
  710. }
  711. /*
  712. * This routine will, depending on the values passed to it,
  713. * either make it accept multicast packets, go into
  714. * promiscuous mode (for TCPDUMP and cousins) or accept
  715. * a select set of multicast packets
  716. */
  717. static void bfin_mac_set_multicast_list(struct net_device *dev)
  718. {
  719. u32 sysctl;
  720. if (dev->flags & IFF_PROMISC) {
  721. printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
  722. sysctl = bfin_read_EMAC_OPMODE();
  723. sysctl |= RAF;
  724. bfin_write_EMAC_OPMODE(sysctl);
  725. } else if (dev->flags & IFF_ALLMULTI) {
  726. /* accept all multicast */
  727. sysctl = bfin_read_EMAC_OPMODE();
  728. sysctl |= PAM;
  729. bfin_write_EMAC_OPMODE(sysctl);
  730. } else if (dev->mc_count) {
  731. /* set up multicast hash table */
  732. sysctl = bfin_read_EMAC_OPMODE();
  733. sysctl |= HM;
  734. bfin_write_EMAC_OPMODE(sysctl);
  735. bfin_mac_multicast_hash(dev);
  736. } else {
  737. /* clear promisc or multicast mode */
  738. sysctl = bfin_read_EMAC_OPMODE();
  739. sysctl &= ~(RAF | PAM);
  740. bfin_write_EMAC_OPMODE(sysctl);
  741. }
  742. }
  743. /*
  744. * this puts the device in an inactive state
  745. */
  746. static void bfin_mac_shutdown(struct net_device *dev)
  747. {
  748. /* Turn off the EMAC */
  749. bfin_write_EMAC_OPMODE(0x00000000);
  750. /* Turn off the EMAC RX DMA */
  751. bfin_write_DMA1_CONFIG(0x0000);
  752. bfin_write_DMA2_CONFIG(0x0000);
  753. }
  754. /*
  755. * Open and Initialize the interface
  756. *
  757. * Set up everything, reset the card, etc..
  758. */
  759. static int bfin_mac_open(struct net_device *dev)
  760. {
  761. struct bfin_mac_local *lp = netdev_priv(dev);
  762. int retval;
  763. pr_debug("%s: %s\n", dev->name, __func__);
  764. /*
  765. * Check that the address is valid. If its not, refuse
  766. * to bring the device up. The user must specify an
  767. * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
  768. */
  769. if (!is_valid_ether_addr(dev->dev_addr)) {
  770. printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
  771. return -EINVAL;
  772. }
  773. /* initial rx and tx list */
  774. retval = desc_list_init();
  775. if (retval)
  776. return retval;
  777. phy_start(lp->phydev);
  778. phy_write(lp->phydev, MII_BMCR, BMCR_RESET);
  779. setup_system_regs(dev);
  780. setup_mac_addr(dev->dev_addr);
  781. bfin_mac_disable();
  782. bfin_mac_enable();
  783. pr_debug("hardware init finished\n");
  784. netif_start_queue(dev);
  785. netif_carrier_on(dev);
  786. return 0;
  787. }
  788. /*
  789. * this makes the board clean up everything that it can
  790. * and not talk to the outside world. Caused by
  791. * an 'ifconfig ethX down'
  792. */
  793. static int bfin_mac_close(struct net_device *dev)
  794. {
  795. struct bfin_mac_local *lp = netdev_priv(dev);
  796. pr_debug("%s: %s\n", dev->name, __func__);
  797. netif_stop_queue(dev);
  798. netif_carrier_off(dev);
  799. phy_stop(lp->phydev);
  800. phy_write(lp->phydev, MII_BMCR, BMCR_PDOWN);
  801. /* clear everything */
  802. bfin_mac_shutdown(dev);
  803. /* free the rx/tx buffers */
  804. desc_list_free();
  805. return 0;
  806. }
  807. static const struct net_device_ops bfin_mac_netdev_ops = {
  808. .ndo_open = bfin_mac_open,
  809. .ndo_stop = bfin_mac_close,
  810. .ndo_start_xmit = bfin_mac_hard_start_xmit,
  811. .ndo_set_mac_address = bfin_mac_set_mac_address,
  812. .ndo_tx_timeout = bfin_mac_timeout,
  813. .ndo_set_multicast_list = bfin_mac_set_multicast_list,
  814. .ndo_validate_addr = eth_validate_addr,
  815. .ndo_change_mtu = eth_change_mtu,
  816. #ifdef CONFIG_NET_POLL_CONTROLLER
  817. .ndo_poll_controller = bfin_mac_poll,
  818. #endif
  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. struct platform_device *pd;
  825. int rc;
  826. ndev = alloc_etherdev(sizeof(struct bfin_mac_local));
  827. if (!ndev) {
  828. dev_err(&pdev->dev, "Cannot allocate net device!\n");
  829. return -ENOMEM;
  830. }
  831. SET_NETDEV_DEV(ndev, &pdev->dev);
  832. platform_set_drvdata(pdev, ndev);
  833. lp = netdev_priv(ndev);
  834. /* Grab the MAC address in the MAC */
  835. *(__le32 *) (&(ndev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
  836. *(__le16 *) (&(ndev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
  837. /* probe mac */
  838. /*todo: how to proble? which is revision_register */
  839. bfin_write_EMAC_ADDRLO(0x12345678);
  840. if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
  841. dev_err(&pdev->dev, "Cannot detect Blackfin on-chip ethernet MAC controller!\n");
  842. rc = -ENODEV;
  843. goto out_err_probe_mac;
  844. }
  845. /*
  846. * Is it valid? (Did bootloader initialize it?)
  847. * Grab the MAC from the board somehow
  848. * this is done in the arch/blackfin/mach-bfxxx/boards/eth_mac.c
  849. */
  850. if (!is_valid_ether_addr(ndev->dev_addr))
  851. bfin_get_ether_addr(ndev->dev_addr);
  852. /* If still not valid, get a random one */
  853. if (!is_valid_ether_addr(ndev->dev_addr))
  854. random_ether_addr(ndev->dev_addr);
  855. setup_mac_addr(ndev->dev_addr);
  856. if (!pdev->dev.platform_data) {
  857. dev_err(&pdev->dev, "Cannot get platform device bfin_mii_bus!\n");
  858. rc = -ENODEV;
  859. goto out_err_probe_mac;
  860. }
  861. pd = pdev->dev.platform_data;
  862. lp->mii_bus = platform_get_drvdata(pd);
  863. lp->mii_bus->priv = ndev;
  864. rc = mii_probe(ndev);
  865. if (rc) {
  866. dev_err(&pdev->dev, "MII Probe failed!\n");
  867. goto out_err_mii_probe;
  868. }
  869. /* Fill in the fields of the device structure with ethernet values. */
  870. ether_setup(ndev);
  871. ndev->netdev_ops = &bfin_mac_netdev_ops;
  872. ndev->ethtool_ops = &bfin_mac_ethtool_ops;
  873. spin_lock_init(&lp->lock);
  874. /* now, enable interrupts */
  875. /* register irq handler */
  876. rc = request_irq(IRQ_MAC_RX, bfin_mac_interrupt,
  877. IRQF_DISABLED, "EMAC_RX", ndev);
  878. if (rc) {
  879. dev_err(&pdev->dev, "Cannot request Blackfin MAC RX IRQ!\n");
  880. rc = -EBUSY;
  881. goto out_err_request_irq;
  882. }
  883. rc = register_netdev(ndev);
  884. if (rc) {
  885. dev_err(&pdev->dev, "Cannot register net device!\n");
  886. goto out_err_reg_ndev;
  887. }
  888. /* now, print out the card info, in a short format.. */
  889. dev_info(&pdev->dev, "%s, Version %s\n", DRV_DESC, DRV_VERSION);
  890. return 0;
  891. out_err_reg_ndev:
  892. free_irq(IRQ_MAC_RX, ndev);
  893. out_err_request_irq:
  894. out_err_mii_probe:
  895. mdiobus_unregister(lp->mii_bus);
  896. mdiobus_free(lp->mii_bus);
  897. peripheral_free_list(pin_req);
  898. out_err_probe_mac:
  899. platform_set_drvdata(pdev, NULL);
  900. free_netdev(ndev);
  901. return rc;
  902. }
  903. static int __devexit bfin_mac_remove(struct platform_device *pdev)
  904. {
  905. struct net_device *ndev = platform_get_drvdata(pdev);
  906. struct bfin_mac_local *lp = netdev_priv(ndev);
  907. platform_set_drvdata(pdev, NULL);
  908. lp->mii_bus->priv = NULL;
  909. unregister_netdev(ndev);
  910. free_irq(IRQ_MAC_RX, ndev);
  911. free_netdev(ndev);
  912. peripheral_free_list(pin_req);
  913. return 0;
  914. }
  915. #ifdef CONFIG_PM
  916. static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
  917. {
  918. struct net_device *net_dev = platform_get_drvdata(pdev);
  919. if (netif_running(net_dev))
  920. bfin_mac_close(net_dev);
  921. return 0;
  922. }
  923. static int bfin_mac_resume(struct platform_device *pdev)
  924. {
  925. struct net_device *net_dev = platform_get_drvdata(pdev);
  926. if (netif_running(net_dev))
  927. bfin_mac_open(net_dev);
  928. return 0;
  929. }
  930. #else
  931. #define bfin_mac_suspend NULL
  932. #define bfin_mac_resume NULL
  933. #endif /* CONFIG_PM */
  934. static int __devinit bfin_mii_bus_probe(struct platform_device *pdev)
  935. {
  936. struct mii_bus *miibus;
  937. int rc, i;
  938. /*
  939. * We are setting up a network card,
  940. * so set the GPIO pins to Ethernet mode
  941. */
  942. rc = peripheral_request_list(pin_req, DRV_NAME);
  943. if (rc) {
  944. dev_err(&pdev->dev, "Requesting peripherals failed!\n");
  945. return rc;
  946. }
  947. rc = -ENOMEM;
  948. miibus = mdiobus_alloc();
  949. if (miibus == NULL)
  950. goto out_err_alloc;
  951. miibus->read = bfin_mdiobus_read;
  952. miibus->write = bfin_mdiobus_write;
  953. miibus->reset = bfin_mdiobus_reset;
  954. miibus->parent = &pdev->dev;
  955. miibus->name = "bfin_mii_bus";
  956. snprintf(miibus->id, MII_BUS_ID_SIZE, "0");
  957. miibus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  958. if (miibus->irq == NULL)
  959. goto out_err_alloc;
  960. for (i = 0; i < PHY_MAX_ADDR; ++i)
  961. miibus->irq[i] = PHY_POLL;
  962. rc = mdiobus_register(miibus);
  963. if (rc) {
  964. dev_err(&pdev->dev, "Cannot register MDIO bus!\n");
  965. goto out_err_mdiobus_register;
  966. }
  967. platform_set_drvdata(pdev, miibus);
  968. return 0;
  969. out_err_mdiobus_register:
  970. mdiobus_free(miibus);
  971. out_err_alloc:
  972. peripheral_free_list(pin_req);
  973. return rc;
  974. }
  975. static int __devexit bfin_mii_bus_remove(struct platform_device *pdev)
  976. {
  977. struct mii_bus *miibus = platform_get_drvdata(pdev);
  978. platform_set_drvdata(pdev, NULL);
  979. mdiobus_unregister(miibus);
  980. mdiobus_free(miibus);
  981. peripheral_free_list(pin_req);
  982. return 0;
  983. }
  984. static struct platform_driver bfin_mii_bus_driver = {
  985. .probe = bfin_mii_bus_probe,
  986. .remove = __devexit_p(bfin_mii_bus_remove),
  987. .driver = {
  988. .name = "bfin_mii_bus",
  989. .owner = THIS_MODULE,
  990. },
  991. };
  992. static struct platform_driver bfin_mac_driver = {
  993. .probe = bfin_mac_probe,
  994. .remove = __devexit_p(bfin_mac_remove),
  995. .resume = bfin_mac_resume,
  996. .suspend = bfin_mac_suspend,
  997. .driver = {
  998. .name = DRV_NAME,
  999. .owner = THIS_MODULE,
  1000. },
  1001. };
  1002. static int __init bfin_mac_init(void)
  1003. {
  1004. int ret;
  1005. ret = platform_driver_register(&bfin_mii_bus_driver);
  1006. if (!ret)
  1007. return platform_driver_register(&bfin_mac_driver);
  1008. return -ENODEV;
  1009. }
  1010. module_init(bfin_mac_init);
  1011. static void __exit bfin_mac_cleanup(void)
  1012. {
  1013. platform_driver_unregister(&bfin_mac_driver);
  1014. platform_driver_unregister(&bfin_mii_bus_driver);
  1015. }
  1016. module_exit(bfin_mac_cleanup);