bfin_mac.c 24 KB

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