bfin_mac.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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 + 2);
  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, 2);
  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 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 mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
  237. {
  238. mdio_poll();
  239. /* read mode */
  240. bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
  241. SET_REGAD((u16) regnum) |
  242. STABUSY);
  243. 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 mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
  248. u16 value)
  249. {
  250. 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. mdio_poll();
  258. return 0;
  259. }
  260. static int 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. netif_schedule(dev);
  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() & PHYCLKOE))
  334. bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
  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, phydev->dev.bus_id, &bfin_mac_adjust_link, 0,
  356. PHY_INTERFACE_MODE_RMII);
  357. #else
  358. phydev = phy_connect(dev, phydev->dev.bus_id, &bfin_mac_adjust_link, 0,
  359. 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, phydev->dev.bus_id, 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->dev.bus_id);
  414. }
  415. static 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. mdelay(1);
  480. if (tx_list_head->status.status_word != 0
  481. || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) {
  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. unsigned int data;
  515. current_tx_ptr->skb = skb;
  516. /*
  517. * Is skb->data always 16-bit aligned?
  518. * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)?
  519. */
  520. if ((((unsigned int)(skb->data)) & 0x02) == 2) {
  521. /* move skb->data to current_tx_ptr payload */
  522. data = (unsigned int)(skb->data) - 2;
  523. *((unsigned short *)data) = (unsigned short)(skb->len);
  524. current_tx_ptr->desc_a.start_addr = (unsigned long)data;
  525. /* this is important! */
  526. blackfin_dcache_flush_range(data, (data + (skb->len)) + 2);
  527. } else {
  528. *((unsigned short *)(current_tx_ptr->packet)) =
  529. (unsigned short)(skb->len);
  530. memcpy((char *)(current_tx_ptr->packet + 2), skb->data,
  531. (skb->len));
  532. current_tx_ptr->desc_a.start_addr =
  533. (unsigned long)current_tx_ptr->packet;
  534. if (current_tx_ptr->status.status_word != 0)
  535. current_tx_ptr->status.status_word = 0;
  536. blackfin_dcache_flush_range((unsigned int)current_tx_ptr->
  537. packet,
  538. (unsigned int)(current_tx_ptr->
  539. packet + skb->len) +
  540. 2);
  541. }
  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() & 0x08)
  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 0;
  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 + 2);
  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, 2);
  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. dev->last_rx = jiffies;
  588. skb->dev = dev;
  589. skb->protocol = eth_type_trans(skb, dev);
  590. #if defined(BFIN_MAC_CSUM_OFFLOAD)
  591. skb->csum = current_rx_ptr->status.ip_payload_csum;
  592. skb->ip_summed = CHECKSUM_COMPLETE;
  593. #endif
  594. netif_rx(skb);
  595. dev->stats.rx_packets++;
  596. dev->stats.rx_bytes += len;
  597. current_rx_ptr->status.status_word = 0x00000000;
  598. current_rx_ptr = current_rx_ptr->next;
  599. out:
  600. return;
  601. }
  602. /* interrupt routine to handle rx and error signal */
  603. static irqreturn_t bfin_mac_interrupt(int irq, void *dev_id)
  604. {
  605. struct net_device *dev = dev_id;
  606. int number = 0;
  607. get_one_packet:
  608. if (current_rx_ptr->status.status_word == 0) {
  609. /* no more new packet received */
  610. if (number == 0) {
  611. if (current_rx_ptr->next->status.status_word != 0) {
  612. current_rx_ptr = current_rx_ptr->next;
  613. goto real_rx;
  614. }
  615. }
  616. bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
  617. DMA_DONE | DMA_ERR);
  618. return IRQ_HANDLED;
  619. }
  620. real_rx:
  621. bfin_mac_rx(dev);
  622. number++;
  623. goto get_one_packet;
  624. }
  625. #ifdef CONFIG_NET_POLL_CONTROLLER
  626. static void bfin_mac_poll(struct net_device *dev)
  627. {
  628. disable_irq(IRQ_MAC_RX);
  629. bfin_mac_interrupt(IRQ_MAC_RX, dev);
  630. enable_irq(IRQ_MAC_RX);
  631. }
  632. #endif /* CONFIG_NET_POLL_CONTROLLER */
  633. static void bfin_mac_disable(void)
  634. {
  635. unsigned int opmode;
  636. opmode = bfin_read_EMAC_OPMODE();
  637. opmode &= (~RE);
  638. opmode &= (~TE);
  639. /* Turn off the EMAC */
  640. bfin_write_EMAC_OPMODE(opmode);
  641. }
  642. /*
  643. * Enable Interrupts, Receive, and Transmit
  644. */
  645. static void bfin_mac_enable(void)
  646. {
  647. u32 opmode;
  648. pr_debug("%s: %s\n", DRV_NAME, __FUNCTION__);
  649. /* Set RX DMA */
  650. bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
  651. bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
  652. /* Wait MII done */
  653. mdio_poll();
  654. /* We enable only RX here */
  655. /* ASTP : Enable Automatic Pad Stripping
  656. PR : Promiscuous Mode for test
  657. PSF : Receive frames with total length less than 64 bytes.
  658. FDMODE : Full Duplex Mode
  659. LB : Internal Loopback for test
  660. RE : Receiver Enable */
  661. opmode = bfin_read_EMAC_OPMODE();
  662. if (opmode & FDMODE)
  663. opmode |= PSF;
  664. else
  665. opmode |= DRO | DC | PSF;
  666. opmode |= RE;
  667. #if defined(CONFIG_BFIN_MAC_RMII)
  668. opmode |= RMII; /* For Now only 100MBit are supported */
  669. #if (defined(CONFIG_BF537) || defined(CONFIG_BF536)) && CONFIG_BF_REV_0_2
  670. opmode |= TE;
  671. #endif
  672. #endif
  673. /* Turn on the EMAC rx */
  674. bfin_write_EMAC_OPMODE(opmode);
  675. }
  676. /* Our watchdog timed out. Called by the networking layer */
  677. static void bfin_mac_timeout(struct net_device *dev)
  678. {
  679. pr_debug("%s: %s\n", dev->name, __FUNCTION__);
  680. bfin_mac_disable();
  681. /* reset tx queue */
  682. tx_list_tail = tx_list_head->next;
  683. bfin_mac_enable();
  684. /* We can accept TX packets again */
  685. dev->trans_start = jiffies;
  686. netif_wake_queue(dev);
  687. }
  688. static void bfin_mac_multicast_hash(struct net_device *dev)
  689. {
  690. u32 emac_hashhi, emac_hashlo;
  691. struct dev_mc_list *dmi = dev->mc_list;
  692. char *addrs;
  693. int i;
  694. u32 crc;
  695. emac_hashhi = emac_hashlo = 0;
  696. for (i = 0; i < dev->mc_count; i++) {
  697. addrs = dmi->dmi_addr;
  698. dmi = dmi->next;
  699. /* skip non-multicast addresses */
  700. if (!(*addrs & 1))
  701. continue;
  702. crc = ether_crc(ETH_ALEN, addrs);
  703. crc >>= 26;
  704. if (crc & 0x20)
  705. emac_hashhi |= 1 << (crc & 0x1f);
  706. else
  707. emac_hashlo |= 1 << (crc & 0x1f);
  708. }
  709. bfin_write_EMAC_HASHHI(emac_hashhi);
  710. bfin_write_EMAC_HASHLO(emac_hashlo);
  711. return;
  712. }
  713. /*
  714. * This routine will, depending on the values passed to it,
  715. * either make it accept multicast packets, go into
  716. * promiscuous mode (for TCPDUMP and cousins) or accept
  717. * a select set of multicast packets
  718. */
  719. static void bfin_mac_set_multicast_list(struct net_device *dev)
  720. {
  721. u32 sysctl;
  722. if (dev->flags & IFF_PROMISC) {
  723. printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
  724. sysctl = bfin_read_EMAC_OPMODE();
  725. sysctl |= RAF;
  726. bfin_write_EMAC_OPMODE(sysctl);
  727. } else if (dev->flags & IFF_ALLMULTI) {
  728. /* accept all multicast */
  729. sysctl = bfin_read_EMAC_OPMODE();
  730. sysctl |= PAM;
  731. bfin_write_EMAC_OPMODE(sysctl);
  732. } else if (dev->mc_count) {
  733. /* set up multicast hash table */
  734. sysctl = bfin_read_EMAC_OPMODE();
  735. sysctl |= HM;
  736. bfin_write_EMAC_OPMODE(sysctl);
  737. bfin_mac_multicast_hash(dev);
  738. } else {
  739. /* clear promisc or multicast mode */
  740. sysctl = bfin_read_EMAC_OPMODE();
  741. sysctl &= ~(RAF | PAM);
  742. bfin_write_EMAC_OPMODE(sysctl);
  743. }
  744. }
  745. /*
  746. * this puts the device in an inactive state
  747. */
  748. static void bfin_mac_shutdown(struct net_device *dev)
  749. {
  750. /* Turn off the EMAC */
  751. bfin_write_EMAC_OPMODE(0x00000000);
  752. /* Turn off the EMAC RX DMA */
  753. bfin_write_DMA1_CONFIG(0x0000);
  754. bfin_write_DMA2_CONFIG(0x0000);
  755. }
  756. /*
  757. * Open and Initialize the interface
  758. *
  759. * Set up everything, reset the card, etc..
  760. */
  761. static int bfin_mac_open(struct net_device *dev)
  762. {
  763. struct bfin_mac_local *lp = netdev_priv(dev);
  764. int retval;
  765. pr_debug("%s: %s\n", dev->name, __FUNCTION__);
  766. /*
  767. * Check that the address is valid. If its not, refuse
  768. * to bring the device up. The user must specify an
  769. * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
  770. */
  771. if (!is_valid_ether_addr(dev->dev_addr)) {
  772. printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
  773. return -EINVAL;
  774. }
  775. /* initial rx and tx list */
  776. retval = desc_list_init();
  777. if (retval)
  778. return retval;
  779. phy_start(lp->phydev);
  780. phy_write(lp->phydev, MII_BMCR, BMCR_RESET);
  781. setup_system_regs(dev);
  782. bfin_mac_disable();
  783. bfin_mac_enable();
  784. pr_debug("hardware init finished\n");
  785. netif_start_queue(dev);
  786. netif_carrier_on(dev);
  787. return 0;
  788. }
  789. /*
  790. *
  791. * this makes the board clean up everything that it can
  792. * and not talk to the outside world. Caused by
  793. * an 'ifconfig ethX down'
  794. */
  795. static int bfin_mac_close(struct net_device *dev)
  796. {
  797. struct bfin_mac_local *lp = netdev_priv(dev);
  798. pr_debug("%s: %s\n", dev->name, __FUNCTION__);
  799. netif_stop_queue(dev);
  800. netif_carrier_off(dev);
  801. phy_stop(lp->phydev);
  802. phy_write(lp->phydev, MII_BMCR, BMCR_PDOWN);
  803. /* clear everything */
  804. bfin_mac_shutdown(dev);
  805. /* free the rx/tx buffers */
  806. desc_list_free();
  807. return 0;
  808. }
  809. static int __init bfin_mac_probe(struct platform_device *pdev)
  810. {
  811. struct net_device *ndev;
  812. struct bfin_mac_local *lp;
  813. int rc, i;
  814. ndev = alloc_etherdev(sizeof(struct bfin_mac_local));
  815. if (!ndev) {
  816. dev_err(&pdev->dev, "Cannot allocate net device!\n");
  817. return -ENOMEM;
  818. }
  819. SET_NETDEV_DEV(ndev, &pdev->dev);
  820. platform_set_drvdata(pdev, ndev);
  821. lp = netdev_priv(ndev);
  822. /* Grab the MAC address in the MAC */
  823. *(__le32 *) (&(ndev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
  824. *(__le16 *) (&(ndev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
  825. /* probe mac */
  826. /*todo: how to proble? which is revision_register */
  827. bfin_write_EMAC_ADDRLO(0x12345678);
  828. if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
  829. dev_err(&pdev->dev, "Cannot detect Blackfin on-chip ethernet MAC controller!\n");
  830. rc = -ENODEV;
  831. goto out_err_probe_mac;
  832. }
  833. /* set the GPIO pins to Ethernet mode */
  834. rc = peripheral_request_list(pin_req, DRV_NAME);
  835. if (rc) {
  836. dev_err(&pdev->dev, "Requesting peripherals failed!\n");
  837. rc = -EFAULT;
  838. goto out_err_setup_pin_mux;
  839. }
  840. /*
  841. * Is it valid? (Did bootloader initialize it?)
  842. * Grab the MAC from the board somehow
  843. * this is done in the arch/blackfin/mach-bfxxx/boards/eth_mac.c
  844. */
  845. if (!is_valid_ether_addr(ndev->dev_addr))
  846. bfin_get_ether_addr(ndev->dev_addr);
  847. /* If still not valid, get a random one */
  848. if (!is_valid_ether_addr(ndev->dev_addr))
  849. random_ether_addr(ndev->dev_addr);
  850. setup_mac_addr(ndev->dev_addr);
  851. /* MDIO bus initial */
  852. lp->mii_bus.priv = ndev;
  853. lp->mii_bus.read = mdiobus_read;
  854. lp->mii_bus.write = mdiobus_write;
  855. lp->mii_bus.reset = mdiobus_reset;
  856. lp->mii_bus.name = "bfin_mac_mdio";
  857. snprintf(lp->mii_bus.id, MII_BUS_ID_SIZE, "0");
  858. lp->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  859. for (i = 0; i < PHY_MAX_ADDR; ++i)
  860. lp->mii_bus.irq[i] = PHY_POLL;
  861. rc = mdiobus_register(&lp->mii_bus);
  862. if (rc) {
  863. dev_err(&pdev->dev, "Cannot register MDIO bus!\n");
  864. goto out_err_mdiobus_register;
  865. }
  866. rc = mii_probe(ndev);
  867. if (rc) {
  868. dev_err(&pdev->dev, "MII Probe failed!\n");
  869. goto out_err_mii_probe;
  870. }
  871. /* Fill in the fields of the device structure with ethernet values. */
  872. ether_setup(ndev);
  873. ndev->open = bfin_mac_open;
  874. ndev->stop = bfin_mac_close;
  875. ndev->hard_start_xmit = bfin_mac_hard_start_xmit;
  876. ndev->set_mac_address = bfin_mac_set_mac_address;
  877. ndev->tx_timeout = bfin_mac_timeout;
  878. ndev->set_multicast_list = bfin_mac_set_multicast_list;
  879. #ifdef CONFIG_NET_POLL_CONTROLLER
  880. ndev->poll_controller = bfin_mac_poll;
  881. #endif
  882. ndev->ethtool_ops = &bfin_mac_ethtool_ops;
  883. spin_lock_init(&lp->lock);
  884. /* now, enable interrupts */
  885. /* register irq handler */
  886. rc = request_irq(IRQ_MAC_RX, bfin_mac_interrupt,
  887. IRQF_DISABLED | IRQF_SHARED, "EMAC_RX", ndev);
  888. if (rc) {
  889. dev_err(&pdev->dev, "Cannot request Blackfin MAC RX IRQ!\n");
  890. rc = -EBUSY;
  891. goto out_err_request_irq;
  892. }
  893. rc = register_netdev(ndev);
  894. if (rc) {
  895. dev_err(&pdev->dev, "Cannot register net device!\n");
  896. goto out_err_reg_ndev;
  897. }
  898. /* now, print out the card info, in a short format.. */
  899. dev_info(&pdev->dev, "%s, Version %s\n", DRV_DESC, DRV_VERSION);
  900. return 0;
  901. out_err_reg_ndev:
  902. free_irq(IRQ_MAC_RX, ndev);
  903. out_err_request_irq:
  904. out_err_mii_probe:
  905. mdiobus_unregister(&lp->mii_bus);
  906. out_err_mdiobus_register:
  907. peripheral_free_list(pin_req);
  908. out_err_setup_pin_mux:
  909. out_err_probe_mac:
  910. platform_set_drvdata(pdev, NULL);
  911. free_netdev(ndev);
  912. return rc;
  913. }
  914. static int bfin_mac_remove(struct platform_device *pdev)
  915. {
  916. struct net_device *ndev = platform_get_drvdata(pdev);
  917. struct bfin_mac_local *lp = netdev_priv(ndev);
  918. platform_set_drvdata(pdev, NULL);
  919. mdiobus_unregister(&lp->mii_bus);
  920. unregister_netdev(ndev);
  921. free_irq(IRQ_MAC_RX, ndev);
  922. free_netdev(ndev);
  923. peripheral_free_list(pin_req);
  924. return 0;
  925. }
  926. #ifdef CONFIG_PM
  927. static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
  928. {
  929. struct net_device *net_dev = platform_get_drvdata(pdev);
  930. if (netif_running(net_dev))
  931. bfin_mac_close(net_dev);
  932. return 0;
  933. }
  934. static int bfin_mac_resume(struct platform_device *pdev)
  935. {
  936. struct net_device *net_dev = platform_get_drvdata(pdev);
  937. if (netif_running(net_dev))
  938. bfin_mac_open(net_dev);
  939. return 0;
  940. }
  941. #else
  942. #define bfin_mac_suspend NULL
  943. #define bfin_mac_resume NULL
  944. #endif /* CONFIG_PM */
  945. static struct platform_driver bfin_mac_driver = {
  946. .probe = bfin_mac_probe,
  947. .remove = bfin_mac_remove,
  948. .resume = bfin_mac_resume,
  949. .suspend = bfin_mac_suspend,
  950. .driver = {
  951. .name = DRV_NAME,
  952. .owner = THIS_MODULE,
  953. },
  954. };
  955. static int __init bfin_mac_init(void)
  956. {
  957. return platform_driver_register(&bfin_mac_driver);
  958. }
  959. module_init(bfin_mac_init);
  960. static void __exit bfin_mac_cleanup(void)
  961. {
  962. platform_driver_unregister(&bfin_mac_driver);
  963. }
  964. module_exit(bfin_mac_cleanup);