altera_tse.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /*
  2. * Altera 10/100/1000 triple speed ethernet mac driver
  3. *
  4. * Copyright (C) 2008 Altera Corporation.
  5. * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <config.h>
  12. #include <common.h>
  13. #include <malloc.h>
  14. #include <net.h>
  15. #include <command.h>
  16. #include <asm/cache.h>
  17. #include <asm/dma-mapping.h>
  18. #include <miiphy.h>
  19. #include "altera_tse.h"
  20. /* sgdma debug - print descriptor */
  21. static void alt_sgdma_print_desc(volatile struct alt_sgdma_descriptor *desc)
  22. {
  23. debug("SGDMA DEBUG :\n");
  24. debug("desc->source : 0x%x \n", (unsigned int)desc->source);
  25. debug("desc->destination : 0x%x \n", (unsigned int)desc->destination);
  26. debug("desc->next : 0x%x \n", (unsigned int)desc->next);
  27. debug("desc->source_pad : 0x%x \n", (unsigned int)desc->source_pad);
  28. debug("desc->destination_pad : 0x%x \n",
  29. (unsigned int)desc->destination_pad);
  30. debug("desc->next_pad : 0x%x \n", (unsigned int)desc->next_pad);
  31. debug("desc->bytes_to_transfer : 0x%x \n",
  32. (unsigned int)desc->bytes_to_transfer);
  33. debug("desc->actual_bytes_transferred : 0x%x \n",
  34. (unsigned int)desc->actual_bytes_transferred);
  35. debug("desc->descriptor_status : 0x%x \n",
  36. (unsigned int)desc->descriptor_status);
  37. debug("desc->descriptor_control : 0x%x \n",
  38. (unsigned int)desc->descriptor_control);
  39. }
  40. /* This is a generic routine that the SGDMA mode-specific routines
  41. * call to populate a descriptor.
  42. * arg1 :pointer to first SGDMA descriptor.
  43. * arg2 :pointer to next SGDMA descriptor.
  44. * arg3 :Address to where data to be written.
  45. * arg4 :Address from where data to be read.
  46. * arg5 :no of byte to transaction.
  47. * arg6 :variable indicating to generate start of packet or not
  48. * arg7 :read fixed
  49. * arg8 :write fixed
  50. * arg9 :read burst
  51. * arg10 :write burst
  52. * arg11 :atlantic_channel number
  53. */
  54. static void alt_sgdma_construct_descriptor_burst(
  55. volatile struct alt_sgdma_descriptor *desc,
  56. volatile struct alt_sgdma_descriptor *next,
  57. unsigned int *read_addr,
  58. unsigned int *write_addr,
  59. unsigned short length_or_eop,
  60. int generate_eop,
  61. int read_fixed,
  62. int write_fixed_or_sop,
  63. int read_burst,
  64. int write_burst,
  65. unsigned char atlantic_channel)
  66. {
  67. /*
  68. * Mark the "next" descriptor as "not" owned by hardware. This prevents
  69. * The SGDMA controller from continuing to process the chain. This is
  70. * done as a single IO write to bypass cache, without flushing
  71. * the entire descriptor, since only the 8-bit descriptor status must
  72. * be flushed.
  73. */
  74. if (!next)
  75. debug("Next descriptor not defined!!\n");
  76. next->descriptor_control = (next->descriptor_control &
  77. ~ALT_SGDMA_DESCRIPTOR_CONTROL_OWNED_BY_HW_MSK);
  78. desc->source = (unsigned int *)((unsigned int)read_addr & 0x1FFFFFFF);
  79. desc->destination =
  80. (unsigned int *)((unsigned int)write_addr & 0x1FFFFFFF);
  81. desc->next = (unsigned int *)((unsigned int)next & 0x1FFFFFFF);
  82. desc->source_pad = 0x0;
  83. desc->destination_pad = 0x0;
  84. desc->next_pad = 0x0;
  85. desc->bytes_to_transfer = length_or_eop;
  86. desc->actual_bytes_transferred = 0;
  87. desc->descriptor_status = 0x0;
  88. /* SGDMA burst not currently supported */
  89. desc->read_burst = 0;
  90. desc->write_burst = 0;
  91. /*
  92. * Set the descriptor control block as follows:
  93. * - Set "owned by hardware" bit
  94. * - Optionally set "generate EOP" bit
  95. * - Optionally set the "read from fixed address" bit
  96. * - Optionally set the "write to fixed address bit (which serves
  97. * serves as a "generate SOP" control bit in memory-to-stream mode).
  98. * - Set the 4-bit atlantic channel, if specified
  99. *
  100. * Note this step is performed after all other descriptor information
  101. * has been filled out so that, if the controller already happens to be
  102. * pointing at this descriptor, it will not run (via the "owned by
  103. * hardware" bit) until all other descriptor has been set up.
  104. */
  105. desc->descriptor_control =
  106. ((ALT_SGDMA_DESCRIPTOR_CONTROL_OWNED_BY_HW_MSK) |
  107. (generate_eop ?
  108. ALT_SGDMA_DESCRIPTOR_CONTROL_GENERATE_EOP_MSK : 0x0) |
  109. (read_fixed ?
  110. ALT_SGDMA_DESCRIPTOR_CONTROL_READ_FIXED_ADDRESS_MSK : 0x0) |
  111. (write_fixed_or_sop ?
  112. ALT_SGDMA_DESCRIPTOR_CONTROL_WRITE_FIXED_ADDRESS_MSK : 0x0) |
  113. (atlantic_channel ? ((atlantic_channel & 0x0F) << 3) : 0)
  114. );
  115. }
  116. static int alt_sgdma_do_sync_transfer(volatile struct alt_sgdma_registers *dev,
  117. volatile struct alt_sgdma_descriptor *desc)
  118. {
  119. unsigned int status;
  120. int counter = 0;
  121. /* Wait for any pending transfers to complete */
  122. alt_sgdma_print_desc(desc);
  123. status = dev->status;
  124. counter = 0;
  125. while (dev->status & ALT_SGDMA_STATUS_BUSY_MSK) {
  126. if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  127. break;
  128. }
  129. if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  130. debug("Timeout waiting sgdma in do sync!\n");
  131. /*
  132. * Clear any (previous) status register information
  133. * that might occlude our error checking later.
  134. */
  135. dev->status = 0xFF;
  136. /* Point the controller at the descriptor */
  137. dev->next_descriptor_pointer = (unsigned int)desc & 0x1FFFFFFF;
  138. debug("next desc in sgdma 0x%x\n",
  139. (unsigned int)dev->next_descriptor_pointer);
  140. /*
  141. * Set up SGDMA controller to:
  142. * - Disable interrupt generation
  143. * - Run once a valid descriptor is written to controller
  144. * - Stop on an error with any particular descriptor
  145. */
  146. dev->control = (ALT_SGDMA_CONTROL_RUN_MSK |
  147. ALT_SGDMA_CONTROL_STOP_DMA_ER_MSK);
  148. /* Wait for the descriptor (chain) to complete */
  149. status = dev->status;
  150. debug("wait for sgdma....");
  151. while (dev->status & ALT_SGDMA_STATUS_BUSY_MSK)
  152. ;
  153. debug("done\n");
  154. /* Clear Run */
  155. dev->control = (dev->control & (~ALT_SGDMA_CONTROL_RUN_MSK));
  156. /* Get & clear status register contents */
  157. status = dev->status;
  158. dev->status = 0xFF;
  159. /* we really should check if the transfer completes properly */
  160. debug("tx sgdma status = 0x%x", status);
  161. return 0;
  162. }
  163. static int alt_sgdma_do_async_transfer(volatile struct alt_sgdma_registers *dev,
  164. volatile struct alt_sgdma_descriptor *desc)
  165. {
  166. unsigned int status;
  167. int counter = 0;
  168. /* Wait for any pending transfers to complete */
  169. alt_sgdma_print_desc(desc);
  170. status = dev->status;
  171. counter = 0;
  172. while (dev->status & ALT_SGDMA_STATUS_BUSY_MSK) {
  173. if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  174. break;
  175. }
  176. if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  177. debug("Timeout waiting sgdma in do async!\n");
  178. /*
  179. * Clear the RUN bit in the control register. This is needed
  180. * to restart the SGDMA engine later on.
  181. */
  182. dev->control = 0;
  183. /*
  184. * Clear any (previous) status register information
  185. * that might occlude our error checking later.
  186. */
  187. dev->status = 0xFF;
  188. /* Point the controller at the descriptor */
  189. dev->next_descriptor_pointer = (unsigned int)desc & 0x1FFFFFFF;
  190. /*
  191. * Set up SGDMA controller to:
  192. * - Disable interrupt generation
  193. * - Run once a valid descriptor is written to controller
  194. * - Stop on an error with any particular descriptor
  195. */
  196. dev->control = (ALT_SGDMA_CONTROL_RUN_MSK |
  197. ALT_SGDMA_CONTROL_STOP_DMA_ER_MSK);
  198. /* we really should check if the transfer completes properly */
  199. return 0;
  200. }
  201. /* u-boot interface */
  202. static int tse_adjust_link(struct altera_tse_priv *priv)
  203. {
  204. unsigned int refvar;
  205. refvar = priv->mac_dev->command_config.image;
  206. if (!(priv->duplexity))
  207. refvar |= ALTERA_TSE_CMD_HD_ENA_MSK;
  208. else
  209. refvar &= ~ALTERA_TSE_CMD_HD_ENA_MSK;
  210. switch (priv->speed) {
  211. case 1000:
  212. refvar |= ALTERA_TSE_CMD_ETH_SPEED_MSK;
  213. refvar &= ~ALTERA_TSE_CMD_ENA_10_MSK;
  214. break;
  215. case 100:
  216. refvar &= ~ALTERA_TSE_CMD_ETH_SPEED_MSK;
  217. refvar &= ~ALTERA_TSE_CMD_ENA_10_MSK;
  218. break;
  219. case 10:
  220. refvar &= ~ALTERA_TSE_CMD_ETH_SPEED_MSK;
  221. refvar |= ALTERA_TSE_CMD_ENA_10_MSK;
  222. break;
  223. }
  224. priv->mac_dev->command_config.image = refvar;
  225. return 0;
  226. }
  227. static int tse_eth_send(struct eth_device *dev,
  228. volatile void *packet, int length)
  229. {
  230. struct altera_tse_priv *priv = dev->priv;
  231. volatile struct alt_sgdma_registers *tx_sgdma = priv->sgdma_tx;
  232. volatile struct alt_sgdma_descriptor *tx_desc =
  233. (volatile struct alt_sgdma_descriptor *)priv->tx_desc;
  234. volatile struct alt_sgdma_descriptor *tx_desc_cur =
  235. (volatile struct alt_sgdma_descriptor *)&tx_desc[0];
  236. flush_dcache((unsigned long)packet, length);
  237. alt_sgdma_construct_descriptor_burst(
  238. (volatile struct alt_sgdma_descriptor *)&tx_desc[0],
  239. (volatile struct alt_sgdma_descriptor *)&tx_desc[1],
  240. (unsigned int *)packet, /* read addr */
  241. (unsigned int *)0,
  242. length, /* length or EOP ,will change for each tx */
  243. 0x1, /* gen eop */
  244. 0x0, /* read fixed */
  245. 0x1, /* write fixed or sop */
  246. 0x0, /* read burst */
  247. 0x0, /* write burst */
  248. 0x0 /* channel */
  249. );
  250. debug("TX Packet @ 0x%x,0x%x bytes", (unsigned int)packet, length);
  251. /* send the packet */
  252. debug("sending packet\n");
  253. alt_sgdma_do_sync_transfer(tx_sgdma, tx_desc_cur);
  254. debug("sent %d bytes\n", tx_desc_cur->actual_bytes_transferred);
  255. return tx_desc_cur->actual_bytes_transferred;
  256. }
  257. static int tse_eth_rx(struct eth_device *dev)
  258. {
  259. int packet_length = 0;
  260. struct altera_tse_priv *priv = dev->priv;
  261. volatile struct alt_sgdma_descriptor *rx_desc =
  262. (volatile struct alt_sgdma_descriptor *)priv->rx_desc;
  263. volatile struct alt_sgdma_descriptor *rx_desc_cur = &rx_desc[0];
  264. if (rx_desc_cur->descriptor_status &
  265. ALT_SGDMA_DESCRIPTOR_STATUS_TERMINATED_BY_EOP_MSK) {
  266. debug("got packet\n");
  267. packet_length = rx_desc->actual_bytes_transferred;
  268. NetReceive(NetRxPackets[0], packet_length);
  269. /* start descriptor again */
  270. flush_dcache((unsigned long)(NetRxPackets[0]), PKTSIZE_ALIGN);
  271. alt_sgdma_construct_descriptor_burst(
  272. (volatile struct alt_sgdma_descriptor *)&rx_desc[0],
  273. (volatile struct alt_sgdma_descriptor *)&rx_desc[1],
  274. (unsigned int)0x0, /* read addr */
  275. (unsigned int *)NetRxPackets[0],
  276. 0x0, /* length or EOP */
  277. 0x0, /* gen eop */
  278. 0x0, /* read fixed */
  279. 0x0, /* write fixed or sop */
  280. 0x0, /* read burst */
  281. 0x0, /* write burst */
  282. 0x0 /* channel */
  283. );
  284. /* setup the sgdma */
  285. alt_sgdma_do_async_transfer(priv->sgdma_rx, &rx_desc[0]);
  286. }
  287. return -1;
  288. }
  289. static void tse_eth_halt(struct eth_device *dev)
  290. {
  291. /* don't do anything! */
  292. /* this gets called after each uboot */
  293. /* network command. don't need to reset the thing all of the time */
  294. }
  295. static void tse_eth_reset(struct eth_device *dev)
  296. {
  297. /* stop sgdmas, disable tse receive */
  298. struct altera_tse_priv *priv = dev->priv;
  299. volatile struct alt_tse_mac *mac_dev = priv->mac_dev;
  300. volatile struct alt_sgdma_registers *rx_sgdma = priv->sgdma_rx;
  301. volatile struct alt_sgdma_registers *tx_sgdma = priv->sgdma_tx;
  302. int counter;
  303. volatile struct alt_sgdma_descriptor *rx_desc =
  304. (volatile struct alt_sgdma_descriptor *)&priv->rx_desc[0];
  305. /* clear rx desc & wait for sgdma to complete */
  306. rx_desc->descriptor_control = 0;
  307. rx_sgdma->control = 0;
  308. counter = 0;
  309. while (rx_sgdma->status & ALT_SGDMA_STATUS_BUSY_MSK) {
  310. if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  311. break;
  312. }
  313. if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR) {
  314. debug("Timeout waiting for rx sgdma!\n");
  315. rx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
  316. rx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
  317. }
  318. counter = 0;
  319. tx_sgdma->control = 0;
  320. while (tx_sgdma->status & ALT_SGDMA_STATUS_BUSY_MSK) {
  321. if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  322. break;
  323. }
  324. if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR) {
  325. debug("Timeout waiting for tx sgdma!\n");
  326. tx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
  327. tx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
  328. }
  329. /* reset the mac */
  330. mac_dev->command_config.bits.transmit_enable = 1;
  331. mac_dev->command_config.bits.receive_enable = 1;
  332. mac_dev->command_config.bits.software_reset = 1;
  333. counter = 0;
  334. while (mac_dev->command_config.bits.software_reset) {
  335. if (counter++ > ALT_TSE_SW_RESET_WATCHDOG_CNTR)
  336. break;
  337. }
  338. if (counter >= ALT_TSE_SW_RESET_WATCHDOG_CNTR)
  339. debug("TSEMAC SW reset bit never cleared!\n");
  340. }
  341. static int tse_mdio_read(struct altera_tse_priv *priv, unsigned int regnum)
  342. {
  343. volatile struct alt_tse_mac *mac_dev;
  344. unsigned int *mdio_regs;
  345. unsigned int data;
  346. u16 value;
  347. mac_dev = priv->mac_dev;
  348. /* set mdio address */
  349. mac_dev->mdio_phy1_addr = priv->phyaddr;
  350. mdio_regs = (unsigned int *)&mac_dev->mdio_phy1;
  351. /* get the data */
  352. data = mdio_regs[regnum];
  353. value = data & 0xffff;
  354. return value;
  355. }
  356. static int tse_mdio_write(struct altera_tse_priv *priv, unsigned int regnum,
  357. unsigned int value)
  358. {
  359. volatile struct alt_tse_mac *mac_dev;
  360. unsigned int *mdio_regs;
  361. unsigned int data;
  362. mac_dev = priv->mac_dev;
  363. /* set mdio address */
  364. mac_dev->mdio_phy1_addr = priv->phyaddr;
  365. mdio_regs = (unsigned int *)&mac_dev->mdio_phy1;
  366. /* get the data */
  367. data = (unsigned int)value;
  368. mdio_regs[regnum] = data;
  369. return 0;
  370. }
  371. /* MDIO access to phy */
  372. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
  373. static int altera_tse_miiphy_write(const char *devname, unsigned char addr,
  374. unsigned char reg, unsigned short value)
  375. {
  376. struct eth_device *dev;
  377. struct altera_tse_priv *priv;
  378. dev = eth_get_dev_by_name(devname);
  379. priv = dev->priv;
  380. tse_mdio_write(priv, (uint) reg, (uint) value);
  381. return 0;
  382. }
  383. static int altera_tse_miiphy_read(const char *devname, unsigned char addr,
  384. unsigned char reg, unsigned short *value)
  385. {
  386. struct eth_device *dev;
  387. struct altera_tse_priv *priv;
  388. volatile struct alt_tse_mac *mac_dev;
  389. unsigned int *mdio_regs;
  390. dev = eth_get_dev_by_name(devname);
  391. priv = dev->priv;
  392. mac_dev = priv->mac_dev;
  393. mac_dev->mdio_phy1_addr = (int)addr;
  394. mdio_regs = (unsigned int *)&mac_dev->mdio_phy1;
  395. *value = 0xffff & mdio_regs[reg];
  396. return 0;
  397. }
  398. #endif
  399. /*
  400. * Also copied from tsec.c
  401. */
  402. /* Parse the status register for link, and then do
  403. * auto-negotiation
  404. */
  405. static uint mii_parse_sr(uint mii_reg, struct altera_tse_priv *priv)
  406. {
  407. /*
  408. * Wait if the link is up, and autonegotiation is in progress
  409. * (ie - we're capable and it's not done)
  410. */
  411. mii_reg = tse_mdio_read(priv, MIIM_STATUS);
  412. if (!(mii_reg & MIIM_STATUS_LINK) && (mii_reg & BMSR_ANEGCAPABLE)
  413. && !(mii_reg & BMSR_ANEGCOMPLETE)) {
  414. int i = 0;
  415. puts("Waiting for PHY auto negotiation to complete");
  416. while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
  417. /*
  418. * Timeout reached ?
  419. */
  420. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  421. puts(" TIMEOUT !\n");
  422. priv->link = 0;
  423. return 0;
  424. }
  425. if ((i++ % 1000) == 0)
  426. putc('.');
  427. udelay(1000); /* 1 ms */
  428. mii_reg = tse_mdio_read(priv, MIIM_STATUS);
  429. }
  430. puts(" done\n");
  431. priv->link = 1;
  432. udelay(500000); /* another 500 ms (results in faster booting) */
  433. } else {
  434. if (mii_reg & MIIM_STATUS_LINK) {
  435. debug("Link is up\n");
  436. priv->link = 1;
  437. } else {
  438. debug("Link is down\n");
  439. priv->link = 0;
  440. }
  441. }
  442. return 0;
  443. }
  444. /* Parse the 88E1011's status register for speed and duplex
  445. * information
  446. */
  447. static uint mii_parse_88E1011_psr(uint mii_reg, struct altera_tse_priv *priv)
  448. {
  449. uint speed;
  450. mii_reg = tse_mdio_read(priv, MIIM_88E1011_PHY_STATUS);
  451. if ((mii_reg & MIIM_88E1011_PHYSTAT_LINK) &&
  452. !(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
  453. int i = 0;
  454. puts("Waiting for PHY realtime link");
  455. while (!(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
  456. /* Timeout reached ? */
  457. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  458. puts(" TIMEOUT !\n");
  459. priv->link = 0;
  460. break;
  461. }
  462. if ((i++ == 1000) == 0) {
  463. i = 0;
  464. puts(".");
  465. }
  466. udelay(1000); /* 1 ms */
  467. mii_reg = tse_mdio_read(priv, MIIM_88E1011_PHY_STATUS);
  468. }
  469. puts(" done\n");
  470. udelay(500000); /* another 500 ms (results in faster booting) */
  471. } else {
  472. if (mii_reg & MIIM_88E1011_PHYSTAT_LINK)
  473. priv->link = 1;
  474. else
  475. priv->link = 0;
  476. }
  477. if (mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
  478. priv->duplexity = 1;
  479. else
  480. priv->duplexity = 0;
  481. speed = (mii_reg & MIIM_88E1011_PHYSTAT_SPEED);
  482. switch (speed) {
  483. case MIIM_88E1011_PHYSTAT_GBIT:
  484. priv->speed = 1000;
  485. debug("PHY Speed is 1000Mbit\n");
  486. break;
  487. case MIIM_88E1011_PHYSTAT_100:
  488. debug("PHY Speed is 100Mbit\n");
  489. priv->speed = 100;
  490. break;
  491. default:
  492. debug("PHY Speed is 10Mbit\n");
  493. priv->speed = 10;
  494. }
  495. return 0;
  496. }
  497. static uint mii_m88e1111s_setmode_sr(uint mii_reg, struct altera_tse_priv *priv)
  498. {
  499. uint mii_data = tse_mdio_read(priv, mii_reg);
  500. mii_data &= 0xfff0;
  501. if ((priv->flags >= 1) && (priv->flags <= 4))
  502. mii_data |= 0xb;
  503. else if (priv->flags == 5)
  504. mii_data |= 0x4;
  505. return mii_data;
  506. }
  507. static uint mii_m88e1111s_setmode_cr(uint mii_reg, struct altera_tse_priv *priv)
  508. {
  509. uint mii_data = tse_mdio_read(priv, mii_reg);
  510. mii_data &= ~0x82;
  511. if ((priv->flags >= 1) && (priv->flags <= 4))
  512. mii_data |= 0x82;
  513. return mii_data;
  514. }
  515. /*
  516. * Returns which value to write to the control register.
  517. * For 10/100, the value is slightly different
  518. */
  519. static uint mii_cr_init(uint mii_reg, struct altera_tse_priv *priv)
  520. {
  521. return MIIM_CONTROL_INIT;
  522. }
  523. /*
  524. * PHY & MDIO code
  525. * Need to add SGMII stuff
  526. *
  527. */
  528. static struct phy_info phy_info_M88E1111S = {
  529. 0x01410cc,
  530. "Marvell 88E1111S",
  531. 4,
  532. (struct phy_cmd[]){ /* config */
  533. /* Reset and configure the PHY */
  534. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  535. {MIIM_88E1111_PHY_EXT_SR, 0x848f,
  536. &mii_m88e1111s_setmode_sr},
  537. /* Delay RGMII TX and RX */
  538. {MIIM_88E1111_PHY_EXT_CR, 0x0cd2,
  539. &mii_m88e1111s_setmode_cr},
  540. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  541. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  542. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  543. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  544. {miim_end,}
  545. },
  546. (struct phy_cmd[]){ /* startup */
  547. /* Status is read once to clear old link state */
  548. {MIIM_STATUS, miim_read, NULL},
  549. /* Auto-negotiate */
  550. {MIIM_STATUS, miim_read, &mii_parse_sr},
  551. /* Read the status */
  552. {MIIM_88E1011_PHY_STATUS, miim_read,
  553. &mii_parse_88E1011_psr},
  554. {miim_end,}
  555. },
  556. (struct phy_cmd[]){ /* shutdown */
  557. {miim_end,}
  558. },
  559. };
  560. /* a generic flavor. */
  561. static struct phy_info phy_info_generic = {
  562. 0,
  563. "Unknown/Generic PHY",
  564. 32,
  565. (struct phy_cmd[]){ /* config */
  566. {MII_BMCR, BMCR_RESET, NULL},
  567. {MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART, NULL},
  568. {miim_end,}
  569. },
  570. (struct phy_cmd[]){ /* startup */
  571. {MII_BMSR, miim_read, NULL},
  572. {MII_BMSR, miim_read, &mii_parse_sr},
  573. {miim_end,}
  574. },
  575. (struct phy_cmd[]){ /* shutdown */
  576. {miim_end,}
  577. }
  578. };
  579. static struct phy_info *phy_info[] = {
  580. &phy_info_M88E1111S,
  581. NULL
  582. };
  583. /* Grab the identifier of the device's PHY, and search through
  584. * all of the known PHYs to see if one matches. If so, return
  585. * it, if not, return NULL
  586. */
  587. static struct phy_info *get_phy_info(struct eth_device *dev)
  588. {
  589. struct altera_tse_priv *priv = (struct altera_tse_priv *)dev->priv;
  590. uint phy_reg, phy_ID;
  591. int i;
  592. struct phy_info *theInfo = NULL;
  593. /* Grab the bits from PHYIR1, and put them in the upper half */
  594. phy_reg = tse_mdio_read(priv, MIIM_PHYIR1);
  595. phy_ID = (phy_reg & 0xffff) << 16;
  596. /* Grab the bits from PHYIR2, and put them in the lower half */
  597. phy_reg = tse_mdio_read(priv, MIIM_PHYIR2);
  598. phy_ID |= (phy_reg & 0xffff);
  599. /* loop through all the known PHY types, and find one that */
  600. /* matches the ID we read from the PHY. */
  601. for (i = 0; phy_info[i]; i++) {
  602. if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) {
  603. theInfo = phy_info[i];
  604. break;
  605. }
  606. }
  607. if (theInfo == NULL) {
  608. theInfo = &phy_info_generic;
  609. debug("%s: No support for PHY id %x; assuming generic\n",
  610. dev->name, phy_ID);
  611. } else
  612. debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
  613. return theInfo;
  614. }
  615. /* Execute the given series of commands on the given device's
  616. * PHY, running functions as necessary
  617. */
  618. static void phy_run_commands(struct altera_tse_priv *priv, struct phy_cmd *cmd)
  619. {
  620. int i;
  621. uint result;
  622. for (i = 0; cmd->mii_reg != miim_end; i++) {
  623. if (cmd->mii_data == miim_read) {
  624. result = tse_mdio_read(priv, cmd->mii_reg);
  625. if (cmd->funct != NULL)
  626. (*(cmd->funct)) (result, priv);
  627. } else {
  628. if (cmd->funct != NULL)
  629. result = (*(cmd->funct)) (cmd->mii_reg, priv);
  630. else
  631. result = cmd->mii_data;
  632. tse_mdio_write(priv, cmd->mii_reg, result);
  633. }
  634. cmd++;
  635. }
  636. }
  637. /* Phy init code */
  638. static int init_phy(struct eth_device *dev)
  639. {
  640. struct altera_tse_priv *priv = (struct altera_tse_priv *)dev->priv;
  641. struct phy_info *curphy;
  642. /* Get the cmd structure corresponding to the attached
  643. * PHY */
  644. curphy = get_phy_info(dev);
  645. if (curphy == NULL) {
  646. priv->phyinfo = NULL;
  647. debug("%s: No PHY found\n", dev->name);
  648. return 0;
  649. } else
  650. debug("%s found\n", curphy->name);
  651. priv->phyinfo = curphy;
  652. phy_run_commands(priv, priv->phyinfo->config);
  653. return 1;
  654. }
  655. static int tse_set_mac_address(struct eth_device *dev)
  656. {
  657. struct altera_tse_priv *priv = dev->priv;
  658. volatile struct alt_tse_mac *mac_dev = priv->mac_dev;
  659. debug("Setting MAC address to 0x%02x%02x%02x%02x%02x%02x\n",
  660. dev->enetaddr[5], dev->enetaddr[4],
  661. dev->enetaddr[3], dev->enetaddr[2],
  662. dev->enetaddr[1], dev->enetaddr[0]);
  663. mac_dev->mac_addr_0 = ((dev->enetaddr[3]) << 24 |
  664. (dev->enetaddr[2]) << 16 |
  665. (dev->enetaddr[1]) << 8 | (dev->enetaddr[0]));
  666. mac_dev->mac_addr_1 = ((dev->enetaddr[5] << 8 |
  667. (dev->enetaddr[4])) & 0xFFFF);
  668. /* Set the MAC address */
  669. mac_dev->supp_mac_addr_0_0 = mac_dev->mac_addr_0;
  670. mac_dev->supp_mac_addr_0_1 = mac_dev->mac_addr_1;
  671. /* Set the MAC address */
  672. mac_dev->supp_mac_addr_1_0 = mac_dev->mac_addr_0;
  673. mac_dev->supp_mac_addr_1_1 = mac_dev->mac_addr_1;
  674. /* Set the MAC address */
  675. mac_dev->supp_mac_addr_2_0 = mac_dev->mac_addr_0;
  676. mac_dev->supp_mac_addr_2_1 = mac_dev->mac_addr_1;
  677. /* Set the MAC address */
  678. mac_dev->supp_mac_addr_3_0 = mac_dev->mac_addr_0;
  679. mac_dev->supp_mac_addr_3_1 = mac_dev->mac_addr_1;
  680. return 0;
  681. }
  682. static int tse_eth_init(struct eth_device *dev, bd_t * bd)
  683. {
  684. int dat;
  685. struct altera_tse_priv *priv = dev->priv;
  686. volatile struct alt_tse_mac *mac_dev = priv->mac_dev;
  687. volatile struct alt_sgdma_descriptor *tx_desc = priv->tx_desc;
  688. volatile struct alt_sgdma_descriptor *rx_desc = priv->rx_desc;
  689. volatile struct alt_sgdma_descriptor *rx_desc_cur =
  690. (volatile struct alt_sgdma_descriptor *)&rx_desc[0];
  691. /* stop controller */
  692. debug("Reseting TSE & SGDMAs\n");
  693. tse_eth_reset(dev);
  694. /* start the phy */
  695. debug("Configuring PHY\n");
  696. phy_run_commands(priv, priv->phyinfo->startup);
  697. /* need to create sgdma */
  698. debug("Configuring tx desc\n");
  699. alt_sgdma_construct_descriptor_burst(
  700. (volatile struct alt_sgdma_descriptor *)&tx_desc[0],
  701. (volatile struct alt_sgdma_descriptor *)&tx_desc[1],
  702. (unsigned int *)NULL, /* read addr */
  703. (unsigned int *)0,
  704. 0, /* length or EOP ,will change for each tx */
  705. 0x1, /* gen eop */
  706. 0x0, /* read fixed */
  707. 0x1, /* write fixed or sop */
  708. 0x0, /* read burst */
  709. 0x0, /* write burst */
  710. 0x0 /* channel */
  711. );
  712. debug("Configuring rx desc\n");
  713. flush_dcache((unsigned long)(NetRxPackets[0]), PKTSIZE_ALIGN);
  714. alt_sgdma_construct_descriptor_burst(
  715. (volatile struct alt_sgdma_descriptor *)&rx_desc[0],
  716. (volatile struct alt_sgdma_descriptor *)&rx_desc[1],
  717. (unsigned int)0x0, /* read addr */
  718. (unsigned int *)NetRxPackets[0],
  719. 0x0, /* length or EOP */
  720. 0x0, /* gen eop */
  721. 0x0, /* read fixed */
  722. 0x0, /* write fixed or sop */
  723. 0x0, /* read burst */
  724. 0x0, /* write burst */
  725. 0x0 /* channel */
  726. );
  727. /* start rx async transfer */
  728. debug("Starting rx sgdma\n");
  729. alt_sgdma_do_async_transfer(priv->sgdma_rx, rx_desc_cur);
  730. /* start TSE */
  731. debug("Configuring TSE Mac\n");
  732. /* Initialize MAC registers */
  733. mac_dev->max_frame_length = PKTSIZE_ALIGN;
  734. mac_dev->rx_almost_empty_threshold = 8;
  735. mac_dev->rx_almost_full_threshold = 8;
  736. mac_dev->tx_almost_empty_threshold = 8;
  737. mac_dev->tx_almost_full_threshold = 3;
  738. mac_dev->tx_sel_empty_threshold =
  739. CONFIG_SYS_ALTERA_TSE_TX_FIFO - 16;
  740. mac_dev->tx_sel_full_threshold = 0;
  741. mac_dev->rx_sel_empty_threshold =
  742. CONFIG_SYS_ALTERA_TSE_TX_FIFO - 16;
  743. mac_dev->rx_sel_full_threshold = 0;
  744. /* NO Shift */
  745. mac_dev->rx_cmd_stat.bits.rx_shift16 = 0;
  746. mac_dev->tx_cmd_stat.bits.tx_shift16 = 0;
  747. /* enable MAC */
  748. dat = 0;
  749. dat = ALTERA_TSE_CMD_TX_ENA_MSK | ALTERA_TSE_CMD_RX_ENA_MSK;
  750. mac_dev->command_config.image = dat;
  751. /* configure the TSE core */
  752. /* -- output clocks, */
  753. /* -- and later config stuff for SGMII */
  754. if (priv->link) {
  755. debug("Adjusting TSE to link speed\n");
  756. tse_adjust_link(priv);
  757. }
  758. return priv->link ? 0 : -1;
  759. }
  760. /* TSE init code */
  761. int altera_tse_initialize(u8 dev_num, int mac_base,
  762. int sgdma_rx_base, int sgdma_tx_base,
  763. u32 sgdma_desc_base, u32 sgdma_desc_size)
  764. {
  765. struct altera_tse_priv *priv;
  766. struct eth_device *dev;
  767. struct alt_sgdma_descriptor *rx_desc;
  768. struct alt_sgdma_descriptor *tx_desc;
  769. unsigned long dma_handle;
  770. dev = (struct eth_device *)malloc(sizeof *dev);
  771. if (NULL == dev)
  772. return 0;
  773. memset(dev, 0, sizeof *dev);
  774. priv = malloc(sizeof(*priv));
  775. if (!priv) {
  776. free(dev);
  777. return 0;
  778. }
  779. if (sgdma_desc_size) {
  780. if (sgdma_desc_size < (sizeof(*tx_desc) * (3 + PKTBUFSRX))) {
  781. printf("ALTERA_TSE-%hu: "
  782. "descriptor memory is too small\n", dev_num);
  783. free(priv);
  784. free(dev);
  785. return 0;
  786. }
  787. tx_desc = (struct alt_sgdma_descriptor *)sgdma_desc_base;
  788. } else {
  789. tx_desc = dma_alloc_coherent(sizeof(*tx_desc) * (3 + PKTBUFSRX),
  790. &dma_handle);
  791. }
  792. rx_desc = tx_desc + 2;
  793. debug("tx desc: address = 0x%x\n", (unsigned int)tx_desc);
  794. debug("rx desc: address = 0x%x\n", (unsigned int)rx_desc);
  795. if (!tx_desc) {
  796. free(priv);
  797. free(dev);
  798. return 0;
  799. }
  800. memset(rx_desc, 0, (sizeof *rx_desc) * (PKTBUFSRX + 1));
  801. memset(tx_desc, 0, (sizeof *tx_desc) * 2);
  802. /* initialize tse priv */
  803. priv->mac_dev = (volatile struct alt_tse_mac *)mac_base;
  804. priv->sgdma_rx = (volatile struct alt_sgdma_registers *)sgdma_rx_base;
  805. priv->sgdma_tx = (volatile struct alt_sgdma_registers *)sgdma_tx_base;
  806. priv->phyaddr = CONFIG_SYS_ALTERA_TSE_PHY_ADDR;
  807. priv->flags = CONFIG_SYS_ALTERA_TSE_FLAGS;
  808. priv->rx_desc = rx_desc;
  809. priv->tx_desc = tx_desc;
  810. /* init eth structure */
  811. dev->priv = priv;
  812. dev->init = tse_eth_init;
  813. dev->halt = tse_eth_halt;
  814. dev->send = tse_eth_send;
  815. dev->recv = tse_eth_rx;
  816. dev->write_hwaddr = tse_set_mac_address;
  817. sprintf(dev->name, "%s-%hu", "ALTERA_TSE", dev_num);
  818. eth_register(dev);
  819. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
  820. miiphy_register(dev->name, altera_tse_miiphy_read,
  821. altera_tse_miiphy_write);
  822. #endif
  823. init_phy(dev);
  824. return 1;
  825. }